summaryrefslogtreecommitdiff
path: root/lx-trainer-vm/dd-multi.sh
diff options
context:
space:
mode:
Diffstat (limited to 'lx-trainer-vm/dd-multi.sh')
-rwxr-xr-xlx-trainer-vm/dd-multi.sh99
1 files changed, 99 insertions, 0 deletions
diff --git a/lx-trainer-vm/dd-multi.sh b/lx-trainer-vm/dd-multi.sh
new file mode 100755
index 0000000..b715681
--- /dev/null
+++ b/lx-trainer-vm/dd-multi.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+set -e
+
+usage_exit()
+{
+ echo
+ echo "usage: $0 src dest [dest]..."
+ echo
+ echo "example: copying image to devs 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
+
+SRC="$1"
+shift
+DESTS="$@"
+
+# 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
+
+# check if we are root (but keep going)
+if [ `id -u` -ne 0 ]; then
+ echo "warning: not root, may not work"
+fi
+
+# delete mbr
+for dest in $DESTS; do
+ dd if=/dev/zero of=$dest bs=1M count=1 &
+done
+wait
+
+partprobe
+
+# efficiently copy source to all destinations
+cat $SRC | tee $DESTS > /dev/null
+
+partprobe
+
+# add 2nd partition
+for dest in $DESTS; do
+ /bin/echo -e "n\np\n\n\n\nw" | fdisk $dest &
+done
+wait
+
+partprobe
+
+# create ext4 on 2nd partition
+for dest in $DESTS; do
+ mkfs.ext4 -L lxhome ${dest}2 &
+done
+wait
+
+# setup temp directory for mountpoints
+TMP_ROOT="/tmp/dd-multi-`date +%s`.$$"
+rm -rf $TMP_ROOT
+mkdir -p $TMP_ROOT
+echo "using temp directory $TMP_ROOT"
+
+for dest in $DESTS; do
+ # create mountpoints
+ mkdir -p ${TMP_ROOT}${dest}1
+ mkdir -p ${TMP_ROOT}${dest}2
+
+ # mount partitions
+ mount ${dest}1 ${TMP_ROOT}${dest}1
+ mount ${dest}2 ${TMP_ROOT}${dest}2
+
+ # move home directories to 2nd partition
+ mv ${TMP_ROOT}${dest}1/home/* ${TMP_ROOT}${dest}2/
+
+ # setup 2nd partition to be mounted as /home
+ echo 'LABEL=lxhome /home ext4 defaults 0 0' | \
+ tee -a ${TMP_ROOT}${dest}1/etc/fstab
+
+ # unmount partitions
+ umount ${TMP_ROOT}${dest}1
+ umount ${TMP_ROOT}${dest}2
+done
+
+# cleanup temp directory
+rm -rf $TMP_ROOT