#!/bin/sh set -e # This script only supports GPT partitions for the lx-trainer image. ROOTPT=3 HOMEPT=4 usage_exit() { echo echo "usage: sudo $0 src [--layer=tarball]... dest [dest]..." echo echo "example: copying image to devices: sde, sdf, sdg" echo "$0 lx-trainer.img /dev/sde /dev/sdf /dev/sdg" echo exit 1 } # minimum 2 arguments if [ $# -lt 2 ]; then echo "error: invalid arguments" usage_exit fi if [ `id -u` -ne 0 ]; then echo "error: sorry, this script must be run as root" exit 1 fi # gather arguments SRC="" LAYERS="" DESTS="" for arg in $@; do case $arg in --layer=*) LAYERS="$LAYERS `echo $arg | sed -e 's/^--layer=//'`" ;; --*) echo "error: unknown option: $arg" usage_exit ;; *) if [ -z "$SRC" ]; then SRC="$arg" else DESTS="$DESTS $arg" fi ;; esac done # source must be file or block device if [ ! -f "$SRC" -a ! -b "$SRC" ]; then echo "error: invalid src" usage_exit fi # destination must be block device for dest in $DESTS; do if [ ! -b $dest ]; then echo "error: invalid dest: $dest" usage_exit fi done # detect partion type of source if [ "`env - /sbin/parted --script $SRC print | \ grep 'Partition Table' | awk '{print $3}'`" != "gpt" ]; then echo "error: only gpt partition tables supported" usage_exit fi # delete mbr's dd if=/dev/zero bs=10M count=1 2> /dev/null | tee $DESTS > /dev/null # efficiently copy source to all destinations and rescan devices echo 'monitor copy status with: sudo kill -s USR1 `pidof dd`' dd if=$SRC bs=64M | tee $DESTS > /dev/null echo 'setting up /home partition' # fix partition table and add extra /home partition for dest in $DESTS; do parted $dest print fix > /dev/null 2>&1 parted --script --align optimal -- $dest mkpart /home 17GB -1MB done # rescan devices while ! partprobe $DESTS 2> /dev/null; do echo "waiting for partprobe..." sleep 1 done # wait for partitions to appear for dest in $DESTS; do while [ ! -b ${dest}${ROOTPT} ]; do echo "waiting for ${dest}${ROOTPT}..." sleep 1 done while [ ! -b ${dest}${HOMEPT} ]; do echo "waiting for ${dest}${HOMEPT}..." sleep 1 done done # create ext4 on home partition for dest in $DESTS; do mkfs.ext4 -L lxhome ${dest}${HOMEPT} > /dev/null 2>&1 & done wait # setup temp directory for mountpoints TMP_ROOT="/tmp/dd-multi-`date +%s`.$$" rm -rf $TMP_ROOT mkdir -p $TMP_ROOT echo "using directory $TMP_ROOT for mount points" for dest in $DESTS; do # create mountpoints mkdir -p ${TMP_ROOT}${dest}${ROOTPT} mkdir -p ${TMP_ROOT}${dest}${HOMEPT} # mount partitions mount ${dest}${ROOTPT} ${TMP_ROOT}${dest}${ROOTPT} mount ${dest}${HOMEPT} ${TMP_ROOT}${dest}${HOMEPT} # move home directories to home partition echo "moving /home/* to ${dest}${HOMEPT}" mv ${TMP_ROOT}${dest}${ROOTPT}/home/* ${TMP_ROOT}${dest}${HOMEPT}/ # setup home partition to be mounted as /home echo 'LABEL=lxhome /home ext4 defaults 0 0' | \ tee -a ${TMP_ROOT}${dest}${ROOTPT}/etc/fstab > /dev/null # remount home to /home umount ${TMP_ROOT}${dest}${HOMEPT} mount ${dest}${HOMEPT} ${TMP_ROOT}${dest}${ROOTPT}/home # unpack layers for tarball in $LAYERS; do echo "unpacking $tarball to $dest" tar -x -f $tarball --numeric-owner \ -C ${TMP_ROOT}${dest}${ROOTPT} done # unmount partitions umount ${TMP_ROOT}${dest}${ROOTPT}/home umount ${TMP_ROOT}${dest}${ROOTPT} done # cleanup temp directory rm -rf $TMP_ROOT echo "done, no errors"