From 32396af448d79d100daf0d5c9962a76f3bf16825 Mon Sep 17 00:00:00 2001 From: Eero Kelly Date: Fri, 22 Nov 2024 19:23:55 +0000 Subject: [PATCH] Add swap to GuestOS --- .../components/early-boot/fstab/fstab-guestos | 1 + ic-os/components/guestos.bzl | 2 ++ .../init/setup-lvs/guestos/setup-lvs.sh | 15 +++++++++++++ .../setup-shared-swap.service | 22 +++++++++++++++++++ .../setup-shared-swap.sh | 7 ++++++ 5 files changed, 47 insertions(+) create mode 100644 ic-os/components/upgrade/shared-resources/setup-shared-resources/setup-shared-swap.service create mode 100755 ic-os/components/upgrade/shared-resources/setup-shared-resources/setup-shared-swap.sh diff --git a/ic-os/components/early-boot/fstab/fstab-guestos b/ic-os/components/early-boot/fstab/fstab-guestos index b188c4cb741..890c8bea875 100644 --- a/ic-os/components/early-boot/fstab/fstab-guestos +++ b/ic-os/components/early-boot/fstab/fstab-guestos @@ -7,3 +7,4 @@ tmpfs /tmp tmpfs defaults 0 2 /dev/mapper/store-shared--backup /var/lib/ic/backup ext4 defaults,context=system_u:object_r:ic_data_t:s0 0 2 /dev/mapper/store-shared--crypto /var/lib/ic/crypto ext4 defaults 0 2 /dev/mapper/store-shared--data /var/lib/ic/data xfs defaults 0 2 +/dev/mapper/store-shared--swap none swap defaults 0 2 diff --git a/ic-os/components/guestos.bzl b/ic-os/components/guestos.bzl index d8b1f00a1de..642b5022260 100644 --- a/ic-os/components/guestos.bzl +++ b/ic-os/components/guestos.bzl @@ -152,6 +152,8 @@ component_files = { Label("upgrade/shared-resources/setup-shared-resources/setup-shared-crypto.service"): "/etc/systemd/system/setup-shared-crypto.service", Label("upgrade/shared-resources/setup-shared-resources/setup-shared-data.sh"): "/opt/ic/bin/setup-shared-data.sh", Label("upgrade/shared-resources/setup-shared-resources/setup-shared-data.service"): "/etc/systemd/system/setup-shared-data.service", + Label("upgrade/shared-resources/setup-shared-resources/setup-shared-swap.sh"): "/opt/ic/bin/setup-shared-swap.sh", + Label("upgrade/shared-resources/setup-shared-resources/setup-shared-swap.service"): "/etc/systemd/system/setup-shared-swap.service", Label("upgrade/systemd-generators/guestos/mount-generator"): "/etc/systemd/system-generators/mount-generator", Label("upgrade/systemd-generators/systemd-gpt-auto-generator"): "/etc/systemd/system-generators/systemd-gpt-auto-generator", Label("upgrade/manageboot/manageboot.sh"): "/opt/ic/bin/manageboot.sh", diff --git a/ic-os/components/init/setup-lvs/guestos/setup-lvs.sh b/ic-os/components/init/setup-lvs/guestos/setup-lvs.sh index a3cce4ae593..8df6487dd33 100755 --- a/ic-os/components/init/setup-lvs/guestos/setup-lvs.sh +++ b/ic-os/components/init/setup-lvs/guestos/setup-lvs.sh @@ -65,3 +65,18 @@ lvs /dev/store/shared-backup >/dev/null 2>&1 || ( fi retry lvcreate --yes -L "$LV_SIZE"M -n shared-backup store ) + +# Set up swap space if it does not exist yet. +lvs /dev/store/shared-swap >/dev/null 2>&1 || ( + echo "Logical volume 'shared-swap' does not exist yet (first boot?), creating it." + TOTAL_SIZE=$(($(blockdev --getsz /dev/mapper/vda10-crypt) * 512)) + # Limit to 64G or 14% of capacity, whichever is lower. + # Note: The odd percentage makes calculation easier, and allows for a + # smaller swap when testing. + LV_SIZE=$(("$TOTAL_SIZE" / 7 / 1024 / 1024)) + LV_SIZE_LIMIT=64000 + if [ "${LV_SIZE}" -gt "${LV_SIZE_LIMIT}" ]; then + LV_SIZE="${LV_SIZE_LIMIT}" + fi + retry lvcreate --yes -L "$LV_SIZE"M -n shared-swap store +) diff --git a/ic-os/components/upgrade/shared-resources/setup-shared-resources/setup-shared-swap.service b/ic-os/components/upgrade/shared-resources/setup-shared-resources/setup-shared-swap.service new file mode 100644 index 00000000000..efed4860284 --- /dev/null +++ b/ic-os/components/upgrade/shared-resources/setup-shared-resources/setup-shared-swap.service @@ -0,0 +1,22 @@ +[Unit] +Description=Setup shared-swap space +DefaultDependencies=no +Requires=dev-mapper-store\x2dshared\x2d\x2dswap.device +After=dev-mapper-store\x2dshared\x2d\x2dswap.device +Before=dev-mapper-store\x2dshared\x2d\x2dswap.swap +# Add an explicit sequencing to LVM setup. The observed problem is that the +# LV apparently becomes notified as "ready" through udev before the actual +# lvcreate command has finished. This results in filesystem setup racing with +# lvcreate performing a "wipe" of the LV, resulting in a destroyed filesystem +# in turn. +After=setup-lvs.service +Requires=setup-lvs.service + +[Install] +RequiredBy=systemd-fsck@dev-mapper-store\x2dshared\x2d\x2dswap.service +WantedBy=local-fs.target + +[Service] +Type=oneshot +RemainAfterExit=true +ExecStart=/opt/ic/bin/setup-shared-swap.sh diff --git a/ic-os/components/upgrade/shared-resources/setup-shared-resources/setup-shared-swap.sh b/ic-os/components/upgrade/shared-resources/setup-shared-resources/setup-shared-swap.sh new file mode 100755 index 00000000000..a2f38d0ac5c --- /dev/null +++ b/ic-os/components/upgrade/shared-resources/setup-shared-resources/setup-shared-swap.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +blkid /dev/mapper/store-shared--swap >/dev/null || ( + mkswap /dev/mapper/store-shared--swap +)