summaryrefslogtreecommitdiff
path: root/sys-boot/systemrescuecd-x86-grub/files/systemrescuecd.grub-2
blob: 332a7c37fce0594856ffb900affba6720965859e (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
#!/bin/sh
# Copyright 1999-2019 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

. /usr/share/grub/grub-mkconfig_lib

if [ -r /etc/default/systemrescuecd ] ; then
	. /etc/default/systemrescuecd
fi

COPY_SRCD_TO_BOOT=${COPY_SRCD_TO_BOOT:-no}

bootdir="/boot"
installdir="/usr/share/systemrescuecd"

isorex='^(/.*/)?systemrescuecd-.*[.]iso$'

# Path of the link to the newest ISO, created by ebuild
srcd="${installdir}/systemrescuecd-x86-newest.iso"

# Extract ISO version
isovsed() {
	sed -E 's|^.*systemrescuecd(-x86)?-||;s|.iso$||'
}

# Find ISOs in a given directory
isofind() {
	find "${1}" -maxdepth 1 -type f -regextype egrep -regex ${isorex}
}

# Copy ISO to boot partition
copy_srcd_iso() {
	if [ ! -f "${bootdir}/"$(basename "${1}") ]; then
		if [ $(df -k --output=avail "${bootdir}" | tail -1) -gt $(du -k "${1}" | cut -f 1) ]; then
			cp "${1}" "${bootdir}/"
		else
			# Before complaining, check if the installed ISO is actually newer
			if $(printf '%s\n' $(isofind "${bootdir}" | isovsed | sort -V) $(echo "${1}" | isovsed) | sort -VC); then
				gettext_printf "Error: Not enough free disk space on ${bootdir}!\n" >&2
				gettext_printf "Error: Failed to copy the new iso!\n" >&2
			fi
		fi
	fi
}

write_srcd() {
	cat <<EOF
	menuentry "${longname} [${1}] (Copy to RAM${bootops}" --class rescue {
${grub_string}
		set gfxpayload=keep
		set isofile=${path}
		loopback loop \${isofile}
		linux (loop)/sysresccd/boot/x86_64/vmlinuz archisobasedir=sysresccd img_loop=\${isofile} img_dev=${diskuuid} copytoram ${SRCD_BOOTOPTIONS}
		initrd (loop)/sysresccd/boot/intel_ucode.img (loop)/sysresccd/boot/amd_ucode.img (loop)/sysresccd/boot/x86_64/sysresccd.img
	}
	menuentry "${longname} [${1}] (Copy to RAM with persistence${bootops}" --class rescue {
${grub_string}
		set gfxpayload=keep
		set isofile=${path}
		loopback loop \${isofile}
		linux (loop)/sysresccd/boot/x86_64/vmlinuz archisobasedir=sysresccd img_loop=\${isofile} img_dev=${diskuuid} cow_device=${diskuuid} cow_directory=/persistent_sysresccd-${1}/x86_64 copytoram ${SRCD_BOOTOPTIONS}
		initrd (loop)/sysresccd/boot/intel_ucode.img (loop)/sysresccd/boot/amd_ucode.img (loop)/sysresccd/boot/x86_64/sysresccd.img
	}
	menuentry "${longname} [${1}] (Minimal${bootops}" --class rescue {
${grub_string}
		set gfxpayload=keep
		set isofile=${path}
		loopback loop \${isofile}
		linux (loop)/sysresccd/boot/x86_64/vmlinuz archisobasedir=sysresccd img_loop=\${isofile} img_dev=${diskuuid} ${SRCD_BOOTOPTIONS}
		initrd (loop)/sysresccd/boot/intel_ucode.img (loop)/sysresccd/boot/amd_ucode.img (loop)/sysresccd/boot/x86_64/sysresccd.img
	}
EOF
}

write_srcd_5() {
	cat <<EOF
	menuentry "${longname} [${1}] (64bit${bootops}" --class rescue {
${grub_string}
		set isofile=${path}
		loopback loop \${isofile}
		linux (loop)/isolinux/rescue64 ${SRCD_BOOTOPTIONS} isoloop=\${isofile}
		initrd (loop)/isolinux/initram.igz
	}
	menuentry "${longname} [${1}] (32bit${bootops}" --class rescue {
${grub_string}
		set isofile=${path}
		loopback loop \${isofile}
		linux (loop)/isolinux/rescue32 ${SRCD_BOOTOPTIONS} isoloop=\${isofile}
		initrd (loop)/isolinux/initram.igz
	}
EOF
}

# Get v5 ISOs
# Use: isogrep5 [-v]
#   -v invert match
isogrep5() {
	ls -rv "${isopref}"/systemrescuecd-*.iso | grep ${1} -e '-x86-5'
}

write_srcd_submenu() {
	# Start submenu
	echo "submenu \"${longname}\" --class submenu {"

	# Make sure to reverse-sort by version
	for iso in $(isogrep5 -v; isogrep5); do
		path=$(make_system_path_relative_to_its_root "${iso}")
		gettext_printf "  image: ${iso}\n" >&2
		if $(printf '%s\n' "6.0.0" $(echo ${iso} | isovsed) | sort -VC); then
			write_srcd $(echo ${iso} | isovsed)
		else
			write_srcd_5 $(echo ${iso} | isovsed)
		fi
	done

	# End submenu
	echo "}"
}

bootops=")"

if [ ! -z "${SRCD_BOOTOPTIONS}" ]; then
	bootops=" with bootoptions)"
fi

if [ "${COPY_SRCD_TO_BOOT}" = yes ]; then
	srcd=$(realpath "${srcd}")
	copy_srcd_iso "${srcd}"
fi

# Build menu entries
for isopref in "${bootdir}" "${installdir}"; do
	# Make sure there are any ISOs
	ls "${isopref}" | grep -E -q -e "${isorex}" || continue

	diskuuid=/dev/disk/by-uuid/$(${grub_probe} --target=fs_uuid "${isopref}")
	device=$(${grub_probe} --target=device "${isopref}")
	label=$(${grub_probe} --target=fs_label "${isopref}")
	[ "${label}" = "(null)" ] && label=${device}
	grub_string=$(prepare_grub_to_access_device "${device}" | grub_add_tab | grub_add_tab)
	longname="SystemRescueCD on ${label}"

	gettext_printf "Found %s on %s\n" "${longname}" "${device}" >&2

	write_srcd_submenu
done