summaryrefslogtreecommitdiffstats
path: root/install-lilo.sh
blob: 6beae2c882b0aee2c5be72b80d23e32efe52d934 (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
#!/bin/sh

set -u

main()
{
	local boot=
	local root=
	local rootdev=
	local rootpart=
	local b4=
	local b3=
	local b2=
	local b1=

	[ "x$(id -u)" = 'x0' ] || error 'Must be run as the superuser'

	if [ ${#} -eq 0 ]; then
		if is_chroot; then
			error 'Running under chroot; %s' \
				'please specify the boot and root devices'
		fi
		boot="$(mountpoint -n /boot | cut -d ' ' -f 1)"
		boot="$(printf '%s\n' "${boot}" | \
			sed 's|^\([/a-zA-Z]*\)\([0-9]*\)|\1|')"
		root="$(mountpoint -n / | cut -d ' ' -f 1)"
		rootdev="$(printf '%s\n' "${root}" | \
			sed 's|^\([/a-zA-Z]*\)\([0-9]*\)|\1|')"
		rootpart="$(printf '%s\n' "${root}" | \
			sed 's|^\([/a-zA-Z]*\)\([0-9]*\)|\2|')"
		root="$(printf 'PARTUUID=%s-%02d\n' \
			"$(/usr/libexec/lilo/blkid "${rootdev}")" ${rootpart})"
	elif [ ${#} -eq 2 ]; then
		boot="${1}"
		root="${2}"
	else
		usage >&2
		exit 1
	fi

	[ -e "${boot}" ] || error 'Boot device not found'
	[ -e /etc/lilo.conf.in ] || \
		error '/etc/lilo.conf: No such file or directory'
	[ -e /etc/lilo.conf ] && error '/etc/lilo.conf exists'

	sed "s|@BOOT@|${boot}|g; s|@ROOT@|${root}|g;" /etc/lilo.conf.in \
		>/etc/lilo.conf
	/sbin/lilo
	rm /etc/lilo.conf
}

usage()
{
	printf 'Usage: install-lilo [<boot> <root>]\n'
}

error()
{
	local fmt="${1}"
	shift 1

	printf "install-lilo: Error: ${fmt}\n" "${@}" >&2
	exit 2
}

is_chroot()
{
	awk '$5 == "/" { exit(1); };' "/proc/${$}/mountinfo"
}

main "${@}"