about summary refs log tree commit diff
path: root/mklive.sh.in
blob: e145fd4b6a2e975904f6641e8ef9c0eae899d356 (plain) (blame)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/bin/sh
#-
# Copyright (c) 2009-2012 Juan Romero Pardines.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#-
trap "echo; error_out $?" INT QUIT

info_msg() {
	printf "\033[1m$@\n\033[m"
}

mount_pseudofs() {
	local fs

	for fs in sys proc dev; do
		if [ ! -d "$ROOTFS/$fs" ]; then
			mkdir -p "$ROOTFS/$fs"
		fi
		mount --bind /$fs "$ROOTFS/$fs" || error_out $?
	done
}

umount_pseudofs() {
	local fs

	for fs in sys proc dev; do
		umount -f "$ROOTFS/$fs" >/dev/null 2>&1
	done
}

error_out() {
	umount_pseudofs

	info_msg "There was an error! cleaning up $BUILDDIR, exiting..."

	[ -d "$BUILDDIR" ] && rm -rf "$BUILDDIR"
	[ -f "$LOGFILE" ] && rm -f "$LOGFILE"

	exit 1
}

write_etc_motd() {
	cat >> "$ROOTFS/etc/motd" <<_EOF
###############################################################################
  Autogenerated by void-mklive @@MKLIVE_VERSION@@
-------------------------------------------------------------------------------

Welcome to the Void Linux Live system, you have been autologged in.
This user has full sudo(8) permissions without any password, be careful
executing commands through sudo(8).

To play with package management use the xbps-bin(8) and xbps-repo(8)
utilities. Please visit:

	http://code.google.com/p/xbps/

for more information and/or documentation about using the X Binary
Package System. If you think it is useful, please make a donation
to improve further development from the above URL, thanks.

To start the installation please type:

	$ sudo void-installer

and follow the on-screen instructions. Thanks for trying Void Linux.

###############################################################################
_EOF
}

write_conf_file() {
	cat > "$1" <<_EOF
# *-*- sh -*-*
# Default configuration file for vmklive-@VERSION@.
#
# List of packages to be installed into the live image.
# At least 'base-system' or 'base-system-live' is required.
PACKAGE_LIST="base-system-live"

# Syslinux splash image.
SPLASH_IMAGE=/usr/share/void-artwork/splash.png

# Default keymap to use.
KEYMAP=us

# Default locale to use.
LOCALE=en_US

# Path to XBPS utilities.
#XBPS_BIN_CMD=xbps-bin
#XBPS_REPO_CMD=xbps-repo
#XBPS_UHELPER_CMD=xbps-uhelper

# XBPS cache directory to install packages from.
#REPOSITORY_CACHE=/blah/foo

_EOF
	chmod 644 "$1"
}

usage()
{
	cat <<_EOF
Usage: $(basename $0) [options]

Options:
 -C file		Path to configuration file (defaults to ~/.mklive.conf)
 -c (gzip|bzip2|xz) 	Compression type for the squashfs/initramfs image.
 -k version		Kernel version to use.
 -o outfile		Output file name for the ISO image.
 -s splash		Splash image file for isolinux.
_EOF
	exit 1
}

#
# main()
#
while getopts "C:c:k:o:s:h" opt; do
	case $opt in
	C) CONFIG_FILE="$OPTARG";;
	c) COMPRESSTYPE="$OPTARG";;
	k) KERNELVERSION="$OPTARG";;
	o) OUTPUT_FILE="$OPTARG";;
	s) SPLASH_IMAGE="$OPTARG";;
	h) usage;;
	esac
done
shift $(($OPTIND - 1))

if [ -z "$KERNELVERSION" ]; then
	KERNELVERSION="$(uname -r)"
fi

# Set defaults
if [ -z "$CONFIG_FILE" ]; then
	CONFIG_FILE="$HOME/.mklive.conf"
fi
if [ -z "$OUTPUT_FILE" ]; then
	OUTPUT_FILE="$HOME/void-live-$(uname -m)-${KERNELVERSION}-$(date +%Y%m%d).iso"
fi
LOGFILE="$(mktemp -t vmklive-XXXXXXXXXX.log)"

if [ -z "$SYSLINUX_DATADIR" ]; then
	SYSLINUX_DATADIR=/usr/share/syslinux
fi
if [ -z "$GRUB_DATADIR" ]; then
	GRUB_DATADIR=/usr/share/grub
fi
if [ -z "$MKLIVE_DATADIR" ]; then
	MKLIVE_DATADIR=/usr/share/void-mklive
fi
if [ -z "$SPLASH_IMAGE" ]; then
	SPLASH_IMAGE=/usr/share/void-artwork/splash.png
fi
if [ -z "$XBPS_REPO_CMD" ]; then
	XBPS_REPO_CMD=xbps-repo
