partition_usb.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/bash
  2. DISK="$1"
  3. # Make sure only root can run our script
  4. if [ "$(id -u)" != "0" ]; then
  5. echo "This script must be run as root" 1>&2
  6. exit 1
  7. fi
  8. if [ x"$DISK" == 'x' ]; then
  9. echo "This script requires a base block device as its only arguement" 1>&2
  10. echo "example: $0 /dev/sdb"
  11. exit 1
  12. fi
  13. DISK_DEVICE_NAME="$(echo $DISK | awk -F'/' '{print $NF}')"
  14. ROOT_PARTITION="$DISK"2
  15. SWAP_PARTITION="$DISK"3
  16. # TODO allow specify TOTAL_SECTORS
  17. TOTAL_SECTORS=$(cat /sys/block/$DISK_DEVICE_NAME/size)
  18. SECTOR_SIZE=$(cat /sys/block/$DISK_DEVICE_NAME/queue/hw_sector_size)
  19. # the following are in sectors, 1MB = 2048 sectors
  20. FIRST_GAP_SIZE=8192 # 2048 sectors * 4MB
  21. LAST_GAP_SIZE=2048 # 2048 sectors * 1MB
  22. SWAP_PART_SIZE=8388608 # 2048 sectors * 4096MB
  23. KERNEL_PART_SIZE=65536 # 2048 sectors * 32MB
  24. # layout arithmatic
  25. FIRST_GAP_START=0
  26. FIRST_GAP_END=$(( $FIRST_GAP_SIZE - 1 ))
  27. KERNEL_PART_START=$(( $FIRST_GAP_END + 1 ))
  28. KERNEL_PART_END=$(( $FIRST_GAP_END + $KERNEL_PART_SIZE ))
  29. ROOTFS_PART_START=$(( $KERNEL_PART_END + 1 ))
  30. LAST_GAP_END=$(( $TOTAL_SECTORS - 1 ))
  31. LAST_GAP_START=$(( $LAST_GAP_END - $LAST_GAP_SIZE + 1 ))
  32. if [ $TOTAL_SECTORS -lt 10000000 ]; then # if size of disk is less than 5GB do not make swap
  33. MAKESWAP='false'
  34. ROOTFS_PART_END=$(( $LAST_GAP_START - 1 ))
  35. SWAP_PART_END=0
  36. SWAP_PART_START=0
  37. else
  38. MAKESWAP='true'
  39. SWAP_PART_END=$(( $LAST_GAP_START - 1 ))
  40. SWAP_PART_START=$(( $SWAP_PART_END - $SWAP_PART_SIZE + 1 ))
  41. ROOTFS_PART_END=$(( $SWAP_PART_START - 1 ))
  42. fi
  43. echo "TOTAL_SECTORS = $TOTAL_SECTORS"
  44. echo "SECTOR_SIZE = $SECTOR_SIZE"
  45. echo ""
  46. echo "FIRST_GAP_SIZE = $FIRST_GAP_SIZE"
  47. echo "LAST_GAP_SIZE = $LAST_GAP_SIZE"
  48. echo "SWAP_PART_SIZE = $SWAP_PART_SIZE"
  49. echo "KERNEL_PART_SIZE = $KERNEL_PART_SIZE"
  50. echo ""
  51. echo "FIRST_GAP_START = $FIRST_GAP_START $(( $FIRST_GAP_START / 2048 )) MB"
  52. echo "FIRST_GAP_END = $FIRST_GAP_END"
  53. echo "KERNEL_PART_START = $KERNEL_PART_START $(( $KERNEL_PART_START / 2048 )) MB"
  54. echo "KERNEL_PART_END = $KERNEL_PART_END"
  55. echo "ROOTFS_PART_START = $ROOTFS_PART_START $(( $ROOTFS_PART_START / 2048 )) MB"
  56. echo "ROOTFS_PART_END = $ROOTFS_PART_END"
  57. echo "SWAP_PART_START = $SWAP_PART_START $(( $SWAP_PART_START / 2048 )) MB"
  58. echo "SWAP_PART_END = $SWAP_PART_END"
  59. echo "LAST_GAP_START = $LAST_GAP_START $(( $LAST_GAP_START / 2048 )) MB"
  60. echo "LAST_GAP_END = $LAST_GAP_END"
  61. # use `sgdisk --list-types' to get all possible type codes
  62. KERNEL_PART_TYPE='7f00'
  63. ROOTFS_PART_TYPE='7f01'
  64. SWAP_PART_TYPE='8200'
  65. # wipe disk, and create new gpt partition table
  66. sgdisk --zap-all $DISK
  67. sgdisk -o $DISK
  68. # create the kernel partition
  69. sgdisk --new=1:$KERNEL_PART_START:$KERNEL_PART_END $DISK
  70. sgdisk --typecode=1:$KERNEL_PART_TYPE $DISK
  71. sgdisk --change-name=1:Kernel $DISK
  72. sgdisk --attributes=1:=:015A000000000000 $DISK
  73. # create the rootfs partition
  74. sgdisk --new=2:$ROOTFS_PART_START:$ROOTFS_PART_END $DISK
  75. sgdisk --typecode=2:$ROOTFS_PART_TYPE $DISK
  76. sgdisk --change-name=2:Root $DISK
  77. mkfs.ext4 $ROOT_PARTITION
  78. # create the swap partition
  79. if [ $MAKESWAP = 'true' ]; then
  80. sgdisk --new=3:$SWAP_PART_START:$SWAP_PART_END $DISK
  81. sgdisk --typecode=3:$SWAP_PART_TYPE $DISK
  82. sgdisk --change-name=3:Swap $DISK
  83. mkswap $SWAP_PARTITION
  84. fi
  85. sgdisk --print $DISK
  86. lsblk $DISK