Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Tune the root filesystem.
#!/bin/bash -e
#
# Goal:
# Tune the root filesystem.
#
# Notes:
# Not needed if building the RAID system.
#
# 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 $ROOT_PARTITION exist?"
[[ -e "$ROOT_PARTITION" ]]
echo "OK"
echo "Is card $SD_CARD not mounted?"
if df | grep --quiet "$SD_CARD"
then
echo "Yes: umount all partitions that belong to $SD_CARD."
df --exclude-type=tmpfs | awk --assign=PATTERN="$SD_CARD" '$1 ~ PATTERN {system("umount " $1)}'
# --exclude-type to shorten list
# --assign to pass SD_CARD from bash into awk script
# $1 ~ PATTERN act only on lines that contain SD_CARD, i.e. use awk to simulate grep
fi
echo "OK"
echo "Delete journal on partition $ROOT_PARTITION."
tune2fs -L "root" -O ^has_journal "$ROOT_PARTITION"
e2fsck -f -y "$ROOT_PARTITION"
echo "OK"
echo "Update fstab."
# mount the ROOT_PARTITION to a known place
ROOT_MOUNT=/tmp/root
mkdir --parents "$ROOT_MOUNT"
mount "$ROOT_PARTITION" "$ROOT_MOUNT"
# the extra spece in the pattern below is very relevant!
sed --in-place 's/noatime /noatime,nodiratime /' "$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