summaryrefslogtreecommitdiff
path: root/scripts/build/binary_grub_cfg
blob: e8322e50ec1e6c32072d50c4ce8f83ea70631d7b (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
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
#!/bin/sh

## live-build(7) - System Build Scripts
## Copyright (C) 2016-2020 The Debian Live team
## Copyright (C) 2006-2015 Daniel Baumann <mail@daniel-baumann.ch>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


set -e

# Including common functions
[ -e "${LIVE_BUILD}/scripts/build.sh" ] && . "${LIVE_BUILD}/scripts/build.sh" || . /usr/lib/live/build.sh

# Setting static variables
DESCRIPTION="Generates config for grub-pc and grub-efi, and installs loopback support"
USAGE="${PROGRAM} [--force]"

# Processing arguments and configuration files
Init_config_data "${@}"

# NOTE: This creates the config used for both grub-pc and grub-efi.
# It also installs loopback.cfg for looback support, which depends upon that
# config. Since loopback support is added unconditionally, this script is
# thus run unconditionally!
# TODO: Add an option to allow disabling loopback.cfg creation? (and thus conditionally run the script)

Echo_message "Begin installing config for grub-pc and/or grub-pc, and loopback support..."

# Requiring stage file
Require_stagefiles config bootstrap

# Checking stage file
Check_stagefile

# Acquire lock file
Acquire_lockfile

# Check architecture
Check_architectures amd64 i386 arm64
Check_crossarchitectures

_TARGET="binary/boot/grub"

# Local functions
Grub_live_menu_entry() {
	local LABEL="$1"
	local KERNEL="$2"
	local INITRD="$3"
	local APPEND="$4"
	local HOTKEY="${5:+ --hotkey=$5}"

	cat >> "${LIVE_ENTRIES_TMP}" <<END
menuentry "${LABEL}"${HOTKEY} {
	linux	$KERNEL $APPEND
	initrd	$INITRD
}
END
}

Grub_live_autodetect_menu_entry ()
{
	local LABEL="${1}"
	local AMD64_KERNEL="${2}"
	local AMD64_INITRD="${3}"
	local _686_KERNEL="${4}"
	local _686_INITRD="${5}"
	local APPEND="${6}"

	cat >> "${LIVE_ENTRIES_TMP}" <<END
# Autodetect if amd64 is supported
menuentry "$LABEL" {
	if cpuid -l; then
		linux ${AMD64_KERNEL} ${APPEND}
		initrd ${AMD64_INITRD}
	else
		linux ${_686_KERNEL} ${APPEND}
		initrd ${_686_INITRD}
	fi
}
END
}

# User config replacement/additional files
_SOURCE_USER="config/bootloaders/grub-pc"

# Copying templates
mkdir -p "${_TARGET}"
if [ -n "${LIVE_BUILD}" ]; then
	cp -a "${LIVE_BUILD}/share/bootloaders/grub-pc"/* "${_TARGET}"/
else
	cp -a "/usr/share/live/build/bootloaders/grub-pc"/* "${_TARGET}"/
fi
if [ -e "${_SOURCE_USER}" ]; then
	cp -af "${_SOURCE_USER}"/* "${_TARGET}"/
fi

case "${LB_INITRAMFS}" in
	live-boot)
		INITFS="live"
		;;

	*)
		INITFS="boot"
		;;
esac

# Setting boot parameters
if [ "${LB_UNION_FILESYSTEM}" != "overlay" ]; then
	LB_BOOTAPPEND_LIVE="${LB_BOOTAPPEND_LIVE} union=${LB_UNION_FILESYSTEM}"
fi

# Default entries
DEFAULT_FLAVOUR="$(echo ${LB_LINUX_FLAVOURS} | awk '{ print $1 }')"
DEFAULT_KERNEL="$(basename chroot/boot/vmlinuz-*${DEFAULT_FLAVOUR})"
DEFAULT_INITRD="initrd.img-$(echo ${DEFAULT_KERNEL} | sed -e 's|vmlinuz-||')"

KERNEL_LIVE="/${INITFS}/${DEFAULT_KERNEL}"
INITRD_LIVE="/${INITFS}/${DEFAULT_INITRD}"
APPEND_LIVE="${LB_BOOTAPPEND_LIVE} findiso=\${iso_path}"

# Ensure fresh live entries
LIVE_ENTRIES_TMP="${_TARGET}/live.cfg.tmp"
rm -f "${LIVE_ENTRIES_TMP}"

# Assemble live menu entries
_AMD64_686_NUMBER="0"

for _FLAVOUR in ${LB_LINUX_FLAVOURS}; do
	if [ "${_FLAVOUR}" = "amd64" -o "${_FLAVOUR}" = "686" ]; then
		_AMD64_686_NUMBER="$((${_AMD64_686_NUMBER} + 1))"
	fi
done

if [ "${_AMD64_686_NUMBER}" -ge 2 ] ; then
	# Default entries
	AMD64_KERNEL="$(basename chroot/boot/vmlinuz-*amd64)"
	AMD64_INITRD="initrd.img-$(echo ${AMD64_KERNEL} | sed -e 's|vmlinuz-||')"
	_686_KERNEL="$(basename chroot/boot/vmlinuz-*686)"
	_686_INITRD="initrd.img-$(echo ${_686_KERNEL} | sed -e 's|vmlinuz-||')"

	Grub_live_autodetect_menu_entry "Live system (autodetect)" \
		"/${INITFS}/${AMD64_KERNEL}" \
		"/${INITFS}/${AMD64_INITRD}" \
		"/${INITFS}/${_686_KERNEL}" \
		"/${INITFS}/${_686_INITRD}" \
		"${APPEND_LIVE}"

	if [ "${LB_BOOTAPPEND_LIVE_FAILSAFE}" != "none" ]; then
		Grub_live_autodetect_menu_entry "Live system (autodetect) (fail-safe mode)" \
			"/${INITFS}/${AMD64_KERNEL}" \
			"/${INITFS}/${AMD64_INITRD}" \
			"/${INITFS}/${_686_KERNEL}" \
			"/${INITFS}/${_686_INITRD}" \
			"${LB_BOOTAPPEND_LIVE_FAILSAFE}"
	fi
else
	Grub_live_menu_entry "Live system" \
		"/${INITFS}/${DEFAULT_KERNEL}" \
		"/${INITFS}/${DEFAULT_INITRD}" \
		"${APPEND_LIVE}" \
		"l"
	if [ "${LB_BOOTAPPEND_LIVE_FAILSAFE}" != "none" ]; then
		Grub_live_menu_entry "Live system (fail-safe mode)" \
			"/${INITFS}/${DEFAULT_KERNEL}" \
			"/${INITFS}/${DEFAULT_INITRD}" \
			"${LB_BOOTAPPEND_LIVE_FAILSAFE}"
	fi
fi

_COUNT=0
for KERNEL in chroot/boot/vmlinuz-*; do
	_COUNT=$(( $_COUNT + 1 ))
done

if [ $_COUNT -gt 1 ]; then
	for KERNEL in chroot/boot/vmlinuz-*; do
		VERSION="$(basename ${KERNEL} | sed -e 's|vmlinuz-||')"

		Grub_live_menu_entry "Live system, kernel ${VERSION}" \
			"/${INITFS}/$(basename ${KERNEL})" \
			"/${INITFS}/initrd.img-${VERSION}" \
			"${APPEND_LIVE}"

		if [ "${LB_BOOTAPPEND_LIVE_FAILSAFE}" != "none" ]; then
			Grub_live_menu_entry "Live system, kernel ${VERSION} (fail-safe mode)" \
				"/${INITFS}/$(basename ${KERNEL})" \
				"/${INITFS}/initrd.img-${VERSION}" \
				"${LB_BOOTAPPEND_LIVE_FAILSAFE}"
		fi
	done
fi

# Replace placeholder with compiled live entries temporarily held in live.cfg.tmp
sed -i -e "/@LINUX_LIVE@/r ${LIVE_ENTRIES_TMP}" -e "/@LINUX_LIVE@/d" "${_TARGET}"/*.cfg
sed -i -e "/LINUX_LIVE/r ${LIVE_ENTRIES_TMP}" -e "/LINUX_LIVE/d" "${_TARGET}"/*.cfg #backwards compatibility
rm -f "${LIVE_ENTRIES_TMP}"

# Assembling debian-installer configuration
if [ "${LB_DEBIAN_INSTALLER}" != "none" ]; then
	ENABLE_INSTALL_MENU="true"

	KERNEL_DI="/install/vmlinuz"
	INITRD_DI="/install/initrd.gz"
	APPEND_DI="vga=normal ${LB_BOOTAPPEND_INSTALL} --- quiet"

	KERNEL_GI="/install/gtk/vmlinuz"
	INITRD_GI="/install/gtk/initrd.gz"
	APPEND_GI="vga=788 ${LB_BOOTAPPEND_INSTALL} --- quiet"

	if [ "${LB_DEBIAN_INSTALLER_GUI}" = "true" ]; then
		rm ${_TARGET}/install_text.cfg
		rm ${_TARGET}/install_start_text.cfg
		mv ${_TARGET}/install_gui.cfg ${_TARGET}/install.cfg
		mv ${_TARGET}/install_start_gui.cfg ${_TARGET}/install_start.cfg
	else
		rm ${_TARGET}/install_gui.cfg
		rm ${_TARGET}/install_start_gui.cfg
		mv ${_TARGET}/install_text.cfg ${_TARGET}/install.cfg
		mv ${_TARGET}/install_start_text.cfg ${_TARGET}/install_start.cfg
	fi
else
	ENABLE_INSTALL_MENU="false"
	rm "${_TARGET}"/install_text.cfg
	rm "${_TARGET}"/install_gui.cfg
	rm "${_TARGET}"/install_start_text.cfg
	rm "${_TARGET}"/install_start_gui.cfg
fi

# Assembling memtest configuration
MEMTEST_BIN="/${INITFS}/memtest"
if [ -f "binary/${MEMTEST_BIN}" ]; then
	ENABLE_MEMTEST="true"
	MEMTEST="source /boot/grub/memtest.cfg" #for backwards compatibility
else
	ENABLE_MEMTEST="false"
	rm -f "${_TARGET}"/memtest.cfg
fi

# These must be held in variables to avoid mistake by checkbashisms
LINUX_INSTALL="source /boot/grub/install_start.cfg"
LINUX_ADVANCED_INSTALL="source /boot/grub/install.cfg"

# Those without '@' markers are for backwards compatibility
sed -i \
    -e "s|@KERNEL_GI@|${KERNEL_GI}|" \
    -e "s|@INITRD_GI@|${INITRD_GI}|" \
    -e "s|@APPEND_GI@|${APPEND_GI}|" \
    -e "s|@KERNEL_DI@|${KERNEL_DI}|" \
    -e "s|@INITRD_DI@|${INITRD_DI}|" \
    -e "s|@APPEND_DI@|${APPEND_DI}|" \
    -e "s|@KERNEL_LIVE@|${KERNEL_LIVE}|" \
    -e "s|@INITRD_LIVE@|${INITRD_LIVE}|" \
    -e "s|@APPEND_LIVE@|${APPEND_LIVE}|" \
    -e "s|@LB_BOOTAPPEND_INSTALL@|${LB_BOOTAPPEND_INSTALL}|" \
    -e "s|@LB_BOOTAPPEND_LIVE_FAILSAFE@|${LB_BOOTAPPEND_LIVE_FAILSAFE}|" \
    -e "s|@LB_BOOTAPPEND_LIVE@|${LB_BOOTAPPEND_LIVE}|" \
    -e "s|@APPEND_INSTALL@|${LB_BOOTAPPEND_INSTALL}|" \
    -e "s|@ENABLE_INSTALL_MENU@|${ENABLE_INSTALL_MENU}|" \
    -e "s|@ENABLE_MEMTEST@|${ENABLE_MEMTEST}|" \
    -e "s|@MEMTEST_BIN@|${MEMTEST_BIN}|" \
    -e "s|@MEMTEST_VERSION@|${LB_MEMTEST}|" \
    -e "s|LINUX_INSTALL|${LINUX_INSTALL}|" \
    -e "s|LINUX_ADVANCED_INSTALL|${LINUX_ADVANCED_INSTALL}|" \
    -e "s|MEMTEST|${MEMTEST}|" \
    -e "s|KERNEL_GI|${KERNEL_GI}|" \
    -e "s|INITRD_GI|${INITRD_GI}|" \
    -e "s|APPEND_GI|${APPEND_GI}|" \
    -e "s|KERNEL_DI|${KERNEL_DI}|" \
    -e "s|INITRD_DI|${INITRD_DI}|" \
    -e "s|APPEND_DI|${APPEND_DI}|" \
    -e "s|KERNEL_LIVE|${KERNEL_LIVE}|" \
    -e "s|INITRD_LIVE|${INITRD_LIVE}|" \
    -e "s|APPEND_LIVE|${APPEND_LIVE}|" \
    -e "s|LB_BOOTAPPEND_INSTALL|${LB_BOOTAPPEND_INSTALL}|" \
    -e "s|LB_BOOTAPPEND_LIVE_FAILSAFE|${LB_BOOTAPPEND_LIVE_FAILSAFE}|" \
    -e "s|LB_BOOTAPPEND_LIVE|${LB_BOOTAPPEND_LIVE}|" \
    "${_TARGET}"/*.cfg

sed -i -e 's|//|/|g' "${_TARGET}"/*.cfg
sed -i -e 's|\ $||g' "${_TARGET}"/*.cfg

# Add loopback support
echo "source /boot/grub/grub.cfg" > "${_TARGET}"/loopback.cfg

# Creating stage file
Create_stagefile