fi
if [ -z "$XBPS_BIN_CMD" ]; then
	XBPS_BIN_CMD=xbps-bin
fi
if [ -z "$XBPS_UHELPER_CMD" ]; then
	XBPS_UHELPER_CMD=xbps-uhelper
fi
if [ -z "$COMPRESSTYPE" ]; then
	COMPRESSTYPE=xz
fi

# Create or read configuration file.
if [ ! -r $CONFIG_FILE ]; then
	info_msg "Creating config file at $CONFIG_FILE."
	write_conf_file $CONFIG_FILE
fi

. $CONFIG_FILE

if [ -z "$PACKAGE_LIST" ]; then
	PACKAGE_LIST="base-system-live"
else
	PACKAGE_LIST="$PACKAGE_LIST"
fi
if [ ! -f $SYSLINUX_DATADIR/isolinux.bin ]; then
	echo "Missing required isolinux files in $SYSLINUX_DATADIR, exiting..."
	exit 1
fi

# Check for root permissions.
if [ "$(id -u)" -ne 0 ]; then
	echo "Must be run as root, exiting..."
	exit 1
fi

ISO_VOLUME="VOID_LIVE"
BUILDDIR=$(mktemp --tmpdir=$HOME -d) || exit 1
BUILDDIR=$(readlink -f $BUILDDIR)
ROOTFS="$BUILDDIR/rootfs"
BOOT_DIR="$BUILDDIR/boot"
ISOLINUX_DIR="$BOOT_DIR/isolinux"
GRUB_DIR="$BOOT_DIR/grub"
ISOLINUX_CFG="$ISOLINUX_DIR/isolinux.cfg"

#
# Check there are repos registered before anything.
#
${XBPS_REPO_CMD} list >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "No repositories available, exiting..."
	error_out
fi

#
# Mount pseudofs in the target rootfs.
#
mount_pseudofs
mkdir -p "$ROOTFS/tmp" "$ISOLINUX_DIR" "$GRUB_DIR"

XBPS_ARGS="-r $ROOTFS -y"
if [ -n "$REPOSITORY_CACHE" ]; then
	XBPS_ARGS="$XBPS_ARGS -c $REPOSITORY_CACHE"
fi
XBPS_VERSION=$($XBPS_BIN_CMD -V|awk '{print $2}')
case $XBPS_VERSION in
	# XBPS >= 0.16.6
	[0-9].[1-9][6-9].[6-9]*) XBPS_016_6=1;;
esac

info_msg "Redirecting stdout/stderr to $LOGFILE ..."
info_msg "[1/10] Installing packages into the rootfs..."
for f in ${PACKAGE_LIST}; do
	info_msg "  $f"
done
# Check that all pkgs are reachable.
${XBPS_BIN_CMD} ${XBPS_ARGS} -n install ${PACKAGE_LIST} >>$LOGFILE 2>&1
if [ $? -ne 0 ]; then
	info_msg "Missing required binary packages, exiting..."
	error_out
fi
${XBPS_BIN_CMD} ${XBPS_ARGS} install ${PACKAGE_LIST} \
	2>&1|cat >> $LOGFILE || error_out
${XBPS_BIN_CMD} ${XBPS_ARGS} autoupdate \
	2>&1|cat >> $LOGFILE || error_out
${XBPS_BIN_CMD} ${XBPS_ARGS} autoremove \
	2>&1|cat >> $LOGFILE || error_out

${XBPS_BIN_CMD} -r "$ROOTFS" list > \
	"${OUTPUT_FILE%.iso}"-package-list.txt || error_out

#
# Prepare /etc/motd.
#
info_msg "[2/10] Creating /etc/motd..."
mkdir -p "$ROOTFS"/etc
write_etc_motd

#
# Create the initramfs with XZ compression.
#
info_msg "[3/10] Creating initramfs image ($COMPRESSTYPE)..."
dracut --no-hostonly --add "dmsquash-live vmklive" --${COMPRESSTYPE} \
	"${BOOT_DIR}/initrd.lz" ${KERNELVERSION} 2>/dev/null || error_out

#
# Copy the linux image to the target directory.
#
info_msg "[4/10] Copying kernel image/modules..."
cp -f /boot/vmlinuz-${KERNELVERSION} "${BOOT_DIR}/vmlinuz" || error_out $?
mkdir -p "$ROOTFS/lib/modules"
cp -a /lib/modules/${KERNELVERSION} "$ROOTFS/lib/modules" || error_out $?

# Generate a sane xbps.conf for the rootfs.
rm -f $ROOTFS/etc/xbps/xbps.conf
echo "# xbps.conf generated by void-mklive-@@MKLIVE_VERSION@@" \
	> $ROOTFS/etc/xbps/xbps.conf
