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 :)
24 skiptask partition ||
! type skiptask
# for running not in fai
26 #### begin configuration
35 ##### end configuration
40 if [[ $# == 1 ]]; then
47 if [[ $d == /dev
/disk
/by-id
/* ]]; then
55 bootdev
() { add-part $@
$bootn; }
56 rootdev
() { add-part $@
$rootn; }
57 swapdev
() { add-part $@
$swapn; }
58 bios_grubdev
() { add-part $@
$bios_grubn; }
60 crypt-dev
() { echo /dev
/mapper
/crypt_dev_
${1##*/}; }
61 crypt-name
() { echo crypt_dev_
${1##*/}; }
62 root-cryptdev
() { crypt-dev $
(rootdev $@
); }
63 swap-cryptdev
() { crypt-dev $
(swapdev $@
); }
64 root-cryptname
() { crypt-name $
(rootdev $@
); }
65 swap-cryptname
() { crypt-name $
(swapdev $@
); }
68 ##### end function defs
70 if ifclass REPARTITION
;then
71 partition
=true
# force a full wipe
73 partition
=false
# change to true to force a full wipe
82 for disk
in [sv
]d
[a-z
]; do
83 case $
(cat $disk/queue
/rotational
) in
84 0) ssds
+=(/dev
/$disk) ;;
85 1) hdds
+=(/dev
/$disk) ;;
86 *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
87 $(cat $disk/queue/rotational)"; exit 1 ;;
91 # install all ssds, or if there are none, all hdds
92 if (( ${#ssds[@]} > 0 )); then
93 short_devs
=( ${ssds[@]} )
95 short_devs
=( ${hdds[@]} )
98 # check if the partitions exist have the right filesystems
99 #blkid="$(blkid -s TYPE)"
100 for dev
in ${short_devs[@]}; do
101 ! $partition ||
break
102 y
=$
(readlink
-f $dev)
104 [[ ${#x[@]} == "${lastn}" ]] || partition
=true
105 for (( i
=1; i
<= lastn
; i
++ )); do
106 [[ -e ${dev}$i ]] || partition
=true
108 # On one system, blkid is missing some partitions.
109 # maybe we need a flag, like FUZZY_BLKID or something, so we
110 # can check that at least some exist.
111 # for x in "`rootdev`: TYPE=\"crypto_LUKS\"" "`bootdev`: TYPE=\"btrfs\""; do
112 # echo "$blkid" | grep -Fx "$x" &>/dev/null || partition=true
116 if $partition && ifclass PARTITION_PROMPT
; then
117 echo "Press any key except ctrl-c to continue and partition these drives:"
118 echo " ${short_devs[@]}"
124 for short_dev
in ${short_devs[@]}; do
125 devs
+=($
(devbyid
$short_dev))
131 for dev
in ${devs[@]}; do
132 if ifclass frodo
; then
133 # I ran into a machine where the bios doesn't know about some disks,
134 # so 1st stage of grub also doesn't know about them.
135 # Also, grub does not support mounting degraded btrfs as far as
136 # I can tell with some googling.
137 # From within an arch install env, I could detect them by noting
138 # their partitions were mixed with the next disk in /dev/disk/by-path,
139 # and I have mixed model disks, and I could see the 8 models which showed
140 # up in the bios, and thus see which 2 models were missing.
141 # hdparm -I /dev/sdh will give model info in linux.
142 # However, in fai on jessie, /dev/disk/by-path dir doesn't exist,
143 # and I don't see another way, so I'm hardcoding them.
144 # We still put grub on them and partition them the same, for uniformity
145 # and in case they get moved to a system that can recognize them,
146 # we just exclude them from the boot filesystem.
149 for id
in ata-TOSHIBA_MD04ACA500_8539K4TQFS9A \
150 ata-TOSHIBA_MD04ACA500_Y5IFK6IJFS9A
; do
151 if [[ $
(readlink
-f $id) == "$(readlink -f $dev)" ]]; then
156 $bad_disk || boot_devs
+=(`bootdev`)
158 boot_devs
+=(`bootdev`)
162 if [[ ! $DISTRO ]]; then
163 if ifclass STABLE
; then
172 case ${#boot_devs[@]} in
173 # need double the space if we are raid 10, and then
174 # might as well give some extra overhead.
175 [4-9]*|
[1-3]?
*) boot_mib
=$
((boot_mib
* 3)) ;;
179 bpart
() { # btrfs a partition
181 [1-3]) mkfs.btrfs
-f $@
;;
182 [4-9]*|
[1-3]?
*) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
186 first_boot_dev
=${boot_devs[0]}
188 # keyfiles generated like:
189 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
190 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
192 if [[ ! -e $luks_dir/host-
$HOSTNAME ]]; then
193 echo "$0: error: no key for hostname at $luks_dir/host-$HOSTNAME" >&2
198 lukspw
=$
(cat $luks_dir/traci
)
200 lukspw
=$
(cat $luks_dir/ian
)
202 if ifclass demohost
; then
207 first_root_crypt
=$
(root-cryptdev
${devs[0]})
210 # 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
211 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
212 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
216 for dev
in ${devs[@]}; do
217 root_devs
+=(`rootdev`)
221 for dev
in ${devs[@]}; do
222 for x
in $dev[0-9]; do
224 # wipefs has failed, manual run works, google suggests timing issue
225 while ! wipefs
-a $x; do
227 count_down
=$
((count_down
- 1))
228 (( count_down
> 0 )) ||
exit 1
232 for dev
in ${devs[@]}; do
233 # parted will round up the disk size. Do -1 so we can have
234 # fully 1MiB unit partitions for easy resizing of the last partition.
235 # Otherwise we would pass in -0 for the end argument for the last partition.
237 # parted print error output is expected. example:
238 # Error: /dev/vda: unrecognised disk label
239 disk_mib
=$
(( $
(parted
-m $dev unit MiB print | \
240 sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
241 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
/ ${#boot_devs[@]} ))
242 swap_end
=$
(( root_end
+ swap_mib
))
244 parted
-s $dev mklabel gpt
245 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB.
246 # I read something in the parted manual saying cheap flash media
247 # likes to start at 4.
248 # MiB because parted complains about alignment otherwise.
249 pcmd
="parted -a optimal -s -- $dev"
250 $pcmd mkpart primary
"ext3" 4MiB
${root_end}MiB
251 $pcmd mkpart primary
"linux-swap" ${root_end}MiB
${swap_end}MiB
252 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
253 $pcmd mkpart primary
"" 1MiB
4MiB
254 $pcmd set $bios_grubn bios_grub on
255 $pcmd set $bootn boot on
# generally not needed on modern systems
256 # the mkfs failed before on a vm, which prompted me to add
258 # then failed on a physical machine
260 # Device /dev/disk/by-id/foo doesn't exist or access denied,
263 while [[ ! -e `rootdev` ]] && (( secs
< 10 )); do
267 yes YES | cryptsetup luksFormat
`rootdev` $luks_dir/host-
$HOSTNAME \
268 -c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
270 cryptsetup luksAddKey
--key-file $luks_dir/host-
$HOSTNAME \
271 `rootdev` ||
[[ $?
== 141 ]]
272 # background: Keyfile and password are treated just
273 # like 2 ways to input a passphrase, so we don't actually need to have
274 # different contents of keyfile and passphrase, but it makes some
275 # security sense to a really big randomly generated passphrase
276 # as much as possible, so we have both.
278 # This would remove the keyfile.
279 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
280 # /key/file || [[ $? == 141 ]]
282 cryptsetup luksOpen
`rootdev` `root-cryptname` \
283 --key-file $luks_dir/host-
$HOSTNAME
285 ls -la /dev
/btrfs-control
287 bpart $
(for dev
in ${devs[@]}; do root-cryptdev
; done)
288 bpart
${boot_devs[@]}
290 for dev
in ${devs[@]}; do
291 cryptsetup luksOpen
`rootdev` `root-cryptname` \
292 --key-file $luks_dir/host-
$HOSTNAME
297 mount
-o subvolid
=0 $first_root_crypt /mnt
298 # systemd creates subvolumes we want to delete.
299 s
=($
(btrfs subvolume list
--sort=-path /mnt |
300 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
301 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
302 btrfs subvolume set-default
0 /mnt
303 [[ ! -e /mnt
/root_
$DISTRO ]] || btrfs subvolume delete
/mnt
/root_
$DISTRO
307 btrfs subvolume create root_
$DISTRO
308 [[ -e q
]] || btrfs subvolume create q
311 mkdir
-p /mnt
/root_
$DISTRO/boot
317 # could set default like this, but no reason to.
318 # btrfs subvolume set-default \
319 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
320 chattr -Rf +C root_
$DISTRO
323 mount
-o subvolid
=0 $first_boot_dev /mnt
325 btrfs subvolume set-default
0 /mnt
326 [[ ! -e /mnt
/boot_
$DISTRO ]] || btrfs subvolume delete
/mnt
/boot_
$DISTRO
327 btrfs subvolume create boot_
$DISTRO
330 ## end create subvols ##
333 # note, the mount point /a seems to get automatically created somewhere
334 cat > /tmp
/fai
/fstab
<<EOF
335 $first_root_crypt / btrfs noatime,subvol=root_$DISTRO 0 0
336 $first_root_crypt /q btrfs noatime,subvol=q 0 0
337 /q/a /a none bind 0 0
338 $first_boot_dev /boot btrfs noatime,subvol=boot_$DISTRO 0 0
342 # these 2 are alternative ways to identify a subvol, i think i prefer the 2nd.
343 if ifclass treetowl
; then
344 cat >> /tmp
/fai
/fstab
<<'EOF'
345 $first_root_crypt /i btrfs noatime,subvol=i 0 0
346 UUID=3f7b31cd-f299-40b4-a86b-7604282e2715 /i btrfs noatime 0 2
349 if ifclass frodo
; then
350 cat >> /tmp
/fai
/fstab
<<'EOF'
351 $first_root_crypt /i btrfs noatime,subvol=i 0 0
356 for dev
in ${devs[@]}; do
357 swaps
+=(`swap-cryptname`)
358 cat >>/tmp
/fai
/crypttab
<<EOF
359 `root-cryptname` `rootdev` none keyscript=/root/keyscript,discard,luks
360 `swap-cryptname` `swapdev` /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
362 cat >> /tmp
/fai
/fstab
<<EOF
363 `swap-cryptdev` none swap sw 0 0
368 #BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
370 # swaplist seems to do nothing.
372 cat >/tmp
/fai
/disk_var.sh
<<EOF
373 # ROOT_PARTITIONS is added by me, used in arch setup.
374 ROOT_PARTITIONS="${root_devs[@]}"
375 ROOT_PARTITION=\${ROOT_PARTITION:-$first_root_crypt}
376 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
377 BOOT_DEVICE="${short_devs[@]}"
378 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}