Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #
- # Copyright (c) 2012 Igalia, S.L.
- #
- # Author: Víctor Jáquez <vjaquez@igalia.com>
- #
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License as
- # published by the Free Software Foundation; either version 2 of the
- # License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- # USA
- #
- set -e
- trap cleanup 1 2 3 6 15
- WDIR="$(pwd)"
- PROGRAM="$(basename $0)"
- IMAGE="${WDIR}/rootfs.squashfs"
- FQDN="panda1"
- usage()
- {
- cat <<EOF
- Usage: $PROGRAM -f <hostname> [OPTION]...
- Sync the squashfs image ($IMAGE)
- to the root filesystem in a remote system.
- Options:
- Required:
- -f --fqdn <hostname>
- Hostname to be used for the target system
- Additional:
- -h --help
- Display this help
- -n --dry-run
- Test the rsync command
- EOF
- exit 0
- }
- cleanup()
- {
- echo "Cleaning up..."
- umount_image
- rm -rf $WORKDIR
- exit 0
- }
- mount_image()
- {
- if [ ! -d $MOUNTPOINT ]; then
- mkdir -p $MOUNTPOINT
- fi
- mount -o ro,loop $IMAGE $MOUNTPOINT
- }
- umount_image()
- {
- if grep -q ${MOUNTPOINT} /proc/mounts; then
- echo "Umounting temporary Image"
- umount -l $MOUNTPOINT
- fi
- }
- sync_image()
- {
- RSYNCOPTS=-"auvHx --delete --super --progress -e ssh --exclude-from=${WORKDIR}/excludes.files"
- EXTRAOPTS=""
- if [ "${DRYRUN}" ]; then
- EXTRAOPTS="--dry-run"
- fi
- cd ${MOUNTPOINT}
- rsync ${RSYNCOPTS} ${EXTRAOPTS} ./ ${FQDN}:/
- cd -
- }
- # we need root
- if [ $(id -u) != 0 ]; then
- echo "You should run $PROGRAM as root"
- exit 2
- fi
- if [ ! -f $IMAGE ]; then
- echo "There's no image file"
- exit 2
- fi
- # parse commandline options
- GETOPT=`getopt -o hf:n --long help,fqdn:,dry-run -n "$PROGRAM" -- "$@"`
- if [ $? != 0 ]; then usage; exit 1; fi
- eval set -- "$GETOPT"
- while true ; do
- case $1 in
- -h|--help)
- usage
- shift
- ;;
- -f|--fqdn)
- FQDN="$2"
- shift 2
- ;;
- -n|--dry-run)
- DRYRUN=1
- shift
- ;;
- --)
- shift
- break
- ;;
- *)
- echo "Internal error!";
- exit 1
- ;;
- esac
- done
- WORKDIR=$(mktemp -d --tmpdir=${WDIR})
- MOUNTPOINT="${WORKDIR}/rootfs"
- cat > ${WORKDIR}/excludes.files <<EOF
- /usr/local/
- /lib/modules/
- /home/
- /root/
- /etc/dropbear/
- /etc/init.d/*
- /etc/ld.so.conf.d/*
- /etc/network/interfaces
- /etc/network/if-up.d/ntpdate
- /etc/*-
- /etc/group
- /etc/hostname
- /etc/hosts
- /etc/inittab
- /etc/ld.so.cache
- /etc/ld.so.conf
- /etc/resolv.conf
- EOF
- mount_image
- sync_image
- cleanup
Add Comment
Please, Sign In to add comment