Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Prepare empty space on the card to be used as /app partition.
#!/bin/bash -e
#
# Goal:
# Prepare empty space on the card to be used as /app partition.
#
# Notes:
# After this script, first time the Raspberry is powered on, you will see an error:
# "Could not expand filesystem, please try raspi-config or rc_gui."
# This error can be safely ignored, or it can be turned off using the sed command from line 132 in
# file: $ROOT_PARTITION/usr/lib/raspi-config/init_resize.sh
#
# Prerequisites:
# 1. Run 01-write_image_to_card.
# 2. Set the following variables:
#
SD_CARD=/dev/mmcblk0
#BOOT_PARTITION=${SD_CARD}p1
ROOT_PARTITION=${SD_CARD}p2
APP_PARTITION=${SD_CARD}p3
echo "Are we superuser?"
[[ $EUID -eq 0 ]]
echo "OK"
echo "Is card $SD_CARD plugged in?"
[[ -e "$SD_CARD" ]]
echo "OK"
echo "Does partition $APP_PARTITION not exist?"
# partprobe is not 100% reliable; http://serverfault.com/a/36047/278025
partprobe $SD_CARD
[[ ! -e "$APP_PARTITION" ]]
echo "OK"
echo "Create new partition $APP_PARTITION."
# Find the start and end offsets. There are 2 blocks of "Free Space" and we only care about the last one.
OFFSETS=$(parted --script "$SD_CARD" print free | awk '/Free Space/ {print $1 " " $2}' | tail -1)
# do not quote OFFSETS - it holds two parameters
parted --script "$SD_CARD" mkpart primary $OFFSETS
echo "OK"
echo "Format partition $APP_PARTITION."
mkfs.ext2 -L "app" "$APP_PARTITION"
echo "OK"
echo "Update fstab."
if df | grep --quiet "$ROOT_PARTITION"
then
umount "$ROOT_PARTITION"
fi
# mount the ROOT_PARTITION to a known place
ROOT_MOUNT=/tmp/root
mkdir --parents "$ROOT_MOUNT"
mount "$ROOT_PARTITION" "$ROOT_MOUNT"
printf "$APP_PARTITION\t/app\text2\tdefaults,noatime,nodiratime\t0\t2\n" >> "$ROOT_MOUNT/etc/fstab"
umount "$ROOT_MOUNT"
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment