Advertisement
Guest User

nav-sdcard-format.sh

a guest
May 11th, 2021
2,250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [ "${EUID}" -ne 0 ]; then
  4. (>&2 echo "only support exec as root.")
  5. exit 1
  6. fi
  7.  
  8. set -euo pipefail
  9.  
  10. # global errors
  11. ERR_ARGS=1
  12. ERR_DEV_PERMISSION=2
  13. ERR_UNSUPPORTED_DISK_TYPE=3
  14. ERR_DEV_SIZE=4
  15.  
  16. # globals
  17. BLOCK_DEV=""
  18. SHOW_HELP=0
  19. FORCE_FORMAT=0
  20. NAVDEV=""
  21. OFFDEV=""
  22. # Minimal partition size in sectors:
  23. # - 15388655 Sector * 512 (Byte/Sector) = 7878991360 Bytes
  24. # - 7878991360 Bytes / 1024 / 1024 / 1024 = 7.34GiB
  25. MIN_PART_SECTOR=15388655
  26. HALF_DEV_SECTOR=0
  27. PARTITION_MARK=""
  28.  
  29. print_help() {
  30. (>&2 cat <<EOF
  31. Usage:
  32. nav-sdcard-format.sh [option] [dev]
  33.  
  34. Partition a block device into two 7.9GB (7.34GiB) ext4 partitions
  35. Means you need more than 16GB (30777312 Sector) on your device
  36.  
  37. option:
  38. -h, --help display the help
  39. -f, --force force repartioning
  40.  
  41. dev:
  42. Path to block device. Accept sdx, mmcblkx type devices.
  43. EOF
  44. )
  45. exit "${ERR_ARGS}"
  46. }
  47.  
  48. parse_args() {
  49. # show help when there're no args
  50. if [ "$#" -eq 0 ]; then
  51. SHOW_HELP=1
  52. fi
  53.  
  54. # Process options
  55. while [ "$#" -gt 1 ]; do
  56. case $1 in
  57. -h | --help)
  58. echo 'in help'
  59. SHOW_HELP=1
  60. shift
  61. ;;
  62. -f | --force)
  63. FORCE_FORMAT=1
  64. shift
  65. ;;
  66. esac
  67. done
  68.  
  69. # print help and exit
  70. if [ "${SHOW_HELP}" -ne 0 ]; then
  71. print_help
  72. fi
  73.  
  74. # Process required parameters
  75. BLOCK_DEV="$1"
  76. shift
  77. }
  78.  
  79. get_partition_prefix() {
  80. local P_
  81.  
  82. # auto detect if it's testing & set disk partition name
  83. # (e.g. /dev/sdb -> /dev/sdb1 & /dev/sdb2
  84. # /dev/mmcblk1 -> /dev/mmcblk1p1 & /dev/mmcblk1p2)
  85. case "${BLOCK_DEV}" in
  86. /dev/sd* ) P_="";;
  87. /dev/mmcblk* ) P_="p";;
  88. * ) echo "Support only sdx, mmc."; exit "${ERR_UNSUPPORTED_DISK_TYPE}";;
  89. esac
  90.  
  91. echo -n "${P_}"
  92. }
  93.  
  94. check_device_partition() {
  95. # Check device write permission
  96. if [ ! -w "${BLOCK_DEV}" ] || [ ! -r "${BLOCK_DEV}" ]; then
  97. (>&2 echo "Device ${BLOCK_DEV} is not readable/writeable")
  98. return 1
  99. fi
  100.  
  101. if [ -e "${NAVDEV}" ] || [ -e "${OFFDEV}" ]; then
  102. return 1
  103. fi
  104.  
  105. return 0
  106. }
  107.  
  108. do_partition() {
  109. local PART_LAYOUT
  110. PART_LAYOUT="$(
  111. cat <<EOF
  112. ,${MIN_PART_SECTOR},L
  113. ,${MIN_PART_SECTOR},L
  114. EOF
  115. )"
  116. echo "${PART_LAYOUT}" | sfdisk -f "${BLOCK_DEV}"
  117. return 0
  118. }
  119.  
  120. # main() function
  121.  
  122. parse_args "$@"
  123.  
  124. PARTITION_MARK="$(get_partition_prefix)"
  125.  
  126. NAVDEV="${BLOCK_DEV}${PARTITION_MARK}1"
  127. OFFDEV="${BLOCK_DEV}${PARTITION_MARK}2"
  128.  
  129. HALF_DEV_SECTOR=$(( $(blockdev --getsz "${BLOCK_DEV}") / 2 - 1 ))
  130.  
  131. check_device_partition || if [ "${FORCE_FORMAT}" -eq 0 ]; then
  132. echo "Block device appears to already be partitioned and formated. Use -f to force repartion."
  133. exit "${ERR_DEV_PERMISSION}"
  134. fi
  135. grep -q /opt/navigon /proc/mounts && umount /opt/navigon
  136. grep -q "${NAVDEV}" /proc/mounts && umount "${NAVDEV}"
  137. grep -q "${OFFDEV}" /proc/mounts && umount "${OFFDEV}"
  138.  
  139. if [ "${HALF_DEV_SECTOR}" -lt "${MIN_PART_SECTOR}" ] ; then
  140. echo "Two partitions of needed size will not fit."
  141. echo "Need minium: 16 GB"
  142. echo "Available: $(($(blockdev --getsz "${BLOCK_DEV}") / 1024 / 1024 / 2)) GiB"
  143. exit "${ERR_DEV_SIZE}"
  144. fi
  145.  
  146.  
  147. do_partition || (>&2 echo 'partition failed')
  148. sync
  149. sfdisk -l "${BLOCK_DEV}"
  150.  
  151. # Wait until NAVDEV and OFFDEV partitions are created
  152. while [ ! -r "${NAVDEV}" ] || [ ! -r "${OFFDEV}" ]; do sleep 1; done
  153. ls /dev/mmc*
  154.  
  155. # Natually the SDCard should be fully formatted to its expected size as the following:
  156. # - mke2fs -F -t ext4 -E nodiscard -Lempty_nav "${NAVDEV}"
  157. # - mke2fs -F -t ext4 -E nodiscard -Lalt_nav "${OFFDEV}"
  158. # However, on tegra platform for Model S/X, formatting speed is incredibly slow, which
  159. # we choose to optimize to format a very small amount on these two partitions for storing
  160. # only the "empty.txt" file.
  161. # Free to change to `nodiscard` when tegra platform is retired.
  162. mke2fs -F -t ext4 -b4096 -Lempty_nav "${NAVDEV}" 2048
  163. mke2fs -F -t ext4 -b4096 -Lalt_nav "${OFFDEV}" 2048
  164.  
  165. mkdir -p /opt/navigon
  166. mount "${NAVDEV}" /opt/navigon
  167. touch /opt/navigon/empty.txt
  168. umount -l "${NAVDEV}"
  169.  
  170. mkdir -p /opt/navigoff
  171. mount "${OFFDEV}" /opt/navigoff
  172. touch /opt/navigoff/empty.txt
  173. umount -l "${OFFDEV}"
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement