Make move_disk work on bare metal

* Use -S32 -H64 --force to trick sfdisk into allowing MiB-aligned
  partitions on a drive that reports a CHS geometry
* Explicitly create partition at sector 2097152 (=1GiB)
* Force mkfs.ext4 to overwrite any existing filesystem it might find
* Wait up to 2 minutes for the disk to become readable (especially
  USB drives often show up with a delay)
This commit is contained in:
Gábor Stefanik 2024-01-02 00:57:29 +01:00
parent 8341c4e089
commit 6c2144e50a
1 changed files with 14 additions and 2 deletions

View File

@ -10,13 +10,25 @@ mkdir -p /dev /etc
mount -t devtmpfs none /dev &> /junk || true # no /dev/null yet
rm /junk &> /dev/null || true
timeout=120
while ! dd if=/dev/${DISK} of=/dev/null bs=512 count=1; do
sleep 1
# shellcheck disable=SC2219
let timeout--
if [ "${timeout}" -le 0 ]; then
echo "Timeout reached for disk to become accessible"
false
fi
done
# Create partition if it doesn't exist
if [ $(($(stat -c "%Lr" "/dev/${DISK}") % 8)) -eq 0 ]; then
echo "Creating partition table..."
echo ";" | sfdisk "/dev/${DISK}"
# Start at 1GiB, use -S32 -H64 to align to MiB rather than cylinder boundary
echo "2097152;" | sfdisk -uS -S32 -H64 --force "/dev/${DISK}"
fdisk -l "/dev/${DISK}"
echo "Creating ext4 partition..."
mkfs.ext4 "/dev/${DISK}1"
mkfs.ext4 -F -F "/dev/${DISK}1"
DISK="${DISK}1"
fi