logo

live-bootstrap

Mirror of <https://github.com/fosslinux/live-bootstrap>

move_disk.sh (1709B)


  1. #!/bin/sh
  2. #
  3. # SPDX-FileCopyrightText: 2023 fosslinux <fosslinux@aussies.space>
  4. #
  5. # SPDX-License-Identifier: GPL-3.0-or-later
  6. set -e
  7. # mount might fail if /etc doesn't exist because of fstab and mtab
  8. mkdir -p /dev /etc
  9. mount -t devtmpfs none /dev &> /junk || true # no /dev/null yet
  10. rm /junk &> /dev/null || true
  11. timeout=120
  12. while ! dd if=/dev/${DISK} of=/dev/null bs=512 count=1; do
  13. sleep 1
  14. # shellcheck disable=SC2219
  15. let timeout--
  16. if [ "${timeout}" -le 0 ]; then
  17. echo "Timeout reached for disk to become accessible"
  18. false
  19. fi
  20. done
  21. # Create partition if it doesn't exist
  22. # 'stat -c "%T"' prints the minor device type in hexadecimal.
  23. # The decimal version (with "%Lr") is not available in this version of stat.
  24. if [ $((0x$(stat -c "%T" "/dev/${DISK}") % 8)) -eq 0 ]; then
  25. echo "Creating partition table..."
  26. # Start at 1GiB, use -S32 -H64 to align to MiB rather than cylinder boundary
  27. echo "2097152;" | sfdisk -uS -S32 -H64 --force "/dev/${DISK}"
  28. fdisk -l "/dev/${DISK}"
  29. echo "Creating ext4 partition..."
  30. mkfs.ext4 -F -F "/dev/${DISK}1"
  31. DISK="${DISK}1"
  32. echo DISK="${DISK}" >> /steps/bootstrap.cfg
  33. fi
  34. # Mount the partition, move everything into /external
  35. mkdir -p /newroot
  36. mount -t ext4 "/dev/${DISK}" /newroot
  37. mkdir -p /newroot/external
  38. mv /newroot/* /newroot/external/ 2>/dev/null || true # obviously errors trying to move external into itself
  39. # Switch root
  40. mkdir -p /rootonly
  41. # This doesn't recursively mount - that's why we're able to copy everything over
  42. mount --bind / /rootonly
  43. cp -ar /rootonly/* /newroot/
  44. sed -e 's/newroot//' /rootonly/etc/mtab | grep -v 'rootonly' > /newroot/etc/mtab
  45. umount /rootonly
  46. switch_root /newroot /init