diff --git a/update.sh b/update.sh index 0b7f21b..77fc11f 100755 --- a/update.sh +++ b/update.sh @@ -69,27 +69,61 @@ CURRENT_ROOT_HASH=${CURRENT_ROOT_HASH%% *} CURRENT_ROOT_UUID=${CURRENT_ROOT_HASH:32:8}-${CURRENT_ROOT_HASH:40:4}-${CURRENT_ROOT_HASH:44:4}-${CURRENT_ROOT_HASH:48:4}-${CURRENT_ROOT_HASH:52:12} -[[ /dev/disk/by-partlabel/root1 -ef /dev/disk/by-partuuid/${CURRENT_ROOT_UUID} ]] \ - && NEW_ROOT_NUM=2 && OLD_ROOT_NUM=1 +bootdisk() { + UUID=$({ read -r -n 1 -d '' _; read -n 72 uuid; echo -n ${uuid,,}; } < /sys/firmware/efi/efivars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f) -[[ /dev/disk/by-partlabel/root2 -ef /dev/disk/by-partuuid/${CURRENT_ROOT_UUID} ]] \ - && NEW_ROOT_NUM=1 && OLD_ROOT_NUM=2 + [[ $UUID ]] || return 1 + echo "/dev/disk/by-partuuid/$UUID" + return 0 +} -if ! [[ $NEW_ROOT_NUM ]]; then +get_disk() { + for dev in /dev/disk/by-path/*; do + [[ $dev -ef $1 ]] || continue + echo ${dev%-part*} + return 0 + done + return 1 +} + +ROOT_DEV=$(get_disk $(bootdisk)) + +if ! [[ $ROOT_DEV ]]; then echo "Current partitions booted from not found!" exit 1 fi -## find base device and partition number -for dev in /dev/disk/by-path/*; do - if ! [[ $ROOT_PARTNO ]] && [[ /dev/disk/by-partlabel/root${NEW_ROOT_NUM} -ef $dev ]]; then - ROOT_PARTNO=${dev##*-part} - ROOT_DEV=${dev%-part*} - fi - [[ $ROOT_PARTNO ]] && break +unset FOUND +for dev in ${ROOT_DEV}-part*; do + [[ $(blkid -o value -s PARTLABEL $dev) == root1 ]] && + ROOT1_DEV=$dev + + [[ $(blkid -o value -s PARTLABEL $dev) == root2 ]] && + ROOT2_DEV=$dev + + [[ $(blkid -o value -s PARTUUID $dev) == $CURRENT_ROOT_UUID ]] && + CURRENT_ROOT_DEV=$dev + + [[ $ROOT1_DEV ]] && [[ $ROOT2_DEV ]] && [[ $CURRENT_ROOT_DEV ]] && break done -if ! [[ $ROOT_PARTNO ]] || ! [[ $ROOT_DEV ]]; then +if ! [[ $ROOT1_DEV ]] || ! [[ $ROOT2_DEV ]] || ! [[ $CURRENT_ROOT_DEV ]]; then + echo "Couldn't find partition numbers" + exit 1 +fi + +[[ $CURRENT_ROOT_DEV == $ROOT2_DEV ]] \ + && NEW_ROOT_NUM=1 && OLD_ROOT_NUM=2 \ + && NEW_ROOT_PARTNO=${ROOT1_DEV##*-part} + + +[[ $CURRENT_ROOT_DEV == $ROOT1_DEV ]] \ + && NEW_ROOT_NUM=2 && OLD_ROOT_NUM=1 \ + && NEW_ROOT_PARTNO=${ROOT2_DEV##*-part} + +ROOT_PARTNO=${CURRENT_ROOT_DEV##*-part} + +if ! [[ $NEW_ROOT_PARTNO ]] || ! [[ $ROOT_PARTNO ]] || ! [[ $ROOT_DEV ]]; then echo "Couldn't find partition numbers" exit 1 fi @@ -129,12 +163,12 @@ if ! [[ $NO_CHECK ]]; then sha512sum -c sha512sum.txt fi -dd status=progress if=root.img of=/dev/disk/by-partlabel/root${NEW_ROOT_NUM} +dd status=progress if=root.img of=${ROOT_DEV}-part${NEW_ROOT_PARTNO} # set the new partition uuids ROOT_UUID=${ROOT_HASH:32:8}-${ROOT_HASH:40:4}-${ROOT_HASH:44:4}-${ROOT_HASH:48:4}-${ROOT_HASH:52:12} -sfdisk --part-uuid ${ROOT_DEV} ${ROOT_PARTNO} ${ROOT_UUID} +sfdisk --part-uuid ${ROOT_DEV} ${NEW_ROOT_PARTNO} ${ROOT_UUID} # install to /efi mkdir -p /efi/EFI/${NAME}