Skip to content

Instantly share code, notes, and snippets.

@ScottDillman
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottDillman/c929793cc4e3ec2a634e to your computer and use it in GitHub Desktop.
Save ScottDillman/c929793cc4e3ec2a634e to your computer and use it in GitHub Desktop.
#!/bin/bash
#title...........:raspi-fixup.sh
#description.....:mount raspbian image and fixup some files
#author..........:scott@bitwise.ninja
#date............:2015.07.20
#version.........:0.5
#usage...........:sudo bash raspi-fixup.sh
#notes...........:none
#==============================================================================
## make sure we sudo
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
## get raspbian image file name
pi_img=$(ls -t *.img | head -1 )
echo "Image found: " $pi_img
## get start sector
start_sector=$(fdisk -lu $pi_img | sed ':a;N;$!ba;s/\n/ /g' | sed -E 's/.*img2[ \t]+([0-9]+).*/\1/')
## calculate offset
let offset="$start_sector * 512"
echo "start sector: " $start_sector
echo "offset: " $offset
## mount the image
mkdir -p mnt
mount $pi_img -o offset=$offset ./mnt
## fixup the storage
sed -r -i.bak 's/mmcblk0p?/sda/g' ./mnt/etc/fstab
## fixup preload
sed -r -i.bak 's/^/#/g' ./mnt/etc/ld.so.preload
## create udev rules
echo 'KERNEL=="sda", SYMLINK+="mmcblk0"\nKERNEL=="sda?", SYMLINK+="mmcblk0p%n",' > ./mnt/etc/udev/rules.d/90-qemu.rule
## fixup network interfaces
sed -r -i.bak 's/iface eth0 inet manual/iface eth0 inet dhcp/g' ./mnt/etc/network/interfaces
## fixup the config script
sed -r -i.bak 's/mmcblk0p?/sda/g' ./mnt/usr/bin/raspi-config
## cleanup
umount mnt
rmdir mnt
## expand image
qemu-img resize $pi_img +2G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment