Skip to content

Instantly share code, notes, and snippets.

@trisk
Created November 19, 2021 10:50
Show Gist options
  • Save trisk/adc9b5042813b3fd1987395fbe5eea34 to your computer and use it in GitHub Desktop.
Save trisk/adc9b5042813b3fd1987395fbe5eea34 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ "${EUID}" -ne 0 ]; then
(>&2 echo "only support exec as root.")
exit 1
fi
set -euo pipefail
# global errors
ERR_ARGS=1
ERR_DEV_PERMISSION=2
ERR_UNSUPPORTED_DISK_TYPE=3
ERR_DEV_SIZE=4
# globals
BLOCK_DEV=""
SHOW_HELP=0
FORCE_FORMAT=0
NAVDEV=""
OFFDEV=""
# Minimal partition size in sectors:
# - 15388655 Sector * 512 (Byte/Sector) = 7878991360 Bytes
# - 7878991360 Bytes / 1024 / 1024 / 1024 = 7.34GiB
MIN_PART_SECTOR=15388655
HALF_DEV_SECTOR=0
PARTITION_MARK=""
print_help() {
(>&2 cat <<EOF
Usage:
nav-sdcard-format.sh [option] [dev]
Partition a block device into two 7.9GB (7.34GiB) ext4 partitions
Means you need more than 16GB (30777312 Sector) on your device
option:
-h, --help display the help
-f, --force force repartioning
dev:
Path to block device. Accept sdx, mmcblkx type devices.
EOF
)
exit "${ERR_ARGS}"
}
parse_args() {
# show help when there're no args
if [ "$#" -eq 0 ]; then
SHOW_HELP=1
fi
# Process options
while [ "$#" -gt 1 ]; do
case $1 in
-h | --help)
echo 'in help'
SHOW_HELP=1
shift
;;
-f | --force)
FORCE_FORMAT=1
shift
;;
esac
done
# print help and exit
if [ "${SHOW_HELP}" -ne 0 ]; then
print_help
fi
# Process required parameters
BLOCK_DEV="$1"
shift
}
get_partition_prefix() {
local P_
# auto detect if it's testing & set disk partition name
# (e.g. /dev/sdb -> /dev/sdb1 & /dev/sdb2
# /dev/mmcblk1 -> /dev/mmcblk1p1 & /dev/mmcblk1p2)
case "${BLOCK_DEV}" in
/dev/sd* ) P_="";;
/dev/mmcblk* ) P_="p";;
* ) echo "Support only sdx, mmc."; exit "${ERR_UNSUPPORTED_DISK_TYPE}";;
esac
echo -n "${P_}"
}
check_device_partition() {
# Check device write permission
if [ ! -w "${BLOCK_DEV}" ] || [ ! -r "${BLOCK_DEV}" ]; then
(>&2 echo "Device ${BLOCK_DEV} is not readable/writeable")
return 1
fi
if [ -e "${NAVDEV}" ] || [ -e "${OFFDEV}" ]; then
return 1
fi
return 0
}
do_partition() {
local PART_LAYOUT
PART_LAYOUT="$(
cat <<EOF
,${MIN_PART_SECTOR},L
,${MIN_PART_SECTOR},L
EOF
)"
echo "${PART_LAYOUT}" | sfdisk -f "${BLOCK_DEV}"
return 0
}
# main() function
parse_args "$@"
PARTITION_MARK="$(get_partition_prefix)"
NAVDEV="${BLOCK_DEV}${PARTITION_MARK}1"
OFFDEV="${BLOCK_DEV}${PARTITION_MARK}2"
HALF_DEV_SECTOR=$(( $(blockdev --getsz "${BLOCK_DEV}") / 2 - 1 ))
check_device_partition || if [ "${FORCE_FORMAT}" -eq 0 ]; then
echo "Block device appears to already be partitioned and formated. Use -f to force repartion."
exit "${ERR_DEV_PERMISSION}"
fi
grep -q /opt/navigon /proc/mounts && umount /opt/navigon
grep -q "${NAVDEV}" /proc/mounts && umount "${NAVDEV}"
grep -q "${OFFDEV}" /proc/mounts && umount "${OFFDEV}"
if [ "${HALF_DEV_SECTOR}" -lt "${MIN_PART_SECTOR}" ] ; then
echo "Two partitions of needed size will not fit."
echo "Need minium: 16 GB"
echo "Available: $(($(blockdev --getsz "${BLOCK_DEV}") / 1024 / 1024 / 2)) GiB"
exit "${ERR_DEV_SIZE}"
fi
do_partition || (>&2 echo 'partition failed')
sync
sfdisk -l "${BLOCK_DEV}"
# Wait until NAVDEV and OFFDEV partitions are created
while [ ! -r "${NAVDEV}" ] || [ ! -r "${OFFDEV}" ]; do sleep 1; done
ls /dev/mmc*
# Natually the SDCard should be fully formatted to its expected size as the following:
# - mke2fs -F -t ext4 -E nodiscard -Lempty_nav "${NAVDEV}"
# - mke2fs -F -t ext4 -E nodiscard -Lalt_nav "${OFFDEV}"
# However, on tegra platform for Model S/X, formatting speed is incredibly slow, which
# we choose to optimize to format a very small amount on these two partitions for storing
# only the "empty.txt" file.
# Free to change to `nodiscard` when tegra platform is retired.
mke2fs -F -t ext4 -b4096 -Lempty_nav "${NAVDEV}" 2048
mke2fs -F -t ext4 -b4096 -Lalt_nav "${OFFDEV}" 2048
mkdir -p /opt/navigon
mount "${NAVDEV}" /opt/navigon
touch /opt/navigon/empty.txt
umount -l "${NAVDEV}"
mkdir -p /opt/navigoff
mount "${OFFDEV}" /opt/navigoff
touch /opt/navigoff/empty.txt
umount -l "${OFFDEV}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment