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).
add-buildd-aarch64
Dave Jones 3 years ago
parent 701eb38bf0
commit 990b7c8a38

@ -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

Loading…
Cancel
Save