Skip to content

Instantly share code, notes, and snippets.

@tildelowengrimm
Created January 18, 2017 02:35
Show Gist options
  • Save tildelowengrimm/b34c10e5e2b049be472615ffabb95d03 to your computer and use it in GitHub Desktop.
Save tildelowengrimm/b34c10e5e2b049be472615ffabb95d03 to your computer and use it in GitHub Desktop.
#!/bin/bash
# A script to increase the amount of swap space available in VMs running
# Qubes' default kernel. VMs running non-default kernels will be allocated
# volatile images which are un-necessarily large, but will otherwise run fine.
#
# Why not increase the swap allocated for all kernels? If something goes
# horribly wrong, it would be nice to be able to use an older kernel version
# temporarily while fixing whatever went wrong.
#
# Future work:
# * run automatically whenever there's a new default kernel after a dnf run
# * test by attempting to start a canary VM and log, roll back on failure
# * defensively validate the output of changes before applying
# * ensure that there's enough space to run
# * check that new/old initramfs sizes are similar
# * check that rezipped initramfs extracts to same source
# * validate base swap number, or just set target swap size
# * create new faux kernel version and apply to select VMs
BASE_SWAP="1024"
SWAP_MULTIPLIER="8"
SWAP_SIZE="$[ $SWAP_MULTIPLIER * $BASE_SWAP]"
MYDIR="/tmp/swap-expand"
KERNEL_BASE="/var/lib/qubes/vm-kernels"
PREPARE_VOLATILE="/usr/lib/qubes/prepare-volatile-img.sh"
#KERNEL="$(ls $KERNEL_BASE | sort -ns | tail -n 1)"
KERNEL="$(qubes-prefs | grep kernel | cut --delimiter=":" -f 2- | cut -c 2-)"
## Increase the amount of space set aside for swap when preparing volatile images.
if ! [ -e "$PREPARE_VOLATILE".orig ] ; then
sudo cp "$PREPARE_VOLATILE" "$PREPARE_VOLATILE".orig
fi
sudo sed -i "s/SWAP_SIZE=\$\[ 1024 \]/SWAP_SIZE=\$\[ ""$SWAP_SIZE"" \]/" $PREPARE_VOLATILE
## Increase the amount of swap allocated in the kernel intramfs.
if ! [ -e "$KERNEL_BASE"/"$KERNEL"/initramfs.orig ] ; then
sudo cp "$KERNEL_BASE"/"$KERNEL"/initramfs "$KERNEL_BASE"/"$KERNEL"/initramfs.orig
fi
rm -rf "$MYDIR"
mkdir "$MYDIR"/
mkdir "$MYDIR"/"$KERNEL"/
mkdir "$MYDIR"/"$KERNEL"/initramfs
cd "$MYDIR"/"$KERNEL"/initramfs
gunzip -q -c "$KERNEL_BASE"/"$KERNEL"/initramfs | cpio --quiet -i
sed -i "s/SWAP_SIZE=\$(( 1024 \* 1024 \* 2 )) # sectors, 1GB/SWAP_SIZE=\$(( 1024 \* 1024 \* 2 * ""$SWAP_MULTIPLIER"" )) # sectors, ""$SWAP_MULTIPLIER""GB/" init
find . | cpio --quiet -H newc -o | gzip -q -9 | sudo tee "$KERNEL_BASE"/"$KERNEL"/initramfs.new > /dev/null
sudo mv "$KERNEL_BASE"/"$KERNEL"/initramfs "$KERNEL_BASE"/"$KERNEL"/initramfs.old
sudo cp "$KERNEL_BASE"/"$KERNEL"/initramfs.new "$KERNEL_BASE"/"$KERNEL"/initramfs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment