set-disk-partitions 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/bash
  2. set -e
  3. set -x
  4. DEVICE=${1}
  5. DISKTYPE=${2}
  6. if [[ -z $DISKTYPE ]]; then
  7. DISKTYPE="msdos"
  8. fi
  9. if [[ -z $DEVICE ]]; then
  10. echo "Need to Pass a device name as arg1." 1>&2
  11. exit 1
  12. fi
  13. PARTITION_COUNT=$(grep $(echo $DEVICE | cut -d '/' -f3) /proc/partitions | wc -l)
  14. if [ "$PARTITION_COUNT" -gt "1" ]; then
  15. echo "Device ${DEVICE} already partitioned!"
  16. echo "Checking to see if it is mounted"
  17. # Check this container first...
  18. if grep -q "${DEVICE}" /proc/mounts; then
  19. echo "Device is mounted, we can not repartition" 1>&2
  20. exit 1
  21. fi
  22. # Check other system containers...
  23. for container in $(system-docker ps -q); do
  24. if system-docker exec $container grep -q "${DEVICE}" /proc/mounts; then
  25. echo "Device is mounted in system container ${container}, we can not repartition" 1>&2
  26. exit 1
  27. fi
  28. done
  29. fi
  30. dd if=/dev/zero of=${DEVICE} bs=512 count=2048
  31. partprobe ${DEVICE}
  32. # https://www.gnu.org/software/parted/manual/html_node/set.html
  33. # https://wiki.archlinux.org/index.php/syslinux
  34. BOOTFLAG="boot"
  35. if [ "${DISKTYPE}" == "gpt" ]; then
  36. BOOTFLAG="legacy_boot"
  37. fi
  38. parted -s -a optimal ${DEVICE} mklabel ${DISKTYPE} -- \
  39. mkpart primary ext4 1 -1 \
  40. set 1 ${BOOTFLAG} on