Rewrite datactl command (#1046)
* Rewrite datactl command Prepare the target partition as part of the datactl command. Rely on partlabel for the target disk since we are always using GPT on the target disk. Use systemd and partlabel mechanism to wait and find the target data disk. Keep using the file system label to identify the source disk. Also use e2image instead of raw dd to move data. This should speed up the processes significantly. * Fix corner case when reusing same disk again
This commit is contained in:
@@ -1,31 +1,59 @@
|
||||
#!/bin/sh
|
||||
# ==============================================================================
|
||||
# HassOS data partition handling
|
||||
# Home Assistant OS data partition handling
|
||||
# ==============================================================================
|
||||
set -e
|
||||
|
||||
OPTION_FILE=/mnt/overlay/data.opt
|
||||
DATA_DEVICE_CHILD="$(findfs LABEL="hassos-data")"
|
||||
# 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}")"
|
||||
|
||||
# Move command
|
||||
if [ "${1}" = "move" ] && [ -e "${2}" ]; then
|
||||
DEVICE="${2}"
|
||||
NEW_DEVICE_ROOT="${2}"
|
||||
|
||||
# Check device
|
||||
if ! lsblk "${DEVICE}" | grep disk > /dev/null 2>&1; then
|
||||
if ! lsblk "${NEW_DEVICE_ROOT}" | grep disk > /dev/null 2>&1; then
|
||||
echo "[ERROR] Is not disk!"
|
||||
exit 1
|
||||
elif [ "${DEVICE}" = "${DATA_DEVICE_ROOT}" ]; then
|
||||
elif [ "${NEW_DEVICE_ROOT}" = "${DATA_DEVICE_ROOT}" ]; then
|
||||
echo "[ERROR] Can't be the same disk!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Flag device
|
||||
echo "WARNING: ${DEVICE} will be reset on next restart!"
|
||||
echo "Press a key to move forward"
|
||||
read -r
|
||||
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
|
||||
|
||||
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}"
|
||||
|
||||
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
|
||||
|
||||
echo "${DEVICE}" > ${OPTION_FILE}
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user