2 # Copyright (C) 2016 Ian Kelling
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
21 # # fai's setup-storage won't do btrfs on luks,
22 # # so we do it ourself :)
23 # inspiration taken from files in fai-setup-storage package
26 skiptask partition ||
! type skiptask
# for running not in fai
28 #### begin configuration
33 # ext partition so grub can write persistent variables,
34 # so it can do a one time boot.
36 # bios boot partition,
37 # https://wiki.archlinux.org/index.php/GRUB
43 ##### end configuration
46 add-part
() { # add partition suffix to $dev
48 if [[ $# == 1 ]]; then
55 if [[ $d == /dev
/disk
/by-id
/* ]]; then
63 bootdev
() { add-part $@
$bootn; }
64 rootdev
() { add-part $@
$rootn; }
65 swapdev
() { add-part $@
$swapn; }
66 grub_extdev
() { add-part $@
$grub_extn; }
67 # Commented because it's not used, but left because it
68 # finishes the pattern and if we ever do need to use it, it's here.
69 #bios_grubdev() { add-part $@ $bios_grubn; }
71 crypt-dev
() { echo /dev
/mapper
/crypt_dev_
${1##*/}; }
72 crypt-name
() { echo crypt_dev_
${1##*/}; }
73 root-cryptdev
() { crypt-dev $
(rootdev $@
); }
74 swap-cryptdev
() { crypt-dev $
(swapdev $@
); }
75 root-cryptname
() { crypt-name $
(rootdev $@
); }
76 swap-cryptname
() { crypt-name $
(swapdev $@
); }
79 ##### end function defs
81 if ifclass REPARTITION
;then
82 partition
=true
# force a full wipe
84 partition
=false
# change to true to force a full wipe
92 for disk
in [sv
]d
[a-z
]; do
93 case $
(cat $disk/queue
/rotational
) in
94 0) ssds
+=(/dev
/$disk) ;;
95 1) hdds
+=(/dev
/$disk) ;;
96 *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
97 $(cat $disk/queue/rotational)"; exit 1 ;;
101 # install all ssds, or if there are none, all hdds
102 if (( ${#ssds[@]} > 0 )); then
103 short_devs
=( ${ssds[@]} )
105 short_devs
=( ${hdds[@]} )
108 # check if the partitions exist have the right filesystems
109 #blkid="$(blkid -s TYPE)"
110 for dev
in ${short_devs[@]}; do
111 if $partition; then break; fi
112 y
=$
(readlink
-f $dev)
114 [[ ${#x[@]} == "${lastn}" ]] || partition
=true
115 for (( i
=1; i
<= lastn
; i
++ )); do
116 [[ -e ${dev}$i ]] || partition
=true
118 # On one system, blkid is missing some partitions.
119 # maybe we need a flag, like FUZZY_BLKID or something, so we
120 # can check that at least some exist.
121 # for x in "`rootdev`: TYPE=\"crypto_LUKS\"" "`bootdev`: TYPE=\"btrfs\""; do
122 # echo "$blkid" | grep -Fx "$x" &>/dev/null || partition=true
126 if $partition && ifclass PARTITION_PROMPT
; then
127 echo "Press any key except ctrl-c to continue and partition these drives:"
128 echo " ${short_devs[*]}"
134 for short_dev
in ${short_devs[@]}; do
135 devs
+=($
(devbyid
$short_dev))
141 for dev
in ${devs[@]}; do
142 if ifclass frodo
; then
143 # I ran into a machine where the bios doesn't know about some disks,
144 # so 1st stage of grub also doesn't know about them.
145 # Also, grub does not support mounting degraded btrfs as far as
146 # I can tell with some googling.
147 # From within an arch install env, I could detect them by noting
148 # their partitions were mixed with the next disk in /dev/disk/by-path,
149 # and I have mixed model disks, and I could see the 8 models which showed
150 # up in the bios, and thus see which 2 models were missing.
151 # hdparm -I /dev/sdh will give model info in linux.
152 # However, in fai on jessie, /dev/disk/by-path dir doesn't exist,
153 # and I don't see another way, so I'm hardcoding them.
154 # We still put grub on them and partition them the same, for uniformity
155 # and in case they get moved to a system that can recognize them,
156 # we just exclude them from the boot filesystem.
159 for id
in ata-TOSHIBA_MD04ACA500_8539K4TQFS9A \
160 ata-TOSHIBA_MD04ACA500_Y5IFK6IJFS9A
; do
161 if [[ $
(readlink
-f $id) == "$(readlink -f $dev)" ]]; then
166 $bad_disk || boot_devs
+=(`bootdev`)
168 boot_devs
+=(`bootdev`)
170 if [[ $boot_devs && $first ]]; then
171 first_grub_extdev
=`grub_extdev`
176 if [[ ! $DISTRO ]]; then
177 if ifclass STABLE_BOOTSTRAP
; then
178 DISTRO
=debianstable_bootstrap
179 elif ifclass STRETCH64
; then
181 elif ifclass STABLE
; then
183 elif ifclass XENIAL64
; then
185 elif ifclass BELENOS64
; then
186 DISTRO
=trisquelbelenos
188 echo "PARTITIONER ERROR: no distro class/var set" >&2
192 first_boot_dev
=${boot_devs[0]}
195 case ${#boot_devs[@]} in
196 # need double the space if we are raid 10, and then
197 # might as well give some extra overhead.
198 [4-9]*|
[1-3]?
*) boot_mib
=$
((boot_mib
* 3)) ;;
202 bpart
() { # btrfs a partition
204 [1-3]) mkfs.btrfs
-f $@
;;
205 [4-9]*|
[1-3]?
*) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
210 # keyfiles generated like:
211 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
212 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
214 if [[ ! -e $luks_dir/host-
$HOSTNAME ]]; then
215 echo "$0: error: no key for hostname at $luks_dir/host-$HOSTNAME" >&2
220 lukspw
=$
(cat $luks_dir/traci
)
222 lukspw
=$
(cat $luks_dir/ian
)
224 if ifclass demohost
; then
229 first_root_crypt
=$
(root-cryptdev
${devs[0]})
231 # 1.5 x based on https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Installation_Guide/sect-disk-partitioning-setup-x86.html#sect-custom-partitioning-x86
232 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
233 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
237 for dev
in ${devs[@]}; do
238 root_devs
+=(`rootdev`)
242 for dev
in ${devs[@]}; do
243 for x
in $dev[0-9]; do
245 # wipefs has failed, manual run works, google suggests timing issue
246 while ! wipefs
-a $x; do
248 count_down
=$
((count_down
- 1))
249 (( count_down
> 0 )) ||
exit 1
253 for dev
in ${devs[@]}; do
254 # parted will round up the disk size. Do -1 so we can have
255 # fully 1MiB unit partitions for easy resizing of the last partition.
256 # Otherwise we would pass in -0 for the end argument for the last partition.
258 # parted print error output is expected. example:
259 # Error: /dev/vda: unrecognised disk label
260 disk_mib
=$
(( $
(parted
-m $dev unit MiB print | \
261 sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
262 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
/ ${#boot_devs[@]} ))
263 swap_end
=$
(( root_end
+ swap_mib
))
265 parted
-s $dev mklabel gpt
266 # MiB because parted complains about alignment otherwise.
267 pcmd
="parted -a optimal -s -- $dev"
268 $pcmd mkpart primary
"ext3" 12MiB
${root_end}MiB
269 $pcmd mkpart primary
"linux-swap" ${root_end}MiB
${swap_end}MiB
270 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
271 # i only need a few k, but googling min size,
272 # I found someone saying that gparted required
273 # required at least 8 because of their hard drive cylinder size.
274 # And 8 is still very tiny.
275 $pcmd mkpart primary
"ext2" 4MiB
12MiB
276 # gpt ubuntu cloud image uses ~4 mb for this partition. fai uses 1 MiB.
277 # so, I use 3, whatever.
278 # note: parted manual saying cheap flash media
279 # should to start at 4.
280 $pcmd mkpart primary
"" 1MiB
4MiB
281 $pcmd set $bios_grubn bios_grub on
282 $pcmd set $bootn boot on
# generally not needed on modern systems
283 # the mkfs failed before on a vm, which prompted me to add
285 # then it failed again on a physical machine
287 # Device /dev/disk/by-id/foo doesn't exist or access denied,
288 # so I added a wait until it existed.
289 # Then I added the mkfs.ext2, which claimed to succeed,
290 # but then couldn't be found upon reboot. In that case we didn't
291 # wait at all. So I've added a 3 second minimum wait.
294 while [[ ! -e `rootdev` ]] && (( secs
< 10 )); do
298 # Holds just a single file, rarely written, so
299 # use ext2, like was often used for the /boot partition.
300 # This exists because grub can only persist data to a non-cow fs.
301 # And we use persisting a var in grub to do a one time boot.
302 # We could pass the data on the kernel command line and persist it
303 # to grubenv after booting, but that relies on the boot always succeeding.
304 # This is just a bit more robust, and it could work for booting
305 # into ipxe which can't persist data, if we ever got that working.
306 mkfs.ext2
`grub_extdev`
307 yes YES | cryptsetup luksFormat
`rootdev` $luks_dir/host-
$HOSTNAME \
308 -c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
310 cryptsetup luksAddKey
--key-file $luks_dir/host-
$HOSTNAME \
311 `rootdev` ||
[[ $?
== 141 ]]
312 # background: Keyfile and password are treated just
313 # like 2 ways to input a passphrase, so we don't actually need to have
314 # different contents of keyfile and passphrase, but it makes some
315 # security sense to a really big randomly generated passphrase
316 # as much as possible, so we have both.
318 # This would remove the keyfile.
319 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
320 # /key/file || [[ $? == 141 ]]
322 cryptsetup luksOpen
`rootdev` `root-cryptname` \
323 --key-file $luks_dir/host-
$HOSTNAME
325 ls -la /dev
/btrfs-control
# this was probably for debugging...
327 bpart $
(for dev
in ${devs[@]}; do root-cryptdev
; done)
328 bpart
${boot_devs[@]}
330 for dev
in ${devs[@]}; do
331 mkfs.ext2
`grub_extdev`
332 cryptsetup luksOpen
`rootdev` `root-cryptname` \
333 --key-file $luks_dir/host-
$HOSTNAME
339 if [[ $DISTRO != debianstable_bootstrap
]]; then
340 # bootstrap distro doesn't use separate encrypted root.
341 mount
-o subvolid
=0 $first_root_crypt /mnt
342 # systemd creates subvolumes we want to delete.
343 s
=($
(btrfs subvolume list
--sort=-path /mnt |
344 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
345 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
346 btrfs subvolume set-default
0 /mnt
347 [[ ! -e /mnt
/root_
$DISTRO ]] || btrfs subvolume delete
/mnt
/root_
$DISTRO
352 btrfs subvolume create root_
$DISTRO
353 [[ -e q
]] || btrfs subvolume create q
356 mkdir
-p /mnt
/root_
$DISTRO/boot
362 # could set default like this, but no reason to.
363 # btrfs subvolume set-default \
364 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
366 # no cow on the root filesystem. it's setup is fully scripted,
367 # (immutable in buzzwords). if it messes up, we will just recreated it,
368 # and we can get better perf with this.
369 # I can't remember exactly why, but this is preferable to mounting with
370 # -o nodatacow, I think because subvolumes inherit that.
371 chattr -Rf +C root_
$DISTRO
376 mount
-o subvolid
=0 $first_boot_dev /mnt
378 btrfs subvolume set-default
0 /mnt
# already default, just ensuring it.
380 # for libreboot systems.
382 cp $FAI/distro-install-common
/libreboot_grub.cfg
/mnt
/grub2
384 if [[ $DISTRO == debianstable_bootstrap
]]; then
385 # this is just convenience for the libreboot_grub config
386 # so we can glob the other ones easier.
389 boot_vol
=boot_
$DISTRO
391 [[ ! -e /mnt
/$boot_vol ]] || btrfs subvolume delete
/mnt
/$boot_vol
392 btrfs subvolume create
$boot_vol
395 ## end create subvols ##
398 mount
$first_grub_extdev /mnt
399 grub-editenv
/mnt
/grubenv
set did_fai_check
=true
400 grub-editenv
/mnt
/grubenv
set last_boot
=/$boot_vol
403 if [[ $DISTRO == debianstable_bootstrap
]]; then
404 cat > /tmp
/fai
/fstab
<<EOF
405 $first_boot_dev / btrfs noatime,subvol=$boot_vol 0 0
407 cat >/tmp
/fai
/disk_var.sh
<<EOF
408 BOOT_DEVICE="${short_devs[@]}"
409 ROOT_PARTITION=$first_boot_dev
412 # note, the mount point /a seems to get automatically created somewhere
413 cat > /tmp
/fai
/fstab
<<EOF
414 $first_root_crypt / btrfs noatime,subvol=root_$DISTRO 0 0
415 $first_root_crypt /q btrfs noatime,subvol=q 0 0
416 /q/a /a none bind 0 0
417 $first_boot_dev /boot btrfs noatime,subvol=$boot_vol 0 0
420 # I will avoid using uuid in the future. the other way of specifying is simpler
421 if ifclass treetowl
; then
422 cat >> /tmp
/fai
/fstab
<<'EOF'
423 $first_root_crypt /i btrfs noatime,subvol=i 0 0
424 UUID=3f7b31cd-f299-40b4-a86b-7604282e2715 /i btrfs noatime 0 2
429 for dev
in ${devs[@]}; do
430 swaps
+=(`swap-cryptname`)
431 cat >>/tmp
/fai
/crypttab
<<EOF
432 `root-cryptname` `rootdev` none keyscript=/root/keyscript,discard,luks
433 `swap-cryptname` `swapdev` /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
435 cat >> /tmp
/fai
/fstab
<<EOF
436 `swap-cryptdev` none swap sw 0 0
441 #BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
443 # note: swaplist seems to do nothing.
444 cat >/tmp
/fai
/disk_var.sh
<<EOF
445 BOOT_DEVICE="${short_devs[@]}"
446 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
447 # ROOT_PARTITIONS is added by me, used in arch setup.
448 ROOT_PARTITIONS="${root_devs[@]}"
449 ROOT_PARTITION=\${ROOT_PARTITION:-$first_root_crypt}
450 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}