summaryrefslogtreecommitdiff
path: root/scripts/build/binary_loopback_cfg
blob: 00f537c48fab0c803a530597f97b24319b0a648c (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
290
291
292
#!/bin/sh

## live-build(7) - System Build Scripts
## 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="$(Echo 'installs loopback.cfg into binary')"
HELP=""
USAGE="${PROGRAM} [--force]"

Arguments "${@}"

# Reading configuration files
Read_conffiles config/all config/common config/bootstrap config/chroot config/binary config/source
Set_defaults

# We avoid checking if we need to be running this bootloader
# on purpose. The reason: You will always want to have
# a loopback.cfg file
# TODO: Add an option to be able to disable this file creation
Echo_message "Begin installing loopback.cfg..."

# Requiring stage file
Require_stagefile .build/config .build/bootstrap

# Checking stage file
Check_stagefile .build/binary_loopback_cfg

# Checking lock file
Check_lockfile .lock

# Creating lock file
Create_lockfile .lock

# Check architecture
Check_architectures amd64 i386
Check_crossarchitectures

# Variable used to embed newlines
NL="
"

# Local functions
Grub_menu_entry() {
	local LABEL="$1"
	local KERNEL="$2"
	local INITRD="$3"
	local APPEND="$4"

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

Grub_live_entry ()
{
	LINUX_LIVE="${LINUX_LIVE}$(Grub_menu_entry "$@")$NL"
}

Grub_install_entry ()
{
	LABEL="${1}"
	TYPE="${2}"
	KERNEL="${3}"
	INITRD="${4}"
	APPEND="${5}"

	# Drop "quiet" kernel parameter for rescue entries
	case $TYPE in
		rescue|rescuegui)
			APPEND=$(echo "$APPEND" | sed -e 's/ quiet//')
		;;
	esac

	if [ -z "$LINUX_INSTALL" ]; then
	    # First entry recorded in LINUX_INSTALL
	    LINUX_INSTALL="$(Grub_menu_entry "$LABEL" "$KERNEL" "$INITRD" "$APPEND")$NL"
	else
	    # Remaining entries recorded in LINUX_ADVANCED_INSTALL
	    LINUX_ADVANCED_INSTALL="$LINUX_ADVANCED_INSTALL$(Grub_menu_entry "$LABEL" "$KERNEL" "$INITRD" "$APPEND")$NL"
	fi
}

Grub_live_autodetect_menu_entry ()
{
	local LABEL="${1}"
	local AMD64_KERNEL="${2}"
	local AMD64_INITRD="${3}"
	local _486_KERNEL="${4}"
	local _486_INITRD="${5}"
	local APPEND="${6}"
	cat <<END
# Autodetect if amd64 is supported
menuentry "$LABEL" {
	if cpuid -l; then
		linux ${AMD64_KERNEL} ${APPEND}
		initrd ${AMD64_INITRD}
	else
		linux ${_486_KERNEL} ${APPEND}
		initrd ${_486_INITRD}
	fi
}
END
}

Grub_live_autodetect_entry ()
{
	LINUX_LIVE="${LINUX_LIVE}$(Grub_live_autodetect_menu_entry "$@")$NL"
}

Grub_memtest_menu_entry ()
{
	cat <<END
menuentry "$LB_MEMTEST" {
	linux16 /${INITFS}/memtest
}
END
}

if [ -e "config/bootloaders/grub-pc" ]
then
	# Internal local copy
	_SOURCE="config/bootloaders/grub-pc"
else
	# Internal system copy
	if [ -n "${LIVE_BUILD}" ]
	then
		_SOURCE="${LIVE_BUILD}/share/bootloaders/grub-pc"
	else
		_SOURCE="/usr/share/live/build/bootloaders/grub-pc"
	fi
fi

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

	*)
		INITFS=""
		;;
esac

Check_multiarchitectures

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

if [ -n "${LB_NET_COW_PATH}" ]
then
	Echo_error "Net cow not yet supported on grub"
	exit 1
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}"

# Assembling kernel configuration
_AMD64_486_NUMBER="0"

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

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

	Grub_live_autodetect_entry "Live system (autodetect)" \
		"/${INITFS}/${AMD64_KERNEL}" \
		"/${INITFS}/${AMD64_INITRD}" \
		"/${INITFS}/${_486_KERNEL}" \
		"/${INITFS}/${_486_INITRD}" \
		"$APPEND_LIVE"
else
	Grub_live_entry "Live system" "/${INITFS}/${DEFAULT_KERNEL}" "/${INITFS}/${DEFAULT_INITRD}" "${APPEND_LIVE}"
fi

if [ "${LB_BOOTAPPEND_LIVE_FAILSAFE}" != "none" ]
then
	Grub_live_entry "Live system (fail-safe mode)" "/${INITFS}/${DEFAULT_KERNEL}" "/${INITFS}/${DEFAULT_INITRD}" "${LB_BOOTAPPEND_LIVE_FAILSAFE}"
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_entry "Live system, kernel ${VERSION}" "/${INITFS}/$(basename ${KERNEL})" "/${INITFS}/initrd.img-${VERSION}" "${APPEND_LIVE}"

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

