Rewrite extend-disk in python and move vmtty back to basic vm setup
This commit is contained in:
parent
bd946bf564
commit
ad437292ab
@ -1 +1 @@
|
|||||||
Subproject commit 7c9ed8c17a1ef9b403fbf899915ed2f16bf67459
|
Subproject commit ff9a12af65ad6e0a036a74006c7d85578d4be746
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
::sysinit:/sbin/openrc sysinit >/dev/null 2>&1
|
::sysinit:/sbin/openrc sysinit >/dev/null 2>&1
|
||||||
::sysinit:/sbin/openrc boot >/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
|
::wait:/sbin/openrc default >/dev/null 2>&1
|
||||||
|
|
||||||
# Set up getty
|
# Set up getty
|
||||||
|
@ -1,46 +1,87 @@
|
|||||||
#!/bin/sh
|
#!/usr/bin/python3
|
||||||
set -e
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Determine partition and hard drive paths
|
import os
|
||||||
UUID=$(/usr/bin/awk '{print substr($2,6)}' /etc/crypttab)
|
import subprocess
|
||||||
PART=$(/sbin/blkid -U ${UUID})
|
|
||||||
DISK=${PART%?}
|
|
||||||
DEV=$(/usr/bin/basename ${DISK})
|
|
||||||
|
|
||||||
# No resizing with less than 10k unused blocks
|
message_printed = False
|
||||||
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
|
def print_message():
|
||||||
# Force busybox fdisk as util-linux fdisk breaks subsequent partx command
|
# Print user-friendly message
|
||||||
cat <<EOF | /bin/busybox fdisk ${DISK} || /bin/true
|
global message_printed
|
||||||
d
|
if not message_printed:
|
||||||
2
|
print()
|
||||||
n
|
print('Bylo detekováno zvětšení virtuálního disku.')
|
||||||
p
|
print('Systém nyní upraví nastavení diskových oddílů.')
|
||||||
2
|
print()
|
||||||
|
message_printed = True
|
||||||
|
|
||||||
|
def get_sizes():
|
||||||
|
# Returns device sizes in MB
|
||||||
|
sizes = {}
|
||||||
|
lsblk = subprocess.run(['/bin/lsblk', '-bnro', 'PATH,SIZE'], capture_output=True, check=True).stdout.decode()
|
||||||
|
for line in lsblk.splitlines():
|
||||||
|
line = line.split()
|
||||||
|
sizes[line[0]] = int(line[1])//1024**2
|
||||||
|
return sizes
|
||||||
|
|
||||||
t
|
# Determine partition and disk paths
|
||||||
2
|
with open('/etc/crypttab') as f:
|
||||||
8e
|
crypttab = dict(line.split()[:2] for line in f.read().splitlines())
|
||||||
w
|
partition = subprocess.run(['/sbin/findfs', crypttab['system']], capture_output=True, check=True).stdout.decode().strip()
|
||||||
EOF
|
disk = partition.rstrip('0123456789')
|
||||||
|
|
||||||
# Re-read partition table
|
# Resize physical partition when there is more than 10M unallocated
|
||||||
/usr/sbin/partx -u ${PART}
|
sizes = get_sizes()
|
||||||
|
free_space = sizes[disk] - sum([size for part,size in sizes.items() if part.startswith(disk) and part[len(disk):].isdigit()])
|
||||||
|
if free_space > 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
|
# Resize dmcrypt when there is more than 10M of free space (16M is consumed by dmcrypt)
|
||||||
/sbin/cryptsetup resize system
|
free_space = sizes[partition] - sizes['/dev/mapper/system'] - 16
|
||||||
/sbin/pvresize /dev/mapper/system
|
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
|
# Resize LVM PV when there is more than 10M of free space
|
||||||
if [ ! -e /dev/vg0/swap ]; then
|
free_space = sizes['/dev/mapper/system'] - sum(size for part,size in sizes.items() if part.startswith('/dev/mapper/vg0'))
|
||||||
/sbin/lvcreate -L 4G vg0 -n swap
|
if free_space > 10:
|
||||||
/sbin/mkswap /dev/vg0/swap
|
print_message()
|
||||||
/sbin/swapon /dev/vg0/swap
|
subprocess.run(['/sbin/pvresize', '/dev/mapper/system'], stdout=subprocess.DEVNULL, check=True)
|
||||||
fi
|
# 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:
|
||||||
# Extend LV and underlying filesystem
|
subprocess.run(['/sbin/lvcreate', '-L', '4G', 'vg0', '-n', 'swap'], stdout=subprocess.DEVNULL, check=True)
|
||||||
/sbin/lvextend -l +100%FREE vg0/root
|
subprocess.run(['/sbin/mkswap', '/dev/mapper/vg0-swap'], stdout=subprocess.DEVNULL, check=True)
|
||||||
/usr/sbin/resize2fs /dev/vg0/root
|
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)
|
||||||
|
17
vm/sbin/vmtty
Executable file
17
vm/sbin/vmtty
Executable file
@ -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
|
Loading…
Reference in New Issue
Block a user