From 7186daeb1c79076ddaaeb32e6c165dd5e6c620a2 Mon Sep 17 00:00:00 2001 From: Disassembler Date: Wed, 12 Jun 2019 16:27:49 +0200 Subject: [PATCH] Use only UUIDs for runtime disk management --- apk/vmmgr | 2 +- vm.sh | 14 ++++++++------ vm/boot/{extlinux.conf => extlinux.conf.old} | 2 +- vm/sbin/extend-disk | 12 +++++++++--- 4 files changed, 19 insertions(+), 11 deletions(-) rename vm/boot/{extlinux.conf => extlinux.conf.old} (67%) diff --git a/apk/vmmgr b/apk/vmmgr index 54d3c1d..d9334fd 160000 --- a/apk/vmmgr +++ b/apk/vmmgr @@ -1 +1 @@ -Subproject commit 54d3c1d23b8994eeeac8348c6e820aaf3a56cc7b +Subproject commit d9334fd12be8feb11106564d1a3b2e7526c89f43 diff --git a/vm.sh b/vm.sh index 5b4cdaa..4fb497e 100755 --- a/vm.sh +++ b/vm.sh @@ -27,7 +27,7 @@ n p 1 -+100m ++50m a 1 n @@ -63,20 +63,21 @@ mount -t ext4 /dev/sda1 /mnt/boot setup-disk -m sys /mnt # Update boot-time volume information -BOOT_UUID=$(blkid /dev/sda1 | cut -d' ' -f2 | tr -d '"') +BOOT_UUID=$(blkid -o value /dev/sda1 | head -1) +CRYPT_UUID=$(blkid -o value /dev/sda2 | head -1) cat </mnt/etc/fstab /dev/vg0/root / ext4 rw,noatime,data=ordered 0 1 -${BOOT_UUID} /boot ext4 rw,noatime,data=ordered 0 2 +UUID=${BOOT_UUID} /boot ext4 rw,noatime,data=ordered 0 2 /dev/vg0/swap swap swap defaults 0 0 EOF -echo "system /dev/sda2 none luks" >/mnt/etc/crypttab +echo "system UUID=${CRYPT_UUID} none luks" >/mnt/etc/crypttab # Rebuild initfs sed -i 's/lvm/lvm cryptsetup/' /mnt/etc/mkinitfs/mkinitfs.conf mkinitfs -c /mnt/etc/mkinitfs/mkinitfs.conf -b /mnt $(ls /mnt/lib/modules) # Update extlinux (ignore the errors) -sed -i 's/rootfstype=ext4/rootfstype=ext4 cryptroot=\/dev\/sda2 cryptdm=system/' /mnt/etc/update-extlinux.conf +sed -i "s/rootfstype=ext4/rootfstype=ext4 cryptroot=UUID=${CRYPT_UUID} cryptdm=system/" /mnt/etc/update-extlinux.conf chroot /mnt update-extlinux sed -i 's/overwrite=1/overwrite=0/' /mnt/etc/update-extlinux.conf @@ -86,13 +87,14 @@ chroot /mnt setup-timezone -z Europe/Prague # Install basic system apk --no-cache add apache2-utils gettext wget https://repo.spotter.cz/vm.tar -O - | tar xf - -C /mnt +envsubst /boot/extlinux.conf chroot /mnt apk --no-cache add bridge ca-certificates curl e2fsprogs-extra gettext iptables kbd-misc libressl lxc postfix nginx openssh-server openssh-sftp-server util-linux wireguard-virt@vm wireguard-tools-wg@vm acme-sh@vm vmmgr@vm chroot /mnt newaliases mkdir -p /mnt/var/log/lxc for SERVICE in cgroups consolefont crond iptables networking nginx ntpd postfix swap urandom vmmgr; do ln -s /etc/init.d/${SERVICE} /mnt/etc/runlevels/boot done -ADMINPWD=$(htpasswd -bnBC 10 "" "${ENCPWD}" | tr -d ':\n' | sed 's/$2y/$2b/') envsubst /mnt/etc/vmmgr/config.json +ADMINPWD=$(htpasswd -bnBC 10 '' "${ENCPWD}" | tr -d ':\n' | sed 's/$2y/$2b/') envsubst /mnt/etc/vmmgr/config.json # Cleanup rm -rf /mnt/root diff --git a/vm/boot/extlinux.conf b/vm/boot/extlinux.conf.old similarity index 67% rename from vm/boot/extlinux.conf rename to vm/boot/extlinux.conf.old index aee30ca..228732b 100644 --- a/vm/boot/extlinux.conf +++ b/vm/boot/extlinux.conf.old @@ -6,4 +6,4 @@ DISPLAY vm.txt LABEL vm LINUX vmlinuz-virt INITRD initramfs-virt - APPEND elevator=noop root=/dev/vg0/root modules=sd-mod,usb-storage,ext4 nomodeset quiet rootfstype=ext4 cryptroot=/dev/sda2 cryptdm=system + APPEND elevator=noop root=/dev/vg0/root modules=sd-mod,usb-storage,ext4 nomodeset quiet rootfstype=ext4 cryptroot=UUID=${CRYPT_UUID} cryptdm=system diff --git a/vm/sbin/extend-disk b/vm/sbin/extend-disk index 71ca824..d0bd841 100755 --- a/vm/sbin/extend-disk +++ b/vm/sbin/extend-disk @@ -1,13 +1,19 @@ #!/bin/sh set -e +# Determine partition and hard drive paths +UUID=$(/usr/bin/awk '{print substr($2,6)}' /etc/crypttab) +PART=$(/sbin/blkid -U ${UUID}) +DISK=${PART%?} +DEV=$(/usr/bin/basename ${DISK}) + # No resizing with less than 10k unused blocks -BLOCKS_FREE=$(/usr/bin/awk '/sda$/ {blocks = $3} /sda\d/ {blocks -= $3} END {print blocks}' /proc/partitions) +BLOCKS_FREE=$(/usr/bin/awk -v dev="${DEV}" '{if ($0 ~ dev "$") blocks = $3} {if ($0 ~ dev "[0-9]") blocks -= $3} END {print blocks}' /proc/partitions) [ ${BLOCKS_FREE} -lt 10240 ] && exit 0 # Resize physical partition # Force busybox fdisk as util-linux fdisk breaks subsequent partx command -cat <