# Assembling debian-installer configuration
if [ "${LB_DEBIAN_INSTALLER}" != "false" ]
then
	KERNEL_DI="/install/vmlinuz"
	INITRD_DI="/install/initrd.gz"
	APPEND_DI="vga=normal quiet ${LB_BOOTAPPEND_INSTALL}"

	KERNEL_GI="/install/gtk/vmlinuz"
	INITRD_GI="/install/gtk/initrd.gz"
	APPEND_GI="video=vesa:ywrap,mtrr vga=788 quiet ${LB_BOOTAPPEND_INSTALL}"

	Grub_install_entry "Start installer" "installgui" "${KERNEL_GI}" "${INITRD_GI}" "${APPEND_GI}"

	Grub_install_entry "Install in graphical mode" "installgui" "${KERNEL_GI}" "${INITRD_GI}" "${APPEND_GI}"
	Grub_install_entry "Install in text mode" "install" "${KERNEL_DI}" "${INITRD_DI}" "${APPEND_DI}"
	Grub_install_entry "Expert install in graphical mode" "expertgui" "${KERNEL_GI}" "${INITRD_GI}" "priority=low ${APPEND_GI}"
	Grub_install_entry "Expert install in text mode" "expert" "${KERNEL_DI}" "${INITRD_DI}" "priority=low ${APPEND_DI}"
	Grub_install_entry "Automatic install in graphical mode" "autogui" "${KERNEL_GI}" "${INITRD_GI}" "auto=true priority=critical ${APPEND_GI}"
	Grub_install_entry "Automatic install in text mode" "auto" "${KERNEL_DI}" "${INITRD_DI}" "auto=true priority=critical ${APPEND_DI}"
	Grub_install_entry "Rescue system in graphical mode" "rescuegui" "${KERNEL_GI}" "${INITRD_GI}" "rescue/enable=true ${APPEND_GI}"
	Grub_install_entry "Rescue system in text mode" "rescue" "${KERNEL_DI}" "${INITRD_DI}" "rescue/enable=true ${APPEND_DI}"
fi

# Assembling memtest configuration
if [ -f "binary/${INITFS}/memtest" ]
then
	MEMTEST="$(Grub_memtest_menu_entry)$NL"
fi

# Copying templates
mkdir -p binary/boot/grub
cp -a "${_SOURCE}"/* binary/boot/grub/

escape_for_sed() {
    echo -n "$1" | perl -npe 's/\n/\\n/gm'
}

sed -i \
    -e "s|LINUX_LIVE|$(escape_for_sed "${LINUX_LIVE}")|" \
    -e "s|LINUX_ADVANCED_INSTALL|$(escape_for_sed "${LINUX_ADVANCED_INSTALL}")|" \
    -e "s|LINUX_INSTALL|$(escape_for_sed "${LINUX_INSTALL}")|" \
    -e "s|MEMTEST|$(escape_for_sed "${MEMTEST}")|" \
    -e "s|KERNEL_GI|$(escape_for_sed "${KERNEL_GI}")|" \
    -e "s|INITRD_GI|$(escape_for_sed "${INITRD_GI}")|" \
    -e "s|APPEND_GI|$(escape_for_sed "${APPEND_GI}")|" \
    -e "s|KERNEL_DI|$(escape_for_sed "${KERNEL_DI}")|" \
    -e "s|INITRD_DI|$(escape_for_sed "${INITRD_DI}")|" \
    -e "s|APPEND_DI|$(escape_for_sed "${APPEND_DI}")|" \
    -e "s|KERNEL_LIVE|$(escape_for_sed "${KERNEL_LIVE}")|" \
    -e "s|INITRD_LIVE|$(escape_for_sed "${INITRD_LIVE}")|" \
    -e "s|APPEND_LIVE|$(escape_for_sed "${APPEND_LIVE}")|" \
    -e "s|LB_BOOTAPPEND_INSTALL|$(escape_for_sed "${LB_BOOTAPPEND_INSTALL}")|" \
    -e "s|LB_BOOTAPPEND_LIVE_FAILSAFE|$(escape_for_sed "${LB_BOOTAPPEND_LIVE_FAILSAFE}")|" \
    -e "s|LB_BOOTAPPEND_LIVE|$(escape_for_sed "${LB_BOOTAPPEND_LIVE}")|" \
    binary/boot/grub/*.cfg

sed -i -e 's|//|/|g' binary/boot/grub/*.cfg
sed -i -e 's|\ $||g' binary/boot/grub/*.cfg

echo "source /boot/grub/grub.cfg" > binary/boot/grub/loopback.cfg

# Creating stage file
Create_stagefile .build/binary_loopback_cfg