summaryrefslogtreecommitdiff
path: root/lx-trainer-vm/dd-multi.sh
blob: 2af37986ced9a5b875ac8dc2a6debde0524e067f (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/sh
set -e

usage_exit()
{
	echo
	echo "usage: $0 src [--layer=tarball]... 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
}

if [ `id -u` -ne 0 ]; then
	echo "error: this script must be run as root"
	exit 1
fi

# minimum 2 arguments
if [ $# -lt 2 ]; then
	echo "error: invalid arguments"
	usage_exit
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

# 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

	# copy layer tarballs to /home
	for tarball in $LAYERS; do
		destfile="${TMP_ROOT}${dest}2/$tarball"
		if [ -f $destfile ]; then
			echo
			echo "WARNING: OVERWRITING $destfile" 1>&2
			echo
			sleep 1
		fi
		cp $tarball $destfile
	done

	# remount home to /home
	umount ${TMP_ROOT}${dest}2
	mount ${dest}2 ${TMP_ROOT}${dest}1/home

	# unpack layers
	for tarball in $LAYERS; do
		chroot ${TMP_ROOT}${dest}1 tar -x -f /home/$tarball --numeric-owner -C /
		rm ${TMP_ROOT}${dest}1/home/$tarball
	done

	# unmount partitions
	umount ${TMP_ROOT}${dest}1/home
	umount ${TMP_ROOT}${dest}1
done

# cleanup temp directory
rm -rf $TMP_ROOT