>> 3.0.2e. Code formatting fixes from Bob Barry, as well as a patch for bug
[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         # Translate block letters into grub numbers
26         local GRUB_ROOT_DISK=$(echo $GRUB_ROOTFS | sed -e 's/\/dev\/[hs]d\([[:alpha:]]\)[[:digit:]]\+/\1/')
27         case $GRUB_ROOT_DISK in
28                 a )
29                         GRUB_ROOT_DISK='0' ;;
30                 b )
31                         GRUB_ROOT_DISK='1' ;;
32                 c )
33                         GRUB_ROOT_DISK='2' ;;
34                 d )
35                         GRUB_ROOT_DISK='3' ;;
36                 e )
37                         GRUB_ROOT_DISK='4' ;;
38         esac
39
40         # Translate partition numbers into grub numbers
41         local GRUB_ROOT_PARTITION=$(echo $GRUB_BOOTFS | sed -e 's/\/dev\/[hs]d[[:alpha:]]\([[:digit:]]\+\)/\1/')
42         local GRUB_ROOT_PARTITION=$(($GRUB_ROOT_PARTITION-1))
43
44         # Create grub configuration directory and file if it doesn't exist.
45         [ ! -e `basename $GRUB_CONF` ] && mkdir -p `basename $GRUB_CONF`
46
47         if [ ! -e $GRUB_CONF ]
48         then
49                 # grub.conf doesn't exist - create it with standard defaults
50                 touch $GRUB_CONF
51                 echo 'default 0' >> $GRUB_CONF
52                 echo 'timeout 5' >> $GRUB_CONF
53
54                 # Add grub configuration to grub.conf   
55                 echo "title=Gentoo Linux ($KV)" >> $GRUB_CONF
56                 echo -e "\troot (hd$GRUB_ROOT_DISK,$GRUB_ROOT_PARTITION)" >> $GRUB_CONF
57                 if [ "${BUILD_INITRD}" -eq '0' ]
58                 then
59                         echo -e "\tkernel /kernel-$KV root=$GRUB_ROOTFS" >> $GRUB_CONF
60                 else
61                         echo -e "\tkernel /kernel-$KV root=/dev/ram0 init=/linuxrc real_root=$GRUB_ROOTFS" >> $GRUB_CONF
62                         echo -e "\tinitrd /initrd-$KV" >> $GRUB_CONF
63                 fi
64         else
65                 # grub.conf already exists; so...
66                 # * Copy the first boot definition and change the version.
67                 cp $GRUB_CONF $GRUB_CONF.bak
68                 awk 'BEGIN { RS="title=";FS="\n";OFS="\n";ORS=""} 
69                         NR == 2 { 
70                                 sub(/\(.+\)/,"(" ch KV ch ")",$1);
71                                 sub(/kernel-[[:alnum:][:punct:]]+/, "kernel-" KV, $3);
72                                 sub(/initrd-[[:alnum:][:punct:]]+/, "initrd-" KV, $4);
73                                 print RS ch $0 }' \
74                         KV=$KV $GRUB_CONF.bak >> $GRUB_CONF
75         fi
76 }