Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by nld3-dev1.alpinelinux.org (Postfix) with ESMTPS id 16ABE780EDD for <~alpine/devel@lists.alpinelinux.org>; Thu, 8 Oct 2020 17:38:12 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpwn.com; s=key1; t=1602178691; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=3KrpsQeFG6l7/RyiJ3F0/emEDKflo/Jr+Lmf/S41hco=; b=lHxclxuf2JH2lUIDZURDcwaTKpt8OYJifVsHeRTy3WYCaRRmi6EFbp34RPWdos9RnJUjSa o+B4/cXkHOOLb/dulflQH6BSO+J1SPFDSK4MXLaN/dXGZmDdFzMWHwS2cq6/canKpsAmru e64bGy3ZDUmGypNiJMya2IxxeGWDHAmjb3jEhtA/PE7Yqbsi+o1j6yMDJmkLorawQROC3C ENgbZ2b6XmNGsm1ZMH3wa4QJ//QfrqWbsRx+VUCIqhxRHP98Go/xhom6brqsYwi/rHjMZN cODIEnk8VXkFVddqPu5xQtD+jQAhLEYc/30jQO8Yv+Sx+crv7ALXi1R8if6fDg== From: Drew DeVault To: ~alpine/devel@lists.alpinelinux.org Cc: Drew DeVault Subject: [PATCH alpine-conf v6] Support encrypted root in setup-disk Date: Thu, 8 Oct 2020 11:38:01 -0600 Message-Id: <20201008173801.4514-1-sir@cmpwn.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Score: 0.00 --- I had prepared the updates following v5 several months ago but apparently never sent it in. setup-disk.in | 115 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 22 deletions(-) diff --git a/setup-disk.in b/setup-disk.in index 7a3a2a3..9390a69 100644 --- a/setup-disk.in +++ b/setup-disk.in @@ -79,6 +79,26 @@ enumerate_fstab() { done } +# given an fstab on stdin, determine if any of the mountpoints are encrypted +crypt_required() { + while read devname mountpoint fstype mntops freq passno; do + if [ -z "$devname" ] || [ "${devname###}" != "$devname" ]; then + continue + fi + uuid="${devname##UUID=}" + if [ "$uuid" != "$devname" ]; then + devname="$(blkid --uuid "$uuid")" + fi + mapname="${devname##/dev/mapper/}" + if [ "$mapname" != "$devname" ]; then + if cryptsetup status "$mapname" >&1 >/dev/null; then + return 0 + fi + fi + done + return 1 +} + is_vmware() { grep -q VMware /proc/scsi/scsi 2>/dev/null \ || grep -q VMware /proc/ide/hd*/model 2>/dev/null @@ -342,7 +362,7 @@ setup_syslinux() { install_mounted_root() { local mnt="$1" shift 1 - local disks="${@}" mnt_boot= boot_fs= root_fs= + local disks="${@}" mnt_boot= boot_fs= root_fs= use_crypt= local initfs_features="ata base ide scsi usb virtio" local pvs= dev= rootdev= bootdev= extlinux_raidopt= root= modules= local kernel_opts="quiet" @@ -401,7 +421,6 @@ install_mounted_root() { esac done - if [ -n "$VERBOSE" ]; then echo "Root device: $rootdev" echo "Root filesystem: $root_fs" @@ -424,6 +443,27 @@ install_mounted_root() { # we should not try start modloop on sys install rm -f "$mnt"/etc/runlevels/*/modloop + # generate the fstab + if [ -f "$mnt"/etc/fstab ]; then + mv "$mnt"/etc/fstab "$mnt"/etc/fstab.old + fi + enumerate_fstab "$mnt" >> "$mnt"/etc/fstab + if [ -n "$SWAP_DEVICES" ]; then + local swap_dev + for swap_dev in $SWAP_DEVICES; do + echo -e "$(uuid_or_device ${swap_dev})\tswap\tswap\tdefaults\t0 0" \ + >> "$mnt"/etc/fstab + done + fi + cat >>"$mnt"/etc/fstab <<-__EOF__ + /dev/cdrom /media/cdrom iso9660 noauto,ro 0 0 + /dev/usbdisk /media/usb vfat noauto 0 0 + __EOF__ + + if crypt_required <"$mnt"/etc/fstab; then + use_crypt=1 + fi + # generate mkinitfs.conf mkdir -p "$mnt"/etc/mkinitfs/features.d echo "features=\"$initfs_features\"" > "$mnt"/etc/mkinitfs/mkinitfs.conf @@ -441,24 +481,15 @@ install_mounted_root() { if [ -n "$(get_bootopt nomodeset)" ]; then kernel_opts="nomodeset $kernel_opts" fi + if [ "$use_crypt" ] && cryptsetup status "$rootdev" 2>&1 >/dev/null; then + # Boot to encrypted root + initfs_features="${initfs_features% cryptsetup} cryptsetup" + root=$(cryptsetup status "$rootdev" | awk '/device:/ { print $2 }') + kernel_opts="cryptroot=$root cryptdm=root" + root=/dev/mapper/root + fi modules="sd-mod,usb-storage,${root_fs}${raidmod}" - # generate the fstab - if [ -f "$mnt"/etc/fstab ]; then - mv "$mnt"/etc/fstab "$mnt"/etc/fstab.old - fi - enumerate_fstab "$mnt" >> "$mnt"/etc/fstab - if [ -n "$SWAP_DEVICES" ]; then - local swap_dev - for swap_dev in $SWAP_DEVICES; do - echo -e "$(uuid_or_device ${swap_dev})\tswap\tswap\tdefaults\t0 0" \ - >> "$mnt"/etc/fstab - done - fi - cat >>"$mnt"/etc/fstab <<-__EOF__ - /dev/cdrom /media/cdrom iso9660 noauto,ro 0 0 - /dev/usbdisk /media/usb vfat noauto 0 0 - __EOF__ # remove the installed db in case its there so we force re-install rm -f "$mnt"/var/lib/apk/installed "$mnt"/lib/apk/db/installed echo "Installing system on $rootdev:" @@ -502,6 +533,10 @@ unmount_partitions() { # unmount the partitions umount $(awk '{print $2}' /proc/mounts | egrep "^$mnt(/|\$)" | sort -r) + + if [ "$USE_CRYPT" ]; then + cryptsetup close /dev/mapper/root + fi } # figure out decent default swap size in mega bytes @@ -993,6 +1028,18 @@ native_disk_install_lvm() { setup_root $root_dev $BOOT_DEV } +setup_crypt() { + local dev="$1" local dmname="$2" + mkdir -p /run/cryptsetup + echo "Preparing root partition for encryption." >&2 + echo "You will be prompted for your password at boot." >&2 + echo "If you forget your password, your data will be lost." >&2 + cryptsetup luksFormat --type luks2 "$dev" >&2 + echo "Enter password again to unlock disk for installation." >&2 + cryptsetup open "$dev" "$dmname" >&2 + echo "/dev/mapper/$dmname" +} + native_disk_install() { local prep_part_type=$(partition_id prep) local root_part_type=$(partition_id linux) @@ -1064,6 +1111,10 @@ native_disk_install() { root_dev=$(find_nth_non_boot_parts $index "$root_part_type" $@) fi + if [ "$USE_CRYPT" ]; then + root_dev=$(setup_crypt $root_dev root) + fi + [ $SWAP_SIZE -gt 0 ] && setup_swap_dev $swap_dev setup_root $root_dev $BOOT_DEV $@ } @@ -1084,7 +1135,7 @@ diskselect_help() { diskmode_help() { cat <<-__EOF__ - You can select between 'sys', 'data', 'lvm', 'lvmsys' or 'lvmdata'. + You can select between 'sys', 'cryptsys', 'data', 'lvm', 'lvmsys' or 'lvmdata'. sys: This mode is a traditional disk install. The following partitions will be @@ -1092,6 +1143,12 @@ diskmode_help() { This mode may be used for development boxes, desktops, virtual servers, etc. + cryptsys: + This mode is equivalent to sys, except that the root filesystem will be + encrypted with cryptsetup. You will be prompted to enter a decryption + password, and will need to use this password to boot up the operating + system after installation. + data: This mode uses your disk(s) for data storage, not for the operating system. The system itself will run from tmpfs (RAM). @@ -1142,7 +1199,7 @@ ask_disk() { usage() { cat <<-__EOF__ - usage: setup-disk [-hLqrv] [-k kernelflavor] [-m MODE] [-o apkovl] [-s SWAPSIZE] + usage: setup-disk [-hLqrve] [-k kernelflavor] [-m MODE] [-o apkovl] [-s SWAPSIZE] [MOUNTPOINT | DISKDEV...] Install alpine on harddisk. @@ -1156,6 +1213,7 @@ usage() { options: -h Show this help + -e Encrypt disk -m Use disk for MODE without asking, where MODE is either 'data' or 'sys' -o Restore system from given apkovl file -k Use kernelflavor instead of $KERNEL_FLAVOR @@ -1192,11 +1250,13 @@ case $kver in *) KERNEL_FLAVOR=vanilla;; esac +USE_CRYPT= DISK_MODE= USE_LVM= # Parse args -while getopts "hk:Lm:o:qrs:v" opt; do +while getopts "hek:Lm:o:qrs:v" opt; do case $opt in + e) USE_CRYPT=1;; m) DISK_MODE="$OPTARG";; k) KERNEL_FLAVOR="$OPTARG";; L) USE_LVM="_lvm";; @@ -1221,6 +1281,12 @@ fi reset_var swapoff -a +if [ $USE_CRYPT -eq 1 ] && [ -n "$USE_LVM" ]; then + echo "cryptsys and lvm are currently mutually incompatible." + echo "Please run $0 again with only one of these options selected." + exit 1 +fi + # stop all volume groups in use vgchange --ignorelockingfailure -a n >/dev/null 2>&1 @@ -1274,10 +1340,15 @@ if [ -n "$diskdevs" ] && [ -z "$DISK_MODE" ]; then echo "The following $disk_is_or_disks_are selected${USE_LVM:+ (with LVM)}:" show_disk_info $diskdevs _lvm=${USE_LVM:-", 'lvm'"} - echon "How would you like to use $it_them? ('sys', 'data'${_lvm#_lvm} or '?' for help) [?] " + echon "How would you like to use $it_them? ('sys', 'cryptsys', 'data'${_lvm#_lvm} or '?' for help) [?] " default_read answer '?' case "$answer" in '?') diskmode_help;; + cryptsys) + answer=sys + USE_CRYPT=1 + break + ;; sys|data) break;; lvm) USE_LVM="_lvm" ;; nolvm) USE_LVM="";; -- 2.28.0