aboutsummaryrefslogtreecommitdiff
blob: 54741e3786b16d6fd000af0c06cb22e755a449ae (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
#!/bin/bash

set_bootloader() {
	if [ "x$BOOTLOADER" == 'xgrub' ]
	then
		set_grub_bootloader
	else
		return 0
	fi
}

set_grub_bootloader() {
	local GRUB_CONF='/boot/grub/grub.conf'

	print_info 1 ''
	print_info 1 "Adding kernel to $GRUB_CONF..."

	# Extract block device information from /etc/fstab
	local GRUB_ROOTFS=$(awk '/[[:space:]]\/[[:space:]]/ { print $1 }' /etc/fstab)
	local GRUB_BOOTFS=$(awk '/^[^#].+[[:space:]]\/boot[[:space:]]/ { print $1 }' /etc/fstab)

	# If /boot is not defined in /etc/fstab, it must be the same as /
	[ "x$GRUB_BOOTFS" == 'x' ] && GRUB_BOOTFS=$GRUB_ROOTFS

	# Read GRUB device map
	[ ! -d ${tmp} ] && mkdir ${tmp}
	grub --batch --device-map=${tmp}/grub.map <<EOF >/dev/null
quit
EOF

	# Get the GRUB mapping for our device
	local GRUB_BOOT_DISK1=$(echo $GRUB_BOOTFS | sed -e 's#\(/dev/.\+\)[[:digit:]]\+#\1#')
	local GRUB_BOOT_DISK=$(awk '{if ($2 == "'$GRUB_BOOT_DISK1'") {gsub(/(\(|\))/, "", $1); print $1;}}' ${tmp}/grub.map)

	local GRUB_BOOT_PARTITION=$(echo $GRUB_BOOTFS | sed -e 's#/dev/.\+\([[:digit:]]\+\)#\1#')
	[ ! -d ${tmp} ] && rm -r ${tmp}
	
	# Create grub configuration directory and file if it doesn't exist.
	[ ! -e `basename $GRUB_CONF` ] && mkdir -p `basename $GRUB_CONF`

	if [ ! -e $GRUB_CONF ]
	then
		if [ "${GRUB_BOOT_DISK}" = '' -o "${GRUB_BOOT_PARTITION}" = '' ]
		then
			print_error 1 'Error! /boot/grub/grub.conf does not exist and the correct settings can\'t be automatically detected.'
			print_error 1 'Please manually create your /boot/grub/grub.conf file.'
		else
			# grub.conf doesn't exist - create it with standard defaults
			touch $GRUB_CONF
			echo 'default 0' >> $GRUB_CONF
			echo 'timeout 5' >> $GRUB_CONF
			echo >> $GRUB_CONF

			# Add grub configuration to grub.conf	
			echo "# Genkernel generated entry, see GRUB documentation for details" >> $GRUB_CONF
			echo "title=Gentoo Linux ($KV)" >> $GRUB_CONF
			echo -e "\troot ($GRUB_BOOT_DISK,$GRUB_BOOT_PARTITION)" >> $GRUB_CONF
			if [ "${BUILD_INITRD}" -eq '0' ]
			then
				echo -e "\tkernel /kernel-${KNAME}-${ARCH}-${KV} root=$GRUB_ROOTFS" >> $GRUB_CONF
			else
				echo -e "\tkernel /kernel-${KNAME}-${ARCH}-${KV} root=/dev/ram0 init=/linuxrc real_root=$GRUB_ROOTFS" >> $GRUB_CONF
				if [ "${PAT}" -gt '4' -a  "${CMD_BOOTSPLASH}" != '1' ]
				then
				    echo -e "\tinitrd /initramfs-${KNAME}-${ARCH}-${KV}" >> $GRUB_CONF
				else
				    echo -e "\tinitrd /initrd-${KNAME}-${ARCH}-${KV}" >> $GRUB_CONF
				fi
			fi
			echo >> $GRUB_CONF
		fi
	else
		# grub.conf already exists; so...
		# ... Clone the first boot definition and change the version.
		local TYPE
		[ "${KERN_24}" -eq '1' ] && TYPE='rd' || TYPE='ramfs'

		cp $GRUB_CONF $GRUB_CONF.bak
		awk 'BEGIN { RS="" ; FS="\n" ; OFS="\n" ; ORS="\n\n" } 
			NR == 2 {
				ORIG=$0;
				sub(/\(.+\)/,"(" KV ")",$1);
				sub(/kernel-[[:alnum:][:punct:]]+/, "kernel-" KNAME "-" ARCH "-" KV, $3);
				sub(/initr(d|amfs)-[[:alnum:][:punct:]]+/, "init" TYPE "-" KNAME "-" ARCH "-" KV, $4);
				print RS $0; 
				print RS ORIG;}
			NR != 2 { print RS $0; }' KNAME=$KNAME ARCH=$ARCH KV=$KV TYPE=$TYPE $GRUB_CONF.bak > $GRUB_CONF
	fi
}