set-disk-partitions 938 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/bin/bash
  2. set -e
  3. set -x
  4. DEVICE=${1}
  5. if [[ -z $DEVICE ]]; then
  6. echo "Need to Pass a device name as arg1." 1>&2
  7. exit 1
  8. fi
  9. PARTITION_COUNT=$(grep $(echo $DEVICE | cut -d '/' -f3) /proc/partitions | wc -l)
  10. if [ "$PARTITION_COUNT" -gt "1" ]; then
  11. echo "Device ${DEVICE} already partitioned!"
  12. echo "Checking to see if it is mounted"
  13. # Check this container first...
  14. if grep -q "${DEVICE}" /proc/mounts; then
  15. echo "Device is mounted, we can not repartition" 1>&2
  16. exit 1
  17. fi
  18. # Check other system containers...
  19. for container in $(system-docker ps -q); do
  20. if system-docker exec $container grep -q "${DEVICE}" /proc/mounts; then
  21. echo "Device is mounted in system container ${container}, we can not repartition" 1>&2
  22. exit 1
  23. fi
  24. done
  25. fi
  26. dd if=/dev/zero of=${DEVICE} bs=512 count=2048
  27. partprobe ${DEVICE}
  28. fdisk ${DEVICE} <<EOF
  29. n
  30. p
  31. 1
  32. w
  33. EOF