aboutsummaryrefslogtreecommitdiffstats
path: root/main/syslinux/update-extlinux
blob: 0499f5f91a86da86ff4c3ce65c4e745e13c3153b (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
#!/bin/sh

version=
default=0
timeout=5
verbose=0

conf=/boot/extlinux.conf
myconf=/etc/update-extlinux.conf

# read in extlinux settings
if [ -f "$myconf" ]; then
	. $myconf
fi

everbose() {
	if [ "$verbose" = "0" ]; then
		return
	fi

	echo $*
}

ewarn() {
	echo "WARNING:" $@ >&2
}

eerror() {
	echo "ERROR:" $@ >&2
	return 1
}

everbose "Updating extlinux configuration."

if [ "x$root" = "x" ]; then
	ewarn "Root device is not specified in $myconf."
	blkid_export=$(blkid -o export /dev/root)
	if [ -n "$blkid_export" ]; then
		export $blkid_export
	fi
	if [ -z "$UUID" ]; then
		# try parse /proc/mount for mounted /
		dev=$(awk '$2 == "/" {dev=$1} END {print dev}' /proc/mounts)
		if [ -n "$dev" ]; then
			blkid_export=$(blkid -o export $dev)
			if [ -n "$blkid_export" ]; then
				export "$blkid_export"
			fi
		fi
	fi
	if [ -z "$UUID" ]; then
		if [ -z "$dev" ]; then
			eerror "Failed to detect root device"
			exit 1
		else
			root=$dev
		fi
	else
		root=UUID=$UUID
	fi
	everbose "Root device is: $root"
fi

rtimeout=$((${timeout}\*10))
syslinux_menu=menu.c32
menu_hidden=

# vesa menu has been requested?
if [ "$vesa_menu" = "1" ]; then
	syslinux_menu=vesamenu.c32
fi

umask 0022
rm -f $conf.new
echo "# Generated by update-extlinux $version" > $conf.new
echo "DEFAULT $syslinux_menu" >> $conf.new
echo "PROMPT 0" >> $conf.new
echo "MENU TITLE Alpine/$(uname -s) Boot Menu" >> $conf.new
if [ "$hidden" = "1" ]; then
	echo "MENU HIDDEN" >> $conf.new
fi
echo "MENU AUTOBOOT Alpine will be booted automatically in # seconds." >> $conf.new
echo "TIMEOUT $rtimeout" >> $conf.new

lst=0
if [ -f "/boot/xen.gz" ]; then
	for kernel in $(find /boot -name "vmlinuz-*" -type f); do
		tag=$(basename $kernel | cut -b9-)
		everbose "Found Xen hypervisor: /boot/xen.gz, kernel: $kernel"

		if [ -f "/boot/initramfs-$tag" ]; then
			everbose "Found initramfs: /boot/initramfs-$tag"
			initramfs="initramfs-$tag"
		else
			initramfs=
		fi
		label=xen-$(grep -w -l $tag /usr/share/kernel/*/kernel.release \
				| cut -d/ -f5)
		if [ "$label" = "xen-" ]; then
			label=xen-$lst
		fi

		echo "LABEL $label" >> $conf.new
		if [ "$label" = "$default" ]; then
			echo "  MENU DEFAULT" >> $conf.new
		fi
		echo "  MENU LABEL Xen + Linux $tag" >> $conf.new
		echo "  COM32 mboot.c32" >> $conf.new
		echo "  APPEND xen.gz $xen_opts --- $(basename $kernel) root=$root modules=${modules}${TYPE:+,$TYPE} $default_kernel_opts --- $initramfs" >> $conf.new
		echo "" >> $conf.new
		lst=$(($lst + 1))
	done
fi

for kernel in $(find /boot -name "vmlinuz-*" -type f); do
	tag=$(basename $kernel | cut -b9-)
	everbose "Found kernel: $kernel"
	label=$(grep -w -l $tag /usr/share/kernel/*/kernel.release | cut -d/ -f5)
	if [ -z "$label" ]; then
		label=$lst
	fi
	echo "LABEL $label" >> $conf.new
	if [ "$label" = "$default" ]; then
		echo "  MENU DEFAULT" >> $conf.new
	fi
	echo "  MENU LABEL Linux $tag" >> $conf.new
	echo "  LINUX $(basename $kernel)" >> $conf.new
	if [ -f "/boot/initramfs-$tag" ]; then
		everbose "Found initramfs: /boot/initramfs-$tag"
		echo "  INITRD initramfs-$tag" >> $conf.new
	fi
	echo "  APPEND root=$root modules=${modules}${TYPE:+,$TYPE} $default_kernel_opts" >> $conf.new
	echo "" >> $conf.new
	lst=$(($lst + 1))
done

if [ -n "$password" ]; then
	echo "NOESCAPE 1" >> $conf.new
	echo "MENU MASTER PASSWD $password" >> $conf.new
	echo "" >> $conf.new
	chmod o-r $conf.new
fi

everbose "$lst entries found."

for entry in /etc/update-extlinux.d/*; do
	[ -f "$entry" ] && { cat $entry; echo ""; } >> $conf.new
done

echo "MENU SEPARATOR" >> $conf.new
echo "" >> $conf.new

if [ -f "/boot/hdt.c32" ]; then
	everbose "Found Hardware Detection Tool: /boot/hdt.c32"
	echo "LABEL hdt" >> $conf.new
	echo "  MENU LABEL Hardware info" >> $conf.new
	if [ -n "$password" ]; then
		echo "  MENU PASSWD" >> $conf.new
	fi
	echo "  COM32 hdt.c32" >> $conf.new
	if [ -f "/boot/memtest" ]; then
		everbose "Found memtest86+: /boot/memtest"
		echo "  APPEND memtest=memtest" >> $conf.new
	fi
	echo "" >> $conf.new
elif [ -f "/boot/memtest" ]; then
	everbose "Found memtest86+: /boot/memtest"
	echo "LABEL memtest" >> $conf.new
	echo "  MENU LABEL Memtest86+" >> $conf.new
        echo "  KERNEL memtest" >> $conf.new
	echo "" >> $conf.new
fi

if [ -f "/boot/reboot.c32" ]; then
	everbose "Found reboot"
	echo "LABEL reboot" >> $conf.new
	echo "  MENU LABEL Reboot" >> $conf.new
        echo "  COM32 reboot.c32" >> $conf.new
	echo "" >> $conf.new
fi

if cmp --quiet $conf.new $conf; then
	everbose "Configuration unchanged."
	rm $conf.new
fi

if [ "$overwrite" != "1" ]; then
	exit 0
elif [ -f "$conf.new" ]; then
	# keep a backup just in case
	if [ -f "$conf" ]; then
		mv $conf $conf.old
	fi

	mv $conf.new $conf
fi

everbose "Installing libutil.c32 libcom32.c32 mboot.c32 menu.c32 vesamenu.c32 to /boot."
cp /usr/share/syslinux/libutil.c32 \
	/usr/share/syslinux/libcom32.c32 \
	/usr/share/syslinux/mboot.c32 \
	/usr/share/syslinux/menu.c32 \
	/usr/share/syslinux/vesamenu.c32 \
	/boot

case "$(stat -f -c '%T' /boot)" in
ext*) extlinux --update /boot;;
esac