From 990b7c8a383823fbcde67c30a6b32d63119cad32 Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Tue, 15 Feb 2022 16:16:22 +0000 Subject: [PATCH] Generate the default swapfile on first boot Changes in either livecd-rootfs or ubuntu-image seem to periodically break the transfer of the pre-allocated swapfile (copying it in such a fashion that it winds up "with holes" and thus unable to be used as a swapfile). Rather than fight this, just use a simple systemd service to generate the swapfile if it doesn't exist (using fallocate to keep things snappy). --- .../099-ubuntu-image-customization.chroot | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/live-build/ubuntu/hooks/099-ubuntu-image-customization.chroot b/live-build/ubuntu/hooks/099-ubuntu-image-customization.chroot index 526f013e..b4028ceb 100644 --- a/live-build/ubuntu/hooks/099-ubuntu-image-customization.chroot +++ b/live-build/ubuntu/hooks/099-ubuntu-image-customization.chroot @@ -20,11 +20,35 @@ if [ "$IMAGEFORMAT" == "none" ]; then # Make the writable partition grow echo "LABEL=writable / ext4 defaults,x-systemd.growfs 0 0" >>/etc/fstab - # Create a 1GB swapfile - dd if=/dev/zero of=/swapfile bs=1G count=1 - chmod 0600 /swapfile - mkswap /swapfile - echo "/swapfile none swap sw 0 0" >>/etc/fstab + # Add units for a 1GiB swapfile, generated on first boot + cat << EOF > /lib/systemd/system/mkswap.service +[Unit] +Description=Create the default swapfile +DefaultDependencies=no +Requires=local-fs.target +After=local-fs.target +Before=swapfile.swap +ConditionPathExists=!/swapfile + +[Service] +Type=oneshot +ExecStartPre=fallocate -l 1GiB /swapfile +ExecStartPre=chmod 600 /swapfile +ExecStart=mkswap /swapfile + +[Install] +WantedBy=swap.target +EOF + cat << EOF > /lib/systemd/system/swapfile.swap +[Unit] +Description=The default swapfile + +[Swap] +What=/swapfile +EOF + mkdir -p /lib/systemd/system/swap.target.wants + ln -s ../mkswap.service /lib/systemd/system/swap.target.wants/ + ln -s ../swapfile.swap /lib/systemd/system/swap.target.wants/ fi fi