From 1a8f9ca2e3d54d8c4ec3920e5c8035ac6030a937 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Tue, 8 Dec 2020 01:11:00 +0100 Subject: [PATCH] Avoid waiting for external drive unnecessarily (#1066) * Avoid waiting for external drive unnecessarily Even though the condition to start hassos-data.service is not met (the file /mnt/overlay/data-move is not there by default), it seems that systemd waits for the dependencies for hassos-data.service. Don't Require or Wants any dependencies which might not be present by default. * Use systemd to wait for partition using partlabel device * Use sfdisk which allows to wipe filesystem signatures Even though we zap the partition table using sgdisk, the file system superblock (which contains the file system label) does survive. This can cause problems when trying to reuse a disk previously already labeled using hassos-data: It might take precendence on next boot over the existing data partition on the eMMC. Make sure to clean all file system signatures using sfdisk. --- buildroot-external/rootfs-overlay/usr/bin/datactl | 14 ++++++-------- .../usr/lib/systemd/system/hassos-data.service | 2 +- .../rootfs-overlay/usr/libexec/hassos-data | 10 ++++++++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/buildroot-external/rootfs-overlay/usr/bin/datactl b/buildroot-external/rootfs-overlay/usr/bin/datactl index 4a31fb9f5..152b942f3 100755 --- a/buildroot-external/rootfs-overlay/usr/bin/datactl +++ b/buildroot-external/rootfs-overlay/usr/bin/datactl @@ -36,13 +36,11 @@ if [ "${1}" = "move" ] && [ -e "${2}" ]; then exit 1 fi - sgdisk --zap-all "${NEW_DEVICE_ROOT}" - sgdisk \ - -n "0:0:0" \ - -c "0:hassos-data-external" \ - -t "0:0FC63DAF-8483-4772-8E79-3D69D8477DE4" \ - -u "0:a52a4597-fa3a-4851-aefd-2fbe9f849079" \ - "${NEW_DEVICE_ROOT}" + # Create GPT partition table with a single data partition + cat << EOF | sfdisk --wipe-partitions=always --wipe=always "${NEW_DEVICE_ROOT}" +label: gpt +uuid=a52a4597-fa3a-4851-aefd-2fbe9f849079, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, name=hassos-data-external +EOF # Since we create a new partition table etc. we are guaranteed the target # partition is partition 1 @@ -51,7 +49,7 @@ if [ "${1}" = "move" ] && [ -e "${2}" ]; then if [ "${NEW_DEVICE_PART_SIZE}" -lt "${OLD_DEVICE_PART_SIZE}" ]; then echo "[INFO] Target device too small!" - sgdisk --zap-all "${NEW_DEVICE_ROOT}" + echo "label: gpt" | sfdisk "${NEW_DEVICE_ROOT}" exit 1 fi diff --git a/buildroot-external/rootfs-overlay/usr/lib/systemd/system/hassos-data.service b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/hassos-data.service index f2b4660c0..6f8b2136f 100644 --- a/buildroot-external/rootfs-overlay/usr/lib/systemd/system/hassos-data.service +++ b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/hassos-data.service @@ -3,7 +3,7 @@ Description=Home Assistant OS data partition migration DefaultDependencies=no RefuseManualStart=true RefuseManualStop=true -Requires=mnt-overlay.mount dev-disk-by\x2dlabel-hassos\x2ddata.device dev-disk-by\x2dpartlabel-hassos\x2ddata\x2dexternal.device +Requires=mnt-overlay.mount dev-disk-by\x2dlabel-hassos\x2ddata.device Wants=hassos-expand.service After=mnt-overlay.mount dev-disk-by\x2dlabel-hassos\x2ddata.device systemd-fsck@dev-disk-by\x2dlabel-hassos\x2ddata.service dev-disk-by\x2dpartlabel-hassos\x2ddata\x2dexternal.device Before=hassos-expand.service diff --git a/buildroot-external/rootfs-overlay/usr/libexec/hassos-data b/buildroot-external/rootfs-overlay/usr/libexec/hassos-data index 83a38c556..671f779b5 100755 --- a/buildroot-external/rootfs-overlay/usr/libexec/hassos-data +++ b/buildroot-external/rootfs-overlay/usr/libexec/hassos-data @@ -7,6 +7,16 @@ set -e # Rely on systemd-udev symlinks to find current data partition by fs label OLD_DEVICE_CHILD="$(readlink -f "/dev/disk/by-label/hassos-data")" +# We cannot rely on systemd Wants/Requires since this is evaluated before +# ConditionPathExists, and would make us wait for an external device on +# every boot. +# Instead, just start the unit here, which makes sure a device with the +# partlabel "hassos-data-external" is present. +if ! systemctl start "dev-disk-by\\x2dpartlabel-hassos\\x2ddata\\x2dexternal.device"; then + echo "[ERROR] External data device ${NEW_DEVICE} not found!" + exit 1 +fi + # Rely on systemd-udev symlinks to find external data partition by partlabel NEW_DEVICE_CHILD="$(readlink -f "/dev/disk/by-partlabel/hassos-data-external")"