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. grub can't write to
35 # btrfs or any cow fs because it's more
36 # more complicated to do and they don't want to.
38 # bios boot partition,
39 # https://wiki.archlinux.org/index.php/GRUB
42 # this is larger than needed for several /boot subvols,
43 # becuase I keep a minimal debian install on it, for
44 # recovery needs, and for doing pxe-kexec.
48 ##### end configuration
51 add-part
() { # add partition suffix to $dev
53 if [[ $# == 1 ]]; then
60 if [[ $d == /dev
/disk
/by-id
/* ]]; then
68 bootdev
() { add-part $@
$bootn; }
69 rootdev
() { add-part $@
$rootn; }
70 swapdev
() { add-part $@
$swapn; }
71 grub_extdev
() { add-part $@
$grub_extn; }
72 # Commented because it's not used, but left because it
73 # finishes the pattern and if we ever do need to use it, it's here.
74 #bios_grubdev() { add-part $@ $bios_grubn; }
76 crypt-dev
() { echo /dev
/mapper
/crypt_dev_
${1##*/}; }
77 crypt-name
() { echo crypt_dev_
${1##*/}; }
78 root-cryptdev
() { crypt-dev $
(rootdev $@
); }
79 swap-cryptdev
() { crypt-dev $
(swapdev $@
); }
80 root-cryptname
() { crypt-name $
(rootdev $@
); }
81 swap-cryptname
() { crypt-name $
(swapdev $@
); }
84 ##### end function defs
86 if ifclass REPARTITION
;then
87 partition
=true
# force a full wipe
89 partition
=false
# change to true to force a full wipe
97 for disk
in [sv
]d
[a-z
]; do
98 case $
(cat $disk/queue
/rotational
) in
99 0) ssds
+=(/dev
/$disk) ;;
100 1) hdds
+=(/dev
/$disk) ;;
101 *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
102 $(cat $disk/queue/rotational)"; exit 1 ;;
106 # install all ssds, or if there are none, all hdds
107 if ! ifclass ROTATIONAL
&& (( ${#ssds[@]} > 0 )); then
108 short_devs
=( ${ssds[@]} )
110 short_devs
=( ${hdds[@]} )
113 # check if the partitions exist have the right filesystems
114 #blkid="$(blkid -s TYPE)"
115 for dev
in ${short_devs[@]}; do
116 if $partition; then break; fi
117 y
=$
(readlink
-f $dev)
119 [[ ${#x[@]} == "${lastn}" ]] || partition
=true
120 for (( i
=1; i
<= lastn
; i
++ )); do
121 [[ -e ${dev}$i ]] || partition
=true
123 # On one system, blkid is missing some partitions.
124 # maybe we need a flag, like FUZZY_BLKID or something, so we
125 # can check that at least some exist.
126 # for x in "`rootdev`: TYPE=\"crypto_LUKS\"" "`bootdev`: TYPE=\"btrfs\""; do
127 # echo "$blkid" | grep -Fx "$x" &>/dev/null || partition=true
131 if $partition && ifclass PARTITION_PROMPT
; then
132 echo "Press any key except ctrl-c to continue and partition these drives:"
133 echo " ${short_devs[*]}"
139 for short_dev
in ${short_devs[@]}; do
140 devs
+=($
(devbyid
$short_dev))
146 for dev
in ${devs[@]}; do
147 if ifclass frodo
; then
148 # I ran into a machine where the bios doesn't know about some disks,
149 # so 1st stage of grub also doesn't know about them.
150 # Also, grub does not support mounting degraded btrfs as far as
151 # I can tell with some googling.
152 # From within an arch install env, I could detect them by noting
153 # their partitions were mixed with the next disk in /dev/disk/by-path,
154 # and I have mixed model disks, and I could see the 8 models which showed
155 # up in the bios, and thus see which 2 models were missing.
156 # hdparm -I /dev/sdh will give model info in linux.
157 # However, in fai on jessie, /dev/disk/by-path dir doesn't exist,
158 # and I don't see another way, so I'm hardcoding them.
159 # We still put grub on them and partition them the same, for uniformity
160 # and in case they get moved to a system that can recognize them,
161 # we just exclude them from the boot filesystem.
164 for id
in ata-TOSHIBA_MD04ACA500_8539K4TQFS9A \
165 ata-TOSHIBA_MD04ACA500_Y5IFK6IJFS9A
; do
166 if [[ $
(readlink
-f $id) == "$(readlink -f $dev)" ]]; then
171 $bad_disk || boot_devs
+=(`bootdev`)
173 boot_devs
+=(`bootdev`)
175 if [[ $boot_devs && $first ]]; then
176 first_grub_extdev
=`grub_extdev`
181 if ifclass RAID0 ||
(( ${#boot_devs[@]} < 4 )); then
185 # need double the space if we are raid 10, and then
186 # might as well give some extra.
187 boot_mib
=$
((boot_mib
* 3))
192 if [[ ! $DISTRO ]]; then
193 if ifclass STABLE_BOOTSTRAP
; then
194 DISTRO
=debianstable_bootstrap
195 elif ifclass STRETCH64
; then
197 elif ifclass STABLE
; then
199 elif ifclass XENIAL64
; then
201 elif ifclass BELENOS64
; then
202 DISTRO
=trisquelbelenos
204 echo "PARTITIONER ERROR: no distro class/var set" >&2
208 first_boot_dev
=${boot_devs[0]}
211 bpart
() { # btrfs a partition
213 0) mkfs.btrfs
-f $@
;;
214 10) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
219 # keyfiles generated like:
220 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
221 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
223 if [[ ! -e $luks_dir/host-
$HOSTNAME ]]; then
224 echo "$0: error: no key for hostname at $luks_dir/host-$HOSTNAME" >&2
229 lukspw
=$
(cat $luks_dir/traci
)
231 lukspw
=$
(cat $luks_dir/ian
)
233 if ifclass demohost
; then
238 first_root_crypt
=$
(root-cryptdev
${devs[0]})
240 # 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
241 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
242 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
246 for dev
in ${devs[@]}; do
247 root_devs
+=(`rootdev`)
251 for dev
in ${devs[@]}; do
252 # if we repartition to the same as an old partition,
253 # we don't want any old fses hanging around.
254 for (( i
=1; i
<= lastn
; i
++ )); do
255 x
=$
(add-part
$dev $i)
256 [[ -e $x ]] ||
continue
258 # wipefs has failed, manual run works, google suggests timing issue
259 while ! wipefs
-a $x; do
261 count_down
=$
((count_down
- 1))
262 (( count_down
> 0 )) ||
exit 1
266 for dev
in ${devs[@]}; do
267 # parted will round up the disk size. Do -1 so we can have
268 # fully 1MiB unit partitions for easy resizing of the last partition.
269 # Otherwise we would pass in -0 for the end argument for the last partition.
271 # parted print error output is expected. example:
272 # Error: /dev/vda: unrecognised disk label
273 disk_mib
=$
(( $
(parted
-m $dev unit MiB print | \
274 sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
275 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
/ ${#boot_devs[@]} ))
276 swap_end
=$
(( root_end
+ swap_mib
))
278 parted
-s $dev mklabel gpt
279 # MiB because parted complains about alignment otherwise.
280 pcmd
="parted -a optimal -s -- $dev"
281 $pcmd mkpart primary
"ext3" 12MiB
${root_end}MiB
282 $pcmd mkpart primary
"linux-swap" ${root_end}MiB
${swap_end}MiB
283 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
284 # i only need a few k, but googling min size,
285 # I found someone saying that gparted required
286 # required at least 8 because of their hard drive cylinder size.
287 # And 8 is still very tiny.
288 $pcmd mkpart primary
"ext2" 4MiB
12MiB
289 # gpt ubuntu cloud image uses ~4 mb for this partition. fai uses 1 MiB.
290 # so, I use 3, whatever.
291 # note: parted manual saying cheap flash media
292 # should to start at 4.
293 $pcmd mkpart primary
"" 1MiB
4MiB
294 $pcmd set $bios_grubn bios_grub on
295 $pcmd set $bootn boot on
# generally not needed on modern systems
296 # the mkfs failed before on a vm, which prompted me to add
298 # then it failed again on a physical machine
300 # Device /dev/disk/by-id/foo doesn't exist or access denied,
301 # so I added a wait until it existed.
302 # Then I added the mkfs.ext2, which claimed to succeed,
303 # but then couldn't be found upon reboot. In that case we didn't
304 # wait at all. So I've added a 3 second minimum wait.
307 while [[ ! -e `rootdev` ]] && (( secs
< 10 )); do
311 # Holds just a single file, rarely written, so
312 # use ext2, like was often used for the /boot partition.
313 # This exists because grub can only persist data to a non-cow fs.
314 # And we use persisting a var in grub to do a one time boot.
315 # We could pass the data on the kernel command line and persist it
316 # to grubenv after booting, but that relies on the boot always succeeding.
317 # This is just a bit more robust, and it could work for booting
318 # into ipxe which can't persist data, if we ever got that working.
319 mkfs.ext2
`grub_extdev`
320 yes YES | cryptsetup luksFormat
`rootdev` $luks_dir/host-
$HOSTNAME \
321 -c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
323 cryptsetup luksAddKey
--key-file $luks_dir/host-
$HOSTNAME \
324 `rootdev` ||
[[ $?
== 141 ]]
325 # background: Keyfile and password are treated just
326 # like 2 ways to input a passphrase, so we don't actually need to have
327 # different contents of keyfile and passphrase, but it makes some
328 # security sense to a really big randomly generated passphrase
329 # as much as possible, so we have both.
331 # This would remove the keyfile.
332 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
333 # /key/file || [[ $? == 141 ]]
335 cryptsetup luksOpen
`rootdev` `root-cryptname` \
336 --key-file $luks_dir/host-
$HOSTNAME
338 ls -la /dev
/btrfs-control
# this was probably for debugging...
340 bpart $
(for dev
in ${devs[@]}; do root-cryptdev
; done)
341 bpart
${boot_devs[@]}
343 for dev
in ${devs[@]}; do
344 mkfs.ext2
`grub_extdev`
345 cryptsetup luksOpen
`rootdev` `root-cryptname` \
346 --key-file $luks_dir/host-
$HOSTNAME
352 if [[ $DISTRO != debianstable_bootstrap
]]; then
353 # bootstrap distro doesn't use separate encrypted root.
354 mount
-o subvolid
=0 $first_root_crypt /mnt
355 # systemd creates subvolumes we want to delete.
356 s
=($
(btrfs subvolume list
--sort=-path /mnt |
357 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
358 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
359 btrfs subvolume set-default
0 /mnt
360 [[ ! -e /mnt
/root_
$DISTRO ]] || btrfs subvolume delete
/mnt
/root_
$DISTRO
365 btrfs subvolume create root_
$DISTRO
367 mkdir
-p /mnt
/root_
$DISTRO/boot
368 # could set default subvol like this, but no reason to.
369 # btrfs subvolume set-default \
370 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
372 # no cow on the root filesystem. it's setup is fully scripted,
373 # if it's messed up, we will just recreated it,
374 # and we can get better perf with this.
375 # I can't remember exactly why, but this is preferable to mounting with
376 # -o nodatacow, I think because subvolumes inherit that.
377 chattr -Rf +C root_
$DISTRO
382 mount
-o subvolid
=0 $first_boot_dev /mnt
384 btrfs subvolume set-default
0 /mnt
# already default, just ensuring it.
386 # for libreboot systems. grub2 only reads from subvolid=0
388 cp $FAI/distro-install-common
/libreboot_grub.cfg
/mnt
/grub2
390 if [[ $DISTRO == debianstable_bootstrap
]]; then
391 # this is just convenience for the libreboot_grub config
392 # so we can glob the other ones easier.
395 boot_vol
=boot_
$DISTRO
397 [[ ! -e /mnt
/$boot_vol ]] || btrfs subvolume delete
/mnt
/$boot_vol
398 btrfs subvolume create
$boot_vol
401 ## end create subvols ##
404 mount
$first_grub_extdev /mnt
405 grub-editenv
/mnt
/grubenv
set did_fai_check
=true
406 grub-editenv
/mnt
/grubenv
set last_boot
=/$boot_vol
409 if [[ $DISTRO == debianstable_bootstrap
]]; then
410 cat > /tmp
/fai
/fstab
<<EOF
411 $first_boot_dev / btrfs noatime,subvol=$boot_vol 0 0
413 cat >/tmp
/fai
/disk_var.sh
<<EOF
414 BOOT_DEVICE="${short_devs[@]}"
415 ROOT_PARTITION=$first_boot_dev
418 # note, fai creates the mountpoints listed here
419 cat > /tmp
/fai
/fstab
<<EOF
420 $first_root_crypt / btrfs noatime,subvol=root_$DISTRO 0 0
421 $first_root_crypt /mnt/root btrfs noatime,subvolid=0 0 0
422 $first_boot_dev /boot btrfs noatime,subvol=$boot_vol 0 0
425 for dev
in ${devs[@]}; do
426 swaps
+=(`swap-cryptname`)
427 cat >>/tmp
/fai
/crypttab
<<EOF
428 `root-cryptname` `rootdev` none keyscript=/root/keyscript,discard,luks
429 `swap-cryptname` `swapdev` /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
431 cat >> /tmp
/fai
/fstab
<<EOF
432 `swap-cryptdev` none swap sw 0 0
437 #BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
439 # note: swaplist seems to do nothing.
440 cat >/tmp
/fai
/disk_var.sh
<<EOF
441 BOOT_DEVICE="${short_devs[@]}"
442 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
443 # ROOT_PARTITIONS is added by me, used in arch setup.
444 ROOT_PARTITIONS="${root_devs[@]}"
445 ROOT_PARTITION=\${ROOT_PARTITION:-$first_root_crypt}
446 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}