Files
operating-system/buildroot-external/rootfs-overlay/usr/bin/datactl
Stefan Agner 1a8f9ca2e3 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.
2020-12-08 01:11:00 +01:00

75 lines
2.4 KiB
Bash
Executable File

#!/bin/sh
# ==============================================================================
# Home Assistant OS data partition handling
# ==============================================================================
set -e
# Use current mount point. This avoids "Can't be the same disk!" error
# when using a drive which has been used as a data drive previously.
DATA_DEVICE_CHILD="$(findmnt --noheadings --output=source /mnt/data)"
DATA_DEVICE_ROOT="/dev/$(lsblk -no pkname "${DATA_DEVICE_CHILD}")"
if [ "${DATA_DEVICE_ROOT}" = "" ]; then
echo "[ERROR] Data disk device not found!"
exit 1
fi
# Move command
if [ "${1}" = "move" ] && [ -e "${2}" ]; then
NEW_DEVICE_ROOT="${2}"
# Check device
if ! lsblk "${NEW_DEVICE_ROOT}" | grep disk > /dev/null 2>&1; then
echo "[ERROR] Is not disk!"
exit 1
elif [ "${NEW_DEVICE_ROOT}" = "${DATA_DEVICE_ROOT}" ]; then
echo "[ERROR] Can't be the same disk!"
exit 1
fi
# Flag device
echo "WARNING: All partitions on ${NEW_DEVICE_ROOT} will be deleted!"
printf "Enter \"yes\" to confirm: "
read -r confirm
if [ "${confirm}" != "yes" ]; then
echo "Aborting."
exit 1
fi
# 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
NEW_DEVICE_PART_SIZE=$(cat "/sys/class/block/$(basename "${NEW_DEVICE_ROOT}")1/size")
OLD_DEVICE_PART_SIZE=$(cat "/sys/class/block/$(basename "${DATA_DEVICE_CHILD}")/size")
if [ "${NEW_DEVICE_PART_SIZE}" -lt "${OLD_DEVICE_PART_SIZE}" ]; then
echo "[INFO] Target device too small!"
echo "label: gpt" | sfdisk "${NEW_DEVICE_ROOT}"
exit 1
fi
touch "/mnt/overlay/move-data"
cat << EOF
Disk ${NEW_DEVICE_ROOT} has been prepared to be used as data drive and the data
move has been scheduled for the next reboot. Please reboot the device now and
make sure to leave the disk connected to the system from now on.
EOF
else
cat << EOF
Usage: datactl move <device>
Moves data partition to external device provided by <device> (without partition
number). A new partition table and a partition for the complete device will be
created by datactl.
EOF
fi