Advertisement
serafimdahl

nav-card-format.sh

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