public
Last active — forked from fairchild/bundle-to-ebs.sh

A script to rsync a running Linux OS to an image file (specifically, a 10GB image file for deployment to AWS EC2).

  • Download Gist
syncFilesystemToImage.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#!/bin/bash
# Run this script on a running Linux 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/image'}
extraFilesArchive=${3:-'awsInstanceFiles.tar.gz'}
extraScript=${4:-'awsInstanceChanges.sh'}
 
echo "Creating empty 10GB image in ${imageFile}"
# Create an empty 10GB image file
dd if=/dev/zero of=${imageFile} bs=1M count=10240
 
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=/root/.bash_history --exclude=/home/*/.bash_history --exclude=/etc/ssh/ssh_host_* --exclude=/etc/ssh/moduli --exclude=/etc/udev/rules.d/*persistent-net.rules --exclude=/mnt/* --exclude=/proc/* --exclude=/tmp/* --exclude=/sys/* --exclude=/dev/* / ${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}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.