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 STRETCH64
; then
165 elif ifclass STABLE
; then
167 elif ifclass XENIAL64
; then
169 elif ifclass BELENOS64
; then
170 DISTRO
=trisquelbelenos
172 echo "PARTITIONER ERROR: no distro class/var set" >&2
178 case ${#boot_devs[@]} in
179 # need double the space if we are raid 10, and then
180 # might as well give some extra overhead.
181 [4-9]*|
[1-3]?
*) boot_mib
=$
((boot_mib
* 3)) ;;
185 bpart
() { # btrfs a partition
187 [1-3]) mkfs.btrfs
-f $@
;;
188 [4-9]*|
[1-3]?
*) mkfs.btrfs
-f -m raid10
-d raid10 $@
;;
192 first_boot_dev
=${boot_devs[0]}
194 # keyfiles generated like:
195 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
196 luks_dir
=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
198 if [[ ! -e $luks_dir/host-
$HOSTNAME ]]; then
199 echo "$0: error: no key for hostname at $luks_dir/host-$HOSTNAME" >&2
204 lukspw
=$
(cat $luks_dir/traci
)
206 lukspw
=$
(cat $luks_dir/ian
)
208 if ifclass demohost
; then
213 first_root_crypt
=$
(root-cryptdev
${devs[0]})
216 # 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
217 swap_mib
=$
(( $
(grep ^MemTotal
: /proc
/meminfo | \
218 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
222 for dev
in ${devs[@]}; do
223 root_devs
+=(`rootdev`)
227 for dev
in ${devs[@]}; do
228 for x
in $dev[0-9]; do
230 # wipefs has failed, manual run works, google suggests timing issue
231 while ! wipefs
-a $x; do
233 count_down
=$
((count_down
- 1))
234 (( count_down
> 0 )) ||
exit 1
238 for dev
in ${devs[@]}; do
239 # parted will round up the disk size. Do -1 so we can have
240 # fully 1MiB unit partitions for easy resizing of the last partition.
241 # Otherwise we would pass in -0 for the end argument for the last partition.
243 # parted print error output is expected. example:
244 # Error: /dev/vda: unrecognised disk label
245 disk_mib
=$
(( $
(parted
-m $dev unit MiB print | \
246 sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
247 root_end
=$
(( disk_mib
- swap_mib
- boot_mib
/ ${#boot_devs[@]} ))
248 swap_end
=$
(( root_end
+ swap_mib
))
250 parted
-s $dev mklabel gpt
251 # gpt ubuntu cloud image uses ~4. fai uses 1 MiB.
252 # I read something in the parted manual saying cheap flash media
253 # likes to start at 4.
254 # MiB because parted complains about alignment otherwise.
255 pcmd
="parted -a optimal -s -- $dev"
256 $pcmd mkpart primary
"ext3" 4MiB
${root_end}MiB
257 $pcmd mkpart primary
"linux-swap" ${root_end}MiB
${swap_end}MiB
258 $pcmd mkpart primary
"" ${swap_end}MiB
${disk_mib}MiB
259 $pcmd mkpart primary
"" 1MiB
4MiB
260 $pcmd set $bios_grubn bios_grub on
261 $pcmd set $bootn boot on
# generally not needed on modern systems
262 # the mkfs failed before on a vm, which prompted me to add
264 # then failed on a physical machine
266 # Device /dev/disk/by-id/foo doesn't exist or access denied,
269 while [[ ! -e `rootdev` ]] && (( secs
< 10 )); do
273 yes YES | cryptsetup luksFormat
`rootdev` $luks_dir/host-
$HOSTNAME \
274 -c aes-cbc-essiv
:sha256
-s 256 ||
[[ $?
== 141 ]]
276 cryptsetup luksAddKey
--key-file $luks_dir/host-
$HOSTNAME \
277 `rootdev` ||
[[ $?
== 141 ]]
278 # background: Keyfile and password are treated just
279 # like 2 ways to input a passphrase, so we don't actually need to have
280 # different contents of keyfile and passphrase, but it makes some
281 # security sense to a really big randomly generated passphrase
282 # as much as possible, so we have both.
284 # This would remove the keyfile.
285 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
286 # /key/file || [[ $? == 141 ]]
288 cryptsetup luksOpen
`rootdev` `root-cryptname` \
289 --key-file $luks_dir/host-
$HOSTNAME
291 ls -la /dev
/btrfs-control
293 bpart $
(for dev
in ${devs[@]}; do root-cryptdev
; done)
294 bpart
${boot_devs[@]}
296 for dev
in ${devs[@]}; do
297 cryptsetup luksOpen
`rootdev` `root-cryptname` \
298 --key-file $luks_dir/host-
$HOSTNAME
303 mount
-o subvolid
=0 $first_root_crypt /mnt
304 # systemd creates subvolumes we want to delete.
305 s
=($
(btrfs subvolume list
--sort=-path /mnt |
306 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
307 for subvol
in ${s[@]}; do btrfs subvolume delete
/mnt
/$subvol; done
308 btrfs subvolume set-default
0 /mnt
309 [[ ! -e /mnt
/root_
$DISTRO ]] || btrfs subvolume delete
/mnt
/root_
$DISTRO
313 btrfs subvolume create root_
$DISTRO
314 [[ -e q
]] || btrfs subvolume create q
317 mkdir
-p /mnt
/root_
$DISTRO/boot
323 # could set default like this, but no reason to.
324 # btrfs subvolume set-default \
325 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
326 chattr -Rf +C root_
$DISTRO
329 mount
-o subvolid
=0 $first_boot_dev /mnt
331 btrfs subvolume set-default
0 /mnt
332 [[ ! -e /mnt
/boot_
$DISTRO ]] || btrfs subvolume delete
/mnt
/boot_
$DISTRO
333 btrfs subvolume create boot_
$DISTRO
336 ## end create subvols ##
339 # note, the mount point /a seems to get automatically created somewhere
340 cat > /tmp
/fai
/fstab
<<EOF
341 $first_root_crypt / btrfs noatime,subvol=root_$DISTRO 0 0
342 $first_root_crypt /q btrfs noatime,subvol=q 0 0
343 /q/a /a none bind 0 0
344 $first_boot_dev /boot btrfs noatime,subvol=boot_$DISTRO 0 0
348 # I will avoid using uuid in the future.
349 if ifclass treetowl
; then
350 cat >> /tmp
/fai
/fstab
<<'EOF'
351 $first_root_crypt /i btrfs noatime,subvol=i 0 0
352 UUID=3f7b31cd-f299-40b4-a86b-7604282e2715 /i btrfs noatime 0 2
357 for dev
in ${devs[@]}; do
358 swaps
+=(`swap-cryptname`)
359 cat >>/tmp
/fai
/crypttab
<<EOF
360 `root-cryptname` `rootdev` none keyscript=/root/keyscript,discard,luks
361 `swap-cryptname` `swapdev` /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
363 cat >> /tmp
/fai
/fstab
<<EOF
364 `swap-cryptdev` none swap sw 0 0
369 #BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
371 # swaplist seems to do nothing.
373 cat >/tmp
/fai
/disk_var.sh
<<EOF
374 # ROOT_PARTITIONS is added by me, used in arch setup.
375 ROOT_PARTITIONS="${root_devs[@]}"
376 ROOT_PARTITION=\${ROOT_PARTITION:-$first_root_crypt}
377 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
378 BOOT_DEVICE="${short_devs[@]}"
379 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}