usb-part.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. usbPart() {
  33. ## partitions layout for sfdisk
  34. ## /dev/sdx1 64K, fat32
  35. ## TODO: define swap size according to ram
  36. ## /dev/sdx2 32M, swap v1
  37. ## /dev/sdx3 rest of the disk, linux partition
  38. _PARTITIONS="unit: sectors
  39. ${_DEV}1 : start= 2048, size= 128, Id= b
  40. ${_DEV}2 : start= 4096, size= 65536, Id=82
  41. ${_DEV}3 : start= 69632, size= , Id=83
  42. ${_DEV}4 : start= 0, size= 0, Id= 0
  43. "
  44. ## base64 encoded gzip'ed image of /dev/sdx1 FAT32 partition
  45. _FAT_README='H4sICC5ykVQAA3NkYjEuZGQA7d3PaxNpGMDxp1VQI1FxYcWD+FRB8DJp68KCiFBxXAS1tUn8gSC8
  46. 7bxtZ5vMhJm3pgVxvSzsSezJi3+BePQmiAevPfof2FNFPHra7DtNo1VUSgWL7veTvDzv+z7zPplM
  47. YCAE8q6cut+cncqDKeOkf3uf9Ev/XXnXJ6f9Q3bKqrty/N7DQ2+q9bFw/I8L9VD13EhtaFhV9xx+
  48. duP24yPP3e4rT/Y83SFL+26uvB1+tfTr0sGVf2szca7+maROjU6kqTMTDatRnM8GqmMNa3KrcZLb
  49. 7KP8VCNttRbUJFG51Mpsnvvugs7aBXWpusxnpk2caBAEWi4JvkX90btO58PwbefLh+KnxOf//7bu
  50. pu7v9i9uLYaLYTd288ttafnQ8fbKsc463cnlQDKxYiQSn2/6biBO5n0T38tlORw5ezG8M1S9OqYi
  51. U+1u/SJuExkUGemtbvr1+frV3frjq+tVa9dqKpFdW/9yfjHcJf8U5/e+/jVff2gtX8StuZ4AAAAA
  52. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAGzExOCV8xf19+C3YrDvw/zlAyKRSf40G6jRbrc39FqVpo1i
  53. U1ktW3n/x0OVzJqoaQM37zbzBgBsUFlODA8NFr2jRwb0l/31rT4hAAAAAN+Ra/XJXz72r423r8W+
  54. TyIAAAAAAPhxmUjk+l6Rv30rfv/vff9/7cdLvj3o5QAAAAAAwLdpz8STM8VW6XP5nGk0FlTquc10
  55. Mk2cTVyRaKSTxtlI00SN5rZlMj/UMy47V1U/cLGL00R6O65X51o2m27MWY2M88e7NDPTVs6O6qXR
  56. mtaroY6O68iFWjj+ubmBAf16oXKpVC5t6gyLheXSjHOtk5VK3isexI6N3AEAAAAAAAAAAAAAAAAA
  57. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwJb5D53OGNgAAAEA'
  58. if ! findUsbstor; then
  59. echo 'USB device not found'
  60. exit 1
  61. fi
  62. if findMount; then
  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. }