echo "TransactionFrequencyFlush = 0" \
	>> $ROOTFS/etc/xbps/xbps.conf
echo "virtual-package rsyslog { targets = syslog-daemon-0 }" \
	>> $ROOTFS/etc/xbps/xbps.conf
echo "virtual-package dcron { targets = cron-daemon-0 }" \
	>> $ROOTFS/etc/xbps/xbps.conf
echo "virtual-package kmod { targets = module-init-tools-3.17 }" \
	>> $ROOTFS/etc/xbps/xbps.conf
_devel=$($XBPS_UHELPER_CMD -r $ROOTFS version xbps-devel)
if [ -n "${_devel}" ]; then
	echo "virtual-package xbps-devel { targets = xbps-9999 }" \
		>> $ROOTFS/etc/xbps/xbps.conf
fi

# Generate a conf for local repositories.
cat > $ROOTFS/etc/xbps/local-repos.conf <<_EOF
repositories = {
	# XBPS >= 0.16
	/packages,
}
_EOF
# Generate a conf for remote repositories.
cat > $ROOTFS/etc/xbps/network-repos.conf <<_EOF
repositories = {
	# XBPS >= 0.16
	http://xbps.hosting-unlimited.org/binpkgs,
	http://xbps.goodluckwith.us/binpkgs,
	http://xbps.nopcode.org/repos/current,
}
_EOF
chmod 644 $ROOTFS/etc/xbps/*.conf || error_out $?

# Create local repos for base-system and grub-x86_64-efi packages required by
# the void-installer pkg.
pkgs=$($XBPS_BIN_CMD -r /tmp/blah -n install base-system grub-x86_64-efi)
set -- ${pkgs}
while [ $# -ne 0 ]; do
	pkgn=$1; action=$2; ver=$3; repo=$4; binpkg=$5; arch=$6
	shift 6
	mkdir -p $ROOTFS/packages/$arch
	bpkg=$repo/$arch/$binpkg
	cp -f $bpkg $ROOTFS/packages/$arch
	ln -sfr $ROOTFS/packages/$arch/$binpkg $ROOTFS/packages/$binpkg
done
if [ -n "$XBPS_016_6" ]; then
	$XBPS_REPO_CMD index-add $ROOTFS/packages/*.xbps 2>&1 >>$LOGFILE
	rm -f $ROOTFS/packages/index-files.plist
else
	${XBPS_REPO_CMD} genindex $ROOTFS/packages 2>&1 >>$LOGFILE
	rm -f $ROOTFS/packages/index-files.plist
fi

# install lsblk and blkid from util-linux. to avoid installing
# the whole package.
_lsblk=$(which lsblk)
_blkid=$(which blkid)
install -Dm755 ${_lsblk} "$ROOTFS/usr/bin/lsblk" || error_out $?
install -Dm755 ${_blkid} "$ROOTFS/usr/sbin/blkid" || error_out $?

# install mount from util-linux.
install -Dm755 /bin/mount "$ROOTFS/usr/bin/mount" || error_out $?

#
# The pseudofs aren't needed anymore in target rootfs.
#
umount_pseudofs

#
# Prepare isolinux files in the target rootfs.
#
info_msg "[5/10] Preparing isolinux support for BIOS..."
cp -f $SYSLINUX_DATADIR/isolinux.bin "$ISOLINUX_DIR" || error_out $?
cp -f $SYSLINUX_DATADIR/vesamenu.c32 "$ISOLINUX_DIR" || error_out $?
cp -f $MKLIVE_DATADIR/isolinux.cfg.in \
	"$ISOLINUX_DIR"/isolinux.cfg || error_out $?

if [ -f "$SPLASH_IMAGE" ]; then
	cp -f $SPLASH_IMAGE "$ISOLINUX_DIR" || error_out $?
fi
sed -i  -e "s|@@SPLASHIMAGE@@|$(basename $SPLASH_IMAGE)|" \
	-e "s|@@KERNVER@@|${KERNELVERSION}|" \
	-e "s|@@KEYMAP@@|${KEYMAP}|" \
	-e "s|@@ARCH@@|$(uname -m)|" \
	-e "s|@@LOCALE@@|${LOCALE}|" $ISOLINUX_DIR/isolinux.cfg

#
# Prepare grub files for EFI.
#
info_msg "[6/10] Preparing GRUB support for EFI..."
cp -f $MKLIVE_DATADIR/grub.cfg $GRUB_DIR || error_out $?
cp -f $MKLIVE_DATADIR/grub_void.cfg.in $GRUB_DIR/grub_void.cfg || error_out $?
sed -i  -e "s|@@SPLASHIMAGE@@|$(basename $SPLASH_IMAGE)|" \
	-e "s|@@KERNVER@@|${KERNELVERSION}|" \
	-e "s|@@KEYMAP@@|${KEYMAP}|" \
	-e "s|@@ARCH@@|$(uname -m)|" \
	-e "s|@@LOCALE@@|${LOCALE}|" $GRUB_DIR/grub_void.cfg
mkdir -p $GRUB_DIR/fonts $GRUB_DIR/locale || error_out $?
cp -f $GRUB_DATADIR/unicode.pf2 $GRUB_DIR/fonts || error_out $?
cp -f /boot/grub/locale/* $GRUB_DIR/locale || error_out $?

# Create EFI vfat image.
dd if=/dev/zero of=$GRUB_DIR/efiboot.img bs=1024 count=4096 \
	2>&1 | cat >>$LOGFILE || error_out $?
mkfs.vfat -F12 -S 512 -n "grub_uefi" "$GRUB_DIR/efiboot.img" \
	2>&1 | cat >>$LOGFILE || error_out $?

GRUB_EFI_TMPDIR="$(mktemp --tmpdir=$HOME -d)"
LOOP_DEVICE="$(losetup --show --find ${GRUB_DIR}/efiboot.img)"
mount -o rw,flush -t vfat "${LOOP_DEVICE}" "${GRUB_EFI_TMPDIR}" \
	2>&1 | cat >>$LOGFILE || error_out $?

mkdir -p "${GRUB_EFI_TMPDIR}/EFI/boot/" || error_out $?
cd "$BUILDDIR" || error_out $?
grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi" --format="x86_64-efi" \
	        --compression="xz" --output="${GRUB_EFI_TMPDIR}/EFI/boot/bootx64.efi" \
		        "boot/grub/grub.cfg" 2>&1 | cat >>$LOGFILE || error_out $?
umount "$GRUB_EFI_TMPDIR" || error_out $?
losetup --detach "${LOOP_DEVICE}" || error_out $?
rm -rf $GRUB_EFI_TMPDIR || error_out $?

#
# Prepare the squashed rootfs image.
#
info_msg "[7/10] Creating squashfs image ($COMPRESSTYPE) from rootfs..."
# Find out required size for the rootfs and create an ext3fs image off it.
ROOTFS_SIZE=$(du -sk "$ROOTFS"|awk '{print $1}')
mkdir -p "$BUILDDIR/tmp/LiveOS"
dd if=/dev/zero of="$BUILDDIR/tmp/LiveOS/ext3fs.img" \
	bs="$((${ROOTFS_SIZE}+($ROOTFS_SIZE/6)))K" count=1 2>&1 | cat >>$LOGFILE || error_out $?
mkdir -p "$BUILDDIR/tmp-rootfs"
mkfs.ext3 -F -m1 "$BUILDDIR/tmp/LiveOS/ext3fs.img" 2>&1 | cat >>$LOGFILE || error_out $?
mount -o loop "$BUILDDIR/tmp/LiveOS/ext3fs.img" "$BUILDDIR/tmp-rootfs" || error_out $?
cd $BUILDDIR
cp -a rootfs/* tmp-rootfs/
umount -f "$BUILDDIR/tmp-rootfs"
mkdir -p "$BUILDDIR/LiveOS"

mksquashfs "$BUILDDIR/tmp" "$BUILDDIR/LiveOS/squashfs.img" \
	-comp ${COMPRESSTYPE} 2>&1 | cat >> $LOGFILE || error_out
chmod 444 "$BUILDDIR/LiveOS/squashfs.img" || error_out $?

info_msg "[8/10] Removing rootfs directory..."
rm -rf "$ROOTFS" "$BUILDDIR/tmp-rootfs" "$BUILDDIR/tmp" || error_out $?

#
# Prepare the ISO image.
#
info_msg "[9/10] Building ISO image..."
xorriso -as mkisofs \
	-iso-level 3 -rock -joliet \
	-max-iso9660-filenames -omit-period \
	-omit-version-number -relaxed-filenames -allow-lowercase \
	-volid "VOID_LIVE" \
	-eltorito-boot boot/isolinux/isolinux.bin \
	-eltorito-catalog boot/isolinux/boot.cat \
	-no-emul-boot -boot-load-size 4 -boot-info-table \
	-eltorito-alt-boot --efi-boot boot/grub/efiboot.img -no-emul-boot \
	-isohybrid-mbr /usr/share/syslinux/isohdpfx.bin \
	-output "$OUTPUT_FILE" "$BUILDDIR" 2>&1 | cat >>$LOGFILE || error_out $?

info_msg "[10/10] Removing build directory..."
rm -rf "$BUILDDIR" || error_out $?

hsize=$(du -sh "$OUTPUT_FILE"|awk '{print $1}')
info_msg "Created $(readlink -f $OUTPUT_FILE) ($hsize) successfully."

exit 0