summaryrefslogtreecommitdiffstats
path: root/setup-bootable.in
blob: 839a01d28bb38c2d7551837bbe920b44200647a0 (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
#!/bin/sh

prog=${0##*/}
version=@VERSION@

cleanup() {
	if [ -n "$uninstalls" ]; then
		apk del -q syslinux
	fi
	if [ -n "$umounts" ]; then
		umount $umounts
	fi
}

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

usage() {
	cat <<__EOF__
$prog $version
usage: $prog [-hu] SOURCE [DEST]

Copy the contents of SOURCE to DEST and make DEST bootable.

SOURCE can be a directory or a ISO image. DEST can be a mounted directory 
or a device. If DEST is ommitted /media/usb will be used.

Options:
 -f  Force overwrite existing files. Will overwrite syslinux.cfg if upgrade.
 -h  Show this help.
 -u  Upgrade mode. Keep existing syslinux.cfg and don't run syslinux.
 -s  Force run syslinux, even if upgrade mode.
 -v  Verbose mode. Display whats going on.

__EOF__
	exit 1
}

while getopts "fhusv" opt; do
	case "$opt" in
	-f) force=1;;
	-h) usage;;
	-u) upgrade=1;;
	-s) syslinux=1;;
	-v) verbose=1;;
	esac
done

shift $(($OPTIND - 1))

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

srcmnt=${MNT:-/mnt}

[ -z "$src" ] && usage

# Find the srcdir, mount loopback if needed
if [ -f "$src"/.alpine-release ]; then
	srcdir="$src"
	[ -n "$verbose" ] && echo "Using directory $src as source"
else
	mount -o loop -t iso9660 "$src" $srcmnt \
		|| die "Failed to mount loopback $src"
	[ -n "$verbose" ] && echo "Using image $src as source"
	umounts="$srcmnt"
	srcdir="$srcmnt"
	[ -f "$srcdir"/.alpine-release ] || die "No .alpine-release found on image $src"
fi

# find target device
if [ -d "$dest" ]; then
	if ! awk '{print $2}' /proc/mounts | grep -q "^$dest\$"; then
		mount "$dest" || die "Failed to mount $dest"
		umounts="$umounts $dest"
	fi
	destdir="$dest"
	[ -n "$vserbose" ] && echo "Using $destdir as target"
elif [ -b "$dest" ]; then
	destdir="/media/${dest##*/}"
	mkdir -p "$destdir"
	mount "$dest" "$destdir" || die "Failed to mount $dest on $destdir"
	[ -n "$vserbose" ] && echo "Using $dest (mounted on $destdir) as target"
fi

# check if this files exist and not in upgrade mode
if [ -z "$upgrade" ] && [ -z "$force" ]; then
	for i in boot apks syslinux.cfg .alpine-release; do
		[ -e "$destdir"/$i ] && die "$destdir/$i already exists. Aborting"
	done
fi

# remove partial upgrades if any.
rm -rf "$destdir"/.new "$destdir"/.old

# check that we have the space we need
needed_space=$(cd "$srcdir" && du -s -c boot apks syslinux.cfg .alpine-release | awk '$2 == "total" {print $1}')
[ -n "$verbose" ] && echo "Needed space:    $needed_space kB"

available_space=$(df -k "$destdir" | tail -n 1 | awk '{print $4}')
[ -n "$verbose" ] && echo "Available space: $available_space kB"
[ $available_space -lt $needed_space ] && die "Not enough space on $destdir. Aborting."

# copy the files to .new
echo "Copying new files to $destdir/.new..."
mkdir -p "$destdir"/.new || die "Failed to create $destdir/.new"
for i in boot apks syslinux.cfg .alpine-release; do
	[ -n "$verbose" 
	cp -a "$srcdir"/$i "$destdir"/.new/
done

# make sure files are really there before we replace existing
sync

echo "Replacing existing files..."
mkdir -p "$destdir"/.old || die "Failed to create $destdir/.old"

# do we want keep existing syslinux.cfg?
tomove="boot apks .alpine-release"
if [ -n "$force" ] || ! [ -e "$destdir"/syslinux.cfg ]; then
	tomove="$tomove syslinux.cfg"
fi

# move current files to .old
for i in $tomove; do
	if [ -e "$destdir"/$i ]; then
		mv "$destdir"/$i "$destdir"/.old/ || die "Failed to move $destdir/$i to $destdir/.old/"
	fi
done

# move .new to current
for i in $tomove; do
	mv "$destdir"/.new/$i "$destdir"/ || die "Failed to move $destdir/.new/ to $destdir"
done

# cleanup
[ -z "$keep_old" ] && rm -rf "$destdir"/.old "$destdir"/.new
sync


# If we only copy then we are done.
if [ -z "$syslinux" ]; then
	cleanup
	echo "Done."
	exit 0
fi

echo "Making usb bootable..."
dev=$(awk "\$2 == \"$dest\" {print \$1}" /proc/mounts)
parent=$(basename $(dirname /sys/block/*/$(basename $dev)))
if ! apk info -q -e syslinux; then
	apk add -q syslinux || die "Failed to install syslinux"
	uninstalls="syslinux"
fi

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
echo "Done."