usb-part.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/bin/bash
  2. ## SuperGlue project | http://superglue.it | 2014 | GPLv3
  3. ## http://git.superglue.it/superglue/serverfiles
  4. ##
  5. ## usb-part.sh - partition USB storage device
  6. # - detect the last USB attached disk drive
  7. # - check for sg-data partition
  8. # - if sg-data not mounted but device is present, then
  9. # offer to format device
  10. findUsbstor() {
  11. local _P='/sys/block/'
  12. local _D _DEV
  13. for _D in ${_P}sd*; do
  14. _DEV=$(readlink -f ${_D}/device)
  15. if [[ ${_DEV/usb} != $_DEV ]]; then
  16. _USBDEV="/dev/${_D/$_P}"
  17. fi
  18. done
  19. [[ $_USBDEV ]] || return 1
  20. }
  21. findMount() {
  22. local _M
  23. IFS=$'\n'
  24. for _M in $(mount); do
  25. if [[ "$_M" != "${_M/"$_USBDEV"}" ]]; then
  26. IFS=' ' _M=( $_M )
  27. _USBMNT="${_M[0]}"
  28. fi
  29. done
  30. [[ $_USBMNT ]] || return 1
  31. }
  32. ## partitions layout for sfdisk
  33. ## /dev/sdx1 64K, fat32
  34. ## TODO: define swap size according to ram
  35. ## /dev/sdx2 32M, swap v1
  36. ## /dev/sdx3 rest of the disk, linux partition
  37. _PARTITIONS="unit: sectors
  38. ${_DEV}1 : start= 2048, size= 128, Id= b
  39. ${_DEV}2 : start= 4096, size= 65536, Id=82
  40. ${_DEV}3 : start= 69632, size= , Id=83
  41. ${_DEV}4 : start= 0, size= 0, Id= 0
  42. "
  43. ## base64 encoded gzip'ed image of /dev/sdx1 FAT32 partition
  44. _FAT_README='H4sICC5ykVQAA3NkYjEuZGQA7d3PaxNpGMDxp1VQI1FxYcWD+FRB8DJp68KCiFBxXAS1tUn8gSC8
  45. 7bxtZ5vMhJm3pgVxvSzsSezJi3+BePQmiAevPfof2FNFPHra7DtNo1VUSgWL7veTvDzv+z7zPplM
  46. YCAE8q6cut+cncqDKeOkf3uf9Ev/XXnXJ6f9Q3bKqrty/N7DQ2+q9bFw/I8L9VD13EhtaFhV9xx+
  47. duP24yPP3e4rT/Y83SFL+26uvB1+tfTr0sGVf2szca7+maROjU6kqTMTDatRnM8GqmMNa3KrcZLb
  48. 7KP8VCNttRbUJFG51Mpsnvvugs7aBXWpusxnpk2caBAEWi4JvkX90btO58PwbefLh+KnxOf//7bu
  49. pu7v9i9uLYaLYTd288ttafnQ8fbKsc463cnlQDKxYiQSn2/6biBO5n0T38tlORw5ezG8M1S9OqYi
  50. U+1u/SJuExkUGemtbvr1+frV3frjq+tVa9dqKpFdW/9yfjHcJf8U5/e+/jVff2gtX8StuZ4AAAAA
  51. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAGzExOCV8xf19+C3YrDvw/zlAyKRSf40G6jRbrc39FqVpo1i
  52. U1ktW3n/x0OVzJqoaQM37zbzBgBsUFlODA8NFr2jRwb0l/31rT4hAAAAAN+Ra/XJXz72r423r8W+
  53. TyIAAAAAAPhxmUjk+l6Rv30rfv/vff9/7cdLvj3o5QAAAAAAwLdpz8STM8VW6XP5nGk0FlTquc10
  54. Mk2cTVyRaKSTxtlI00SN5rZlMj/UMy47V1U/cLGL00R6O65X51o2m27MWY2M88e7NDPTVs6O6qXR
  55. mtaroY6O68iFWjj+ubmBAf16oXKpVC5t6gyLheXSjHOtk5VK3isexI6N3AEAAAAAAAAAAAAAAAAA
  56. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwJb5D53OGNgAAAEA'
  57. if ! findUsbstor; then
  58. echo 'USB device not found'
  59. exit 1
  60. fi
  61. if findMount; then
  62. #umount -f ${_USBDEV}[1-9] 2>&1
  63. echo 'unmounting'
  64. umount -f $_USBMNT 2>&1
  65. if [[ $? -gt 0 ]]; then
  66. echo 'error unmounting'
  67. exit 1
  68. fi
  69. fi
  70. swapoff -a &>/dev/null
  71. sfdisk -D -f -q $_USBDEV <<< "$_PARTITIONS"
  72. if [[ $? -gt 0 ]]; then
  73. echo 'error making partitions'
  74. exit 1
  75. fi
  76. (echo -n "$_FAT_README" | base64 -d | gunzip -c > ${_USBDEV}1)
  77. if [[ $? -gt 0 ]]; then
  78. echo 'error cloning FAT readme partition'
  79. exit 1
  80. fi
  81. mkswap ${_USBDEV}2
  82. if [[ $? -gt 0 ]]; then
  83. echo 'error making swap'
  84. exit 1
  85. fi
  86. mkfs.btrfs -L sg-data ${_USBDEV}3
  87. if [[ $? -gt 0 ]]; then
  88. echo 'error making Btrfs'
  89. exit 1
  90. fi
  91. exit 0