Add script for automatic disk extension + swap creation

This commit is contained in:
Disassembler 2018-09-01 15:16:22 +02:00
parent 58622fa5b6
commit c6c50bb3c5
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499
4 changed files with 43 additions and 5 deletions

View File

@ -46,13 +46,11 @@ echo -n 'password' | cryptsetup open --type luks /dev/sda2 system
# Set up LVM
pvcreate /dev/mapper/system
vgcreate vg0 /dev/mapper/system
lvcreate -L 4G vg0 -n swap
lvcreate -l 100%FREE vg0 -n root
# Format
mkfs.ext4 -m0 /dev/sda1
mkfs.ext4 -m1 /dev/vg0/root
mkswap /dev/vg0/swap
# Mount
mount -t ext4 /dev/vg0/root /mnt
@ -97,7 +95,6 @@ dd bs=440 count=1 conv=notrunc if=/mnt/usr/share/syslinux/mbr.bin of=/dev/sda
# Unmount and shut down
umount /mnt/boot
umount /mnt
swapoff -a
vgchange -a n
cryptsetup luksClose system
poweroff

View File

@ -5,7 +5,7 @@ SOURCE_DIR=$(realpath $(dirname "${0}"))/basic
# Install packages
apk --no-cache add --virtual .useful git file htop less openssh-server openssh-sftp-server
apk --no-cache add curl docker gettext kbd-misc libressl python3 py3-bcrypt py3-cffi py3-dnspython py3-jinja2 py3-requests py3-six py3-werkzeug nginx
apk --no-cache add curl docker e2fsprogs-extra gettext kbd-misc libressl python3 py3-bcrypt py3-cffi py3-dnspython py3-jinja2 py3-requests py3-six py3-werkzeug nginx util-linux
# Copy profile files and settings
mkdir -p /root/.config/htop /root/.ssh
@ -17,6 +17,7 @@ cp ${SOURCE_DIR}/root/.config/htop/htoprc /root/.config/htop/htoprc
cp ${SOURCE_DIR}/boot/extlinux.conf /boot/extlinux.conf
cp ${SOURCE_DIR}/boot/spotter.txt /boot/spotter.txt
cp ${SOURCE_DIR}/etc/inittab /etc/inittab
cp ${SOURCE_DIR}/sbin/extend-disk /sbin/extend-disk
>/etc/motd
# Enable support for Czech characters
@ -47,7 +48,7 @@ cp -r ${SOURCE_DIR}/srv/spotter /srv/spotter
ln -s /srv/spotter/cli.py /usr/bin/spotter-appmgr
# Configure services
for SERVICE in consolefont crond nginx ntpd sshd spotter-appmgr; do
for SERVICE in consolefont crond nginx ntpd sshd spotter-appmgr swap; do
rc-update add ${SERVICE} boot
service ${SERVICE} start
done

View File

@ -2,6 +2,7 @@
::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

39
basic/sbin/extend-disk Executable file
View File

@ -0,0 +1,39 @@
#!/bin/sh
set -e
# 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} -lt 10240 ] && exit 0
# Resize physical partition
# Force busybox fdisk as util-linux fdisk breaks subsequent partx command
cat <<EOF | /bin/busybox fdisk /dev/sda || /bin/true
d
2
n
p
2
t
2
8e
w
EOF
# Re-read partition table
/usr/sbin/partx -u /dev/sda2
# Resize dmcrypt and LVM PV
/sbin/cryptsetup resize system
/sbin/pvresize /dev/mapper/system
# 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
fi
# Extend LV and underlying filesystem
/sbin/lvextend -l +100%FREE vg0/root
/usr/sbin/resize2fs /dev/vg0/root