#!/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 if [ -z "${DATACTL_NOCONFIRM}" ]; then 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 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. If the device name ends with a digit, "p" is # placed between device name and partition number. case "${NEW_DEVICE_ROOT}" in *[0-9]) NEW_DEVICE_PART="${NEW_DEVICE_ROOT}p1";; *) NEW_DEVICE_PART="${NEW_DEVICE_ROOT}1";; esac NEW_DEVICE_PART_SIZE=$(cat "/sys/class/block/$(basename "${NEW_DEVICE_PART}")/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 Moves data partition to external device provided by (without partition number). A new partition table and a partition for the complete device will be created by datactl. EOF fi