Skip to content

Instantly share code, notes, and snippets.

@kvvoff
Forked from geoffreyanderson/syncFilesystemToImage.sh
Last active May 24, 2018 05:49
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 kvvoff/7e00d3bf3620f9c693ea19f53ac08fda to your computer and use it in GitHub Desktop.
Save kvvoff/7e00d3bf3620f9c693ea19f53ac08fda to your computer and use it in GitHub Desktop.
A script to rsync a running Raspbian OS to an image file (specifically, a 2.5GB image file)
#!/bin/bash
# Run this script on a running Raspbian OS that you want to be put into an image file.
# Ensure that the system you run this on is less than 10GB in size if you wish to
# deploy the image file to AWS EC2.
# Note: This is based on Michael Fairchild's instance-to-ebs-ami.sh script.
# -https://gist.github.com/249915
imageFile=${1:-"awsImage-$(date +%m%d%y-%H%M).img"}
imageMountPoint=${2:-'/mnt/mounthdd'}
extraFilesArchive=${3:-'awsInstanceFiles.tar.gz'}
extraScript=${4:-'awsInstanceChanges.sh'}
echo "Creating empty 10GB image in ${imageFile}"
# Create an empty 2.5GB image file
dd if=/dev/mmcblk0 of=${imageFile} bs=1M count=2560
echo "Creating filesystem in ${imageFile}"
# Create a filesystem on the image file
/sbin/mke2fs -F -j ${imageFile}
echo "Mounting ${imageFile} loopback at ${imageMountPoint}"
# Create the directories needed for imaging
mkdir -p ${imageMountPoint}
mount -o loop ${imageFile} ${imageMountPoint}
echo "Beginning rsync..."
rsync --stats -av --exclude=/mnt/mounthdd/* / ${imageMountPoint}
echo "rsync finished. Flushing copied log data..."
#clear out any remaining log data
cd ${imageMountPoint}/var/log
for i in `ls ./**/*`; do
echo $i && echo -n> $i
done
if [[ -r "${extraFilesArchive}" ]]; then
#tar -pPxzvf ${extraFilesArchive}
echo "Extracting extra files in ${extraFilesArchive} to ${imageMountPoint}"
tar -pxzvf ${extraFilesArchive} -C ${imageMountPoint}
echo "Finished extracting extra files in ${extraFilesArchive} to ${imageMountPoint}"
fi
if [[ -x "${extraScript}" ]]; then
echo "Preparing to run ${extraScript} under chroot in ${imageMountPoint}"
# Strip the leading path from the file passed in.
scriptFileName=$(echo ${extraScript} | awk -F/ '{ print $NF }')
cp ${extraScript} ${imageMountPoint}
# Mount proc so we can chroot
mount -t proc none ${imageMountPoint}/proc
# chroot into the mount point for the image and run the bash script
# with additional changes.
chroot ${imageMountPoint} /bin/bash /${scriptFileName}
# Unmount /proc from the image mount point and remove the copied script.
umount ${imageMountPoint}/proc
rm -f ${imageMountPoint}/${scriptFileName}
echo "Finished running ${extraScript} under chroot in ${imageMountPoint}"
fi
# Create base devices (console, null, zero) under the completed image
for i in console null zero ; do MAKEDEV -d ${imageMountPoint}/dev -x $i ; done
# get out of the image mount point so we can successfully unmount
cd /
# Sync changes and unmount the image
sync
umount ${imageMountPoint}
rmdir ${imageMountPoint}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment