aboutsummaryrefslogtreecommitdiffstats
path: root/setup-bootable-usb.in
blob: 32d40b22e6bd7e019c4fc2e75893bce9706f3939 (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
#!/bin/sh

cleanup() {
	if [ "$install_syslinux" = "yes" ]; then
		apk del -q syslinux
	fi
	if [ "$mount_dest" = "yes" ]; then
		umount "$dest"
	fi
}

die() {
	echo "$@" >&2
	cleanup
	exit 1
}

apk info -q -e syslinux || install_syslinux=yes


if [ "$install_syslinux" = "yes" ]; then
	apk add -q syslinux
fi

src=${1:-/media/cdrom}

[ -f "$src"/.alpine-release ] || die "$src/.alpine-release not found"


dest=${2:-/media/usb}

if ! awk '{print $2}' /proc/mounts | grep -q "^$dest\$"; then
	mount "$dest" || die "Failed to mount $dest"
	mount_dest=yes
fi

echo "Copying files..."
cp -r "$src"/* "$src"/.[a-z]* "$dest" || die "Failed to copy files"

echo "Making usb bootable..."
dev=$(awk "\$2 == \"$dest\" {print \$1}" /proc/mounts)
parent=$(basename $(dirname /sys/block/*/$(basename $dev)))

syslinux $dev

if [ -b /dev/$parent ]; then
	dd if=/usr/share/syslinux/mbr.bin of=/dev/$parent
else
	echo "Warning: Could not find the parent device for $dev"
fi

cleanup