From ad437292ab296409cab8d46316de1af5f367867d Mon Sep 17 00:00:00 2001 From: Disassembler Date: Sat, 11 Apr 2020 14:55:18 +0200 Subject: [PATCH] Rewrite extend-disk in python and move vmtty back to basic vm setup --- apk/vmmgr | 2 +- vm/etc/inittab | 1 - vm/sbin/extend-disk | 117 ++++++++++++++++++++++++++++++-------------- vm/sbin/vmtty | 17 +++++++ 4 files changed, 97 insertions(+), 40 deletions(-) create mode 100755 vm/sbin/vmtty diff --git a/apk/vmmgr b/apk/vmmgr index 7c9ed8c..ff9a12a 160000 --- a/apk/vmmgr +++ b/apk/vmmgr @@ -1 +1 @@ -Subproject commit 7c9ed8c17a1ef9b403fbf899915ed2f16bf67459 +Subproject commit ff9a12af65ad6e0a036a74006c7d85578d4be746 diff --git a/vm/etc/inittab b/vm/etc/inittab index 811da22..1547c8d 100644 --- a/vm/etc/inittab +++ b/vm/etc/inittab @@ -2,7 +2,6 @@ ::sysinit:/sbin/openrc sysinit >/dev/null 2>&1 ::sysinit:/sbin/openrc boot >/dev/null 2>&1 -::wait:/sbin/extend-disk >/dev/null 2>&1 ::wait:/sbin/openrc default >/dev/null 2>&1 # Set up getty diff --git a/vm/sbin/extend-disk b/vm/sbin/extend-disk index d0bd841..750693c 100755 --- a/vm/sbin/extend-disk +++ b/vm/sbin/extend-disk @@ -1,46 +1,87 @@ -#!/bin/sh -set -e +#!/usr/bin/python3 +# -*- coding: utf-8 -*- -# 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}) +import os +import subprocess -# No resizing with less than 10k unused blocks -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 +message_printed = False -# Resize physical partition -# Force busybox fdisk as util-linux fdisk breaks subsequent partx command -cat < 10: + print_message() + try: + # Force busybox fdisk as util-linux fdisk breaks the subsequent partx command by notifying kernel too hard + input = 'd\n2\nn\np\n2\n\n\nt\n2\n8e\nw'.encode() + subprocess.run(['/bin/busybox', 'fdisk', disk], input=input, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) + except subprocess.CalledProcessError as e: + # Returns code 1 because of the nofication failure, but the resize is successful anyway + if e.returncode != 1: + raise + # Re-read partition (partx is from util-linux, issues BLKPG_RESIZE_PARTITION ioctl to update the kernel partition table) + subprocess.run(['/usr/sbin/partx', '-u', partition], stdout=subprocess.DEVNULL, check=True) + sizes = get_sizes() -# Resize dmcrypt and LVM PV -/sbin/cryptsetup resize system -/sbin/pvresize /dev/mapper/system +# Resize dmcrypt when there is more than 10M of free space (16M is consumed by dmcrypt) +free_space = sizes[partition] - sizes['/dev/mapper/system'] - 16 +if free_space > 10: + print_message() + # Create locking dir for cryptsetup to suppress warning + try: + os.mkdir('/run/cryptsetup', 0o700) + except FileExistsError: + pass + # Ask for password 3 times, then fail + fails = 0 + while True: + try: + subprocess.run(['/sbin/cryptsetup', 'resize', 'system'], check=True) + break + except (subprocess.CalledProcessError, KeyboardInterrupt): + fails += 1 + if fails == 3: + raise + sizes = get_sizes() -# Create swap if it doesn't exist -if [ ! -e /dev/vg0/swap ]; then - /sbin/lvcreate -L 4G vg0 -n swap - /sbin/mkswap /dev/vg0/swap - /sbin/swapon /dev/vg0/swap -fi - -# Extend LV and underlying filesystem -/sbin/lvextend -l +100%FREE vg0/root -/usr/sbin/resize2fs /dev/vg0/root +# Resize LVM PV when there is more than 10M of free space +free_space = sizes['/dev/mapper/system'] - sum(size for part,size in sizes.items() if part.startswith('/dev/mapper/vg0')) +if free_space > 10: + print_message() + subprocess.run(['/sbin/pvresize', '/dev/mapper/system'], stdout=subprocess.DEVNULL, check=True) +# Create swap if it doesn't exist yet and there's at least 4 GB of free space +if '/dev/mapper/vg0-swap' not in sizes and free_space > 4*1024: + subprocess.run(['/sbin/lvcreate', '-L', '4G', 'vg0', '-n', 'swap'], stdout=subprocess.DEVNULL, check=True) + subprocess.run(['/sbin/mkswap', '/dev/mapper/vg0-swap'], stdout=subprocess.DEVNULL, check=True) + subprocess.run(['/sbin/swapon', '/dev/mapper/vg0-swap'], stdout=subprocess.DEVNULL, check=True) + sizes = get_sizes() + free_space = sizes['/dev/mapper/system'] - sum(size for part,size in sizes.items() if part.startswith('/dev/mapper/vg0')) +# Resize LVM LV when there is more than 10M of free space +if free_space > 10: + subprocess.run(['/sbin/lvextend', '-l', '+100%FREE', '/dev/mapper/vg0-root'], stdout=subprocess.DEVNULL, check=True) + subprocess.run(['/usr/sbin/resize2fs', '/dev/mapper/vg0-root'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) diff --git a/vm/sbin/vmtty b/vm/sbin/vmtty new file mode 100755 index 0000000..a75d314 --- /dev/null +++ b/vm/sbin/vmtty @@ -0,0 +1,17 @@ +#!/bin/sh + +# Check if virtual disk has been resized and partitions needs extending +if [ -x /sbin/extend-disk ]; then + /sbin/extend-disk +fi + +# Rebuild /etc/issue +if [ -x /usr/bin/vmmgr ]; then + /usr/bin/vmmgr rebuild-issue +fi + +# Print /etc/issue +/bin/cat /etc/issue + +# Wait for key press +read a