Fix #97672, #98886, #98893, #98897; fix real_root=/dev/mdX: upgraded busybox to 1...
[genkernel.git] / gen_bootloader.sh
1 #!/bin/bash
2
3 set_bootloader() {
4         if [ "x$BOOTLOADER" == 'xgrub' ]
5         then
6                 set_grub_bootloader
7         else
8                 return 0
9         fi
10 }
11
12 set_grub_bootloader() {
13         local GRUB_CONF='/boot/grub/grub.conf'
14
15         print_info 1 ''
16         print_info 1 "Adding kernel to $GRUB_CONF..."
17
18         # Extract block device information from /etc/fstab
19         local GRUB_ROOTFS=$(awk '/[[:space:]]\/[[:space:]]/ { print $1 }' /etc/fstab)
20         local GRUB_BOOTFS=$(awk '/^[^#].+[[:space:]]\/boot[[:space:]]/ { print $1 }' /etc/fstab)
21
22         # If /boot is not defined in /etc/fstab, it must be the same as /
23         [ "x$GRUB_BOOTFS" == 'x' ] && GRUB_BOOTFS=$GRUB_ROOTFS
24
25         # Read GRUB device map
26         [ ! -d ${tmp} ] && mkdir ${tmp}
27         grub --batch --device-map=${tmp}/grub.map <<EOF >/dev/null
28 quit
29 EOF
30
31         # Get the GRUB mapping for our device
32         local GRUB_BOOT_DISK1=$(echo $GRUB_BOOTFS | sed -e 's#\(/dev/.\+\)[[:digit:]]\+#\1#')
33         local GRUB_BOOT_DISK=$(awk '{if ($2 == "'$GRUB_BOOT_DISK1'") {gsub(/(\(|\))/, "", $1); print $1;}}' ${tmp}/grub.map)
34
35         local GRUB_BOOT_PARTITION=$(echo $GRUB_BOOTFS | sed -e 's#/dev/.\+\([[:digit:]]\+\)#\1#')
36         [ ! -d ${tmp} ] && rm -r ${tmp}
37         
38         # Create grub configuration directory and file if it doesn't exist.
39         [ ! -e `basename $GRUB_CONF` ] && mkdir -p `basename $GRUB_CONF`
40
41         if [ ! -e $GRUB_CONF ]
42         then
43                 if [ "${GRUB_BOOT_DISK}" = '' -o "${GRUB_BOOT_PARTITION}" = '' ]
44                 then
45                         print_error 1 'Error! /boot/grub/grub.conf does not exist and the correct settings can\'t be automatically detected.'
46                         print_error 1 'Please manually create your /boot/grub/grub.conf file.'
47                 else
48                         # grub.conf doesn't exist - create it with standard defaults
49                         touch $GRUB_CONF
50                         echo 'default 0' >> $GRUB_CONF
51                         echo 'timeout 5' >> $GRUB_CONF
52                         echo >> $GRUB_CONF
53
54                         # Add grub configuration to grub.conf   
55                         echo "# Genkernel generated entry, see GRUB documentation for details" >> $GRUB_CONF
56                         echo "title=Gentoo Linux ($KV)" >> $GRUB_CONF
57                         echo -e "\troot ($GRUB_BOOT_DISK,$GRUB_BOOT_PARTITION)" >> $GRUB_CONF
58                         if [ "${BUILD_INITRD}" -eq '0' ]
59                         then
60                                 echo -e "\tkernel /kernel-${KNAME}-${ARCH}-${KV} root=$GRUB_ROOTFS" >> $GRUB_CONF
61                         else
62                                 echo -e "\tkernel /kernel-${KNAME}-${ARCH}-${KV} root=/dev/ram0 init=/linuxrc real_root=$GRUB_ROOTFS" >> $GRUB_CONF
63                                 if [ "${PAT}" -gt '4' -a  "${CMD_BOOTSPLASH}" != '1' ]
64                                 then
65                                     echo -e "\tinitrd /initramfs-${KNAME}-${ARCH}-${KV}" >> $GRUB_CONF
66                                 else
67                                     echo -e "\tinitrd /initrd-${KNAME}-${ARCH}-${KV}" >> $GRUB_CONF
68                                 fi
69                         fi
70                         echo >> $GRUB_CONF
71                 fi
72         else
73                 # grub.conf already exists; so...
74                 # ... Clone the first boot definition and change the version.
75                 local TYPE
76                 [ "${KERN_24}" -eq '1' ] && TYPE='rd' || TYPE='ramfs'
77
78                 cp $GRUB_CONF $GRUB_CONF.bak
79                 awk 'BEGIN { RS="" ; FS="\n" ; OFS="\n" ; ORS="\n\n" } 
80                         NR == 2 {
81                                 ORIG=$0;
82                                 sub(/\(.+\)/,"(" KV ")",$1);
83                                 sub(/kernel-[[:alnum:][:punct:]]+/, "kernel-" KNAME "-" ARCH "-" KV, $3);
84                                 sub(/initr(d|amfs)-[[:alnum:][:punct:]]+/, "init" TYPE "-" KNAME "-" ARCH "-" KV, $4);
85                                 print RS $0; 
86                                 print RS ORIG;}
87                         NR != 2 { print RS $0; }' KNAME=$KNAME ARCH=$ARCH KV=$KV TYPE=$TYPE $GRUB_CONF.bak > $GRUB_CONF
88         fi
89 }