summaryrefslogtreecommitdiff
path: root/lx-trainer-vm/dd-multi.sh
blob: 4dbaac7424651fb555d4b134aea82b0265a5fc52 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/sh
set -e

# This script only supports GPT partitions for the lx-trainer image.
ROOTPT=3
EXTRAPT=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
	echo "    use --keep-files to speed up multiple calls"
	echo "    (so that vmdk and img can be re-used later)"
	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

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=""
DESTS=""

KEEP_FILES=0
for arg in $@; do
	case $arg in
	--layer=*)
		LAYERS="$LAYERS `echo $arg | sed -e 's/^--layer=//'`"
		;;
	--keep-files)
		KEEP_FILES=1
		;;
	--*)
		echo "error: unknown option: $arg"
		usage_exit
		;;
	*)
		if [ -z "$SRC" ]; then
			SRC="$arg"
		else
			DESTS="$DESTS $arg"
		fi
		;;
	esac
done

# source must be file
if [ ! -f "$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

# all layers must be available
for tarball in $LAYERS; do
	if [ ! -f $tarball ]; then
		echo "error: missing layer file: $tarball"
		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

SRC_FILE="${SRC}_copy.img"
VMDK_FILE="${SRC_FILE}.vmdk"

# 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 [ $KEEP_FILES -eq 1 -a -f $SRC_FILE ]; then
	echo 'using existing prepared copy of the image'
	if [ -n "$LAYERS" ]; then
		echo '(SPECIFIED LAYERS WILL NOT BE RE-UNPACKED)'
	fi
else
	echo 'creating a sparse copy of the image'
	cp -a --sparse=always $SRC $SRC_FILE

	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
fi

if [ $KEEP_FILES -eq 1 -a -f $VMDK_FILE ]; then
	echo 'using existing prepared vmdk file'
else
	echo 'creating vmdk file'
	qemu-img convert -O vmdk $SRC_FILE $VMDK_FILE
fi

# 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 'writing image to disks (64GiB)'
dd if=$SRC_FILE bs=128M status=progress | tee $DESTS > /dev/null

# fix partition table and add extra partition
for dest in $DESTS; do
	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
while ! partprobe $DESTS 2> /dev/null; do
	echo "waiting for partprobe..."
	sleep 1
done
sleep 1

# 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}${EXTRAPT} ]; do
		echo "waiting for ${dest}${EXTRAPT}..."
		sleep 1
	done
done
sleep 1

# create exfat on extra partition
for dest in $DESTS; do
	mkexfatfs ${dest}${EXTRAPT} > /dev/null 2>&1
done

for dest in $DESTS; do
	mkdir -p ${TMP_ROOT}${dest}${EXTRAPT}
	mount ${dest}${EXTRAPT} ${TMP_ROOT}${dest}${EXTRAPT}
	cp -v $VMDK_FILE ${TMP_ROOT}${dest}${EXTRAPT}/training-hd.vmdk &
done
wait
sync

for dest in $DESTS; do
	umount ${TMP_ROOT}${dest}${EXTRAPT}
done

# cleanup temp directory
rm -rf $TMP_ROOT
if [ $KEEP_FILES -ne 1 ]; then
	rm -f $VMDK_FILE $SRC_FILE
else
	echo "as requested, not removing:"
	echo "     $VMDK_FILE"
	echo "     $SRC_FILE"
fi

echo 'done, no errors'