summaryrefslogtreecommitdiff
path: root/lx-trainer-vm
diff options
context:
space:
mode:
Diffstat (limited to 'lx-trainer-vm')
-rwxr-xr-xlx-trainer-vm/dd-multi.sh122
1 files changed, 63 insertions, 59 deletions
diff --git a/lx-trainer-vm/dd-multi.sh b/lx-trainer-vm/dd-multi.sh
index 01105aa..d31482b 100755
--- a/lx-trainer-vm/dd-multi.sh
+++ b/lx-trainer-vm/dd-multi.sh
@@ -27,6 +27,22 @@ if [ `id -u` -ne 0 ]; then
exit 1
fi
+if [ -z "`which mkexfatfs`" ]; then
+ echo "error: mkexfatfs program not found"
+ echo
+ echo "try this:"
+ echo "sudo apt install exfat-utils exfat-fuse"
+ exit 1
+fi
+
+if [ -z "`which qemu-img`" ]; then
+ echo "error: qemu-img program not found"
+ echo
+ echo "try this:"
+ echo "sudo apt install qemu-utils"
+ exit 1
+fi
+
# gather arguments
SRC=""
LAYERS=""
@@ -51,8 +67,8 @@ for arg in $@; do
esac
done
-# source must be file or block device
-if [ ! -f "$SRC" -a ! -b "$SRC" ]; then
+# source must be file
+if [ ! -f "$SRC" ]; then
echo "error: invalid src"
usage_exit
fi
@@ -80,19 +96,51 @@ if [ "`env - /sbin/parted --script $SRC print | \
usage_exit
fi
+SRC_FILE="${SRC}_copy.img"
+VMDK_FILE="${SRC}.vmdk"
+
+echo 'creating a sparse copy of the image'
+cp -a --sparse=always $SRC $SRC_FILE
+
+# 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"
+
+if [ -n "$LAYERS" ]; then
+ LOOPDEV=`losetup --show -P -f $SRC_FILE`
+ LOOPPT="${LOOPDEV}p${ROOTPT}"
+
+ mkdir -p ${TMP_ROOT}${LOOPPT}
+ mount $LOOPPT ${TMP_ROOT}${LOOPPT}
+
+ for tarball in $LAYERS; do
+ echo "unpacking layer: $tarball"
+ tar -x -f $tarball --numeric-owner -C ${TMP_ROOT}${LOOPPT}
+ done
+
+ sync
+ umount ${LOOPPT}
+
+ losetup -d $LOOPDEV
+fi
+
+echo 'creating vmdk file'
+qemu-img convert -O vmdk $SRC_FILE $VMDK_FILE
+
# delete mbr's
dd if=/dev/zero bs=10M count=1 2> /dev/null | tee $DESTS > /dev/null
+partprobe $DESTS
# 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'
+echo 'writing image to disks, monitor with: sudo kill -s USR1 `pidof dd`'
+dd if=$SRC_FILE bs=64M | tee $DESTS > /dev/null
-# fix partition table and add extra /home partition
+# fix partition table and add extra partition
for dest in $DESTS; do
- parted $dest print fix > /dev/null 2>&1
- parted --script --align optimal -- $dest mkpart /home 17GB -1MB
+ echo fix | sudo parted ---pretend-input-tty $dest print > /dev/null 2>&1
+ parted --script --align optimal -- $dest mkpart /extra ntfs 128GB 256GB
done
# rescan devices
@@ -112,66 +160,22 @@ for dest in $DESTS; do
sleep 1
done
done
+sleep 1
-# create ext4 on home partition
+# create exfat on extra partition
for dest in $DESTS; do
- mkfs.ext4 -L lxhome ${dest}${EXTRAPT} > /dev/null 2>&1 &
+ mkexfatfs ${dest}${EXTRAPT} > /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}${EXTRAPT}
-
- # mount partitions
- mount ${dest}${ROOTPT} ${TMP_ROOT}${dest}${ROOTPT}
mount ${dest}${EXTRAPT} ${TMP_ROOT}${dest}${EXTRAPT}
-
- # move home directories (if any) to home partition
- if [ -n "`ls ${TMP_ROOT}${dest}${ROOTPT}/home`" ]; then
- echo "moving /home/* to ${dest}${EXTRAPT}"
- mv ${TMP_ROOT}${dest}${ROOTPT}/home/* ${TMP_ROOT}${dest}${EXTRAPT}/
- fi
-
- # 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
+ cp -v $VMDK_FILE ${TMP_ROOT}${dest}${EXTRAPT}/training-hd.vmdk
+ sync
umount ${TMP_ROOT}${dest}${EXTRAPT}
- mount ${dest}${EXTRAPT} ${TMP_ROOT}${dest}${ROOTPT}/home
-done
-
-# unpack layers (in parallel)
-for tarball in $LAYERS; do
- echo "unpacking layer: $tarball"
- for dest in $DESTS; do
- tar -x -f $tarball --numeric-owner \
- -C ${TMP_ROOT}${dest}${ROOTPT} &
- done
- wait
-done
-
-for dest in $DESTS; do
- # If there were no layer files added, umount might return a busy
- # error since we just mounted. (Possibly a kernel bug.) By
- # accessing the filesytem before unmounting it, the mount/umount
- # problem seems to go away. Use "ls" to access the filesystem.
- ls ${TMP_ROOT}${dest}${ROOTPT}/home/ > /dev/null
-
- # unmount partitions
- umount ${TMP_ROOT}${dest}${ROOTPT}/home
- umount ${TMP_ROOT}${dest}${ROOTPT}
done
# cleanup temp directory
-rm -rf $TMP_ROOT
+rm -rf $TMP_ROOT $VMDK_FILE $SRC_FILE
echo 'done, no errors'