Last active
August 2, 2016 15:20
-
-
Save bradfa/1477240 to your computer and use it in GitHub Desktop.
BeagleBone format_sd Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Format an SD card for use in a TI development kit to boot u-boot and Linux. | |
# The layout is a small 32 MB FAT partition and a larger (remainder of device) | |
# ext4 partition. The FAT partition starts at 1 MB and the ext4 partition | |
# immediately follows. | |
# Stop execution on any error condition | |
set -e | |
# Echo all executed statements, useful for debugging | |
#set -x | |
echo_usage() { | |
echo "Usage: $0 [DEVICE]" | |
echo "Format an SD card DEVICE for use in TI dev kits" | |
echo | |
echo "DEVICE should be a block device like /dev/mmcblk0 or /dev/sdc." | |
echo "The device needs to be unmounted for this script to function." | |
} | |
# Exit showing a human readable string plus error number in red | |
die() { | |
local _num=${1} | |
local _name=${2} | |
if [ ! ${_num} ]; then | |
_num="255" | |
fi | |
if [ ! "${_name}" ]; then | |
_name="unspecified" | |
fi | |
if [ ${_num} -ne 0 ]; then | |
echo -e "\e[1;31mError: ${_name}, exiting with status ${_num}\e[0m" >&2 | |
fi | |
exit ${_num} | |
} | |
######################### | |
# Execution Starts Here # | |
######################### | |
# Ensure host has all the needed commands installed | |
awk -V > /dev/null || die 1 "could not find awk executable on your PATH" | |
fdisk -h > /dev/null || die 1 "could not find fdisk executable on your PATH" | |
sfdisk -h > /dev/null || die 1 "could not find sfdisk executable on your PATH" | |
which mkfs.vfat > /dev/null || die 1 "could not find mkfs.vfat executable on your PATH" | |
which mkfs.ext4 > /dev/null || die 1 "could not find mkfs.ext4 executable on your PATH" | |
# Determine the version of sfdisk as versions from util-linux 2.26 and newer | |
# do not support the old style parameters or command line switches and need a | |
# new type. | |
NEWSFDISK=0 | |
_SFDISKVER=`sfdisk -v | cut -d' ' -f 4` | |
_SFDISKVERMAJOR=`echo ${_SFDISKVER} | cut -d'.' -f 1` | |
_SFDISKVERMINOR=`echo ${_SFDISKVER} | cut -d'.' -f 2` | |
if [[ ${_SFDISKVERMAJOR} -gt 2 || ( ${_SFDISKVERMAJOR} -eq 2 && ${_SFDISKVERMINOR} -gt 25) ]]; then | |
NEWSFDISK=1 | |
fi | |
# Determine the version of mkfs.ext4 as v1.43 and newer need some defaults | |
# disabled due to u-boot not supporting them | |
NEWEXT4DEFAULTS=0 | |
_MKFSEXT4VER=`mkfs.ext4 -V 2>&1 | head -n 1 | cut -d' ' -f 2` | |
_MKFSEXT4VERMAJOR=`echo ${_MKFSEXT4VER} | cut -d'.' -f 1` | |
_MKFSEXT4VERMINOR=`echo ${_MKFSEXT4VER} | cut -d'.' -f 2` | |
if [[ ${_MKFSEXT4VERMAJOR} -gt 1 || ( ${_MKFSEXT4VERMAJOR} -eq 1 && ${_MKFSEXT4VERMINOR} -gt 42 ) ]]; then | |
NEWEXT4DEFAULTS=1 | |
fi | |
if [ ${#} -ne 1 ]; then | |
echo_usage | |
die 1 "no DEVICE specified" | |
fi | |
DEVICE=$1 | |
DEVICE_IS_MOUNTED=`grep ${DEVICE} /proc/self/mounts | wc -l` | |
if [ ${DEVICE_IS_MOUNTED} -ne 0 ]; then | |
die 1 "device is currently mounted" | |
fi | |
if [ ! -b "$DEVICE" ] ; then | |
die 1 "device is not a block device" | |
fi | |
SIZE=`fdisk -l $DEVICE | grep Disk | awk '{print $5}'` | |
echo | |
read -p "${DEVICE} is ${SIZE} bytes, continue to format it? [N,y]: " ANSWER | |
case ${ANSWER} in | |
y|Y|yes|Yes) | |
;; | |
*) | |
die 0 "user terminated" | |
;; | |
esac | |
# Blow away the master boot record and partition the disk | |
dd if=/dev/zero of=${DEVICE} bs=1024 count=1 | |
if [ ${NEWSFDISK} -eq 1 ]; then | |
{ | |
echo "label: dos" | |
echo "start=1MiB size=32MiB type=0x0C bootable" | |
echo "start=33MiB" | |
} | sfdisk ${DEVICE} | |
else | |
{ | |
echo 1,32,0x0C,* | |
echo 33,,,- | |
} | sfdisk ${DEVICE} | |
fi | |
# USB connected cards will have their partitions show up like /dev/sdc1 while | |
# real MMC devices will have their partitions show up like /dev/mmcblk0p1 | |
`echo "${DEVICE}" | grep "mmc"` && DEVICE="${DEVICE}p" | |
# Format the partitions, ensure the metadata_csum and 64bit options for ext4 are | |
# disabled as u-boot does not support them yet some versions of mkfs.ext4 will | |
# use them in the default set of features. | |
mkfs.vfat -n "BOOT" ${DEVICE}1 | |
if [ ${NEWEXT4DEFAULTS} -eq 1 ]; then | |
mkfs.ext4 -L "rootfs" -O ^metadata_csum,^64bit ${DEVICE}2 | |
else | |
mkfs.ext4 -L "rootfs" ${DEVICE}2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment