summaryrefslogtreecommitdiff
path: root/backend/modules/bootloader
blob: c4f366caea197bbd85aebda5834a74e2ea408425 (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
#!/bin/bash

# Synopsis: list_bootloader_targets
#
# This function lists all disks and the root-partition if it is suitable to install a bootloader on it.
# Output example:
#   /dev/sda 250059350016 Master Boot Record
#   /dev/sdb 400088457216 Master Boot Record
#   /dev/sdb1 60003385344 Rootpartition (filesysem ext4)
function list_bootloader_targets()
{
    if [ -d /sys/firmware/efi ]; then
        root_disk="$(get_disk "$(hdmap_get device of mountpoint /)")"
        for efi_part in $(list_efi_partitions | grep "$root_disk"; list_efi_partitions | grep -v "$root_disk")
        do
            echo "$efi_part $(blockdev --getsize64 $efi_part) EFI partition"
        done
    else
        for disk in $(list_all_disks)
        do
                echo "$disk $(blockdev --getsize64 $disk) Master Boot Record"
        done
	root_dev="$(hdmap_get device of mountpoint /)"
	root_fs="$(hdmap_get filesystem of mountpoint /)"
        [ -z "$root_fs" ] && root_fs=$(get_filesystem "$root_dev")
        case $root_fs in
        reiserfs|ext2|ext3|ext4)
                list_linux_partitions | grep -q "^$root_dev$" &&
                echo "$root_dev $(blockdev --getsize64 $root_dev) Rootpartition (filesystem $root_fs)"
                ;;
        esac
    fi
}

function send_bootloader_targets()
{
	send data bootloader_targets
	list_bootloader_targets
}

# Synopsis: list_bootloaders
#
# This function lists all available bootloaders
# Output example:
#   BURG - Brand-new Universal loadeR from GRUB
#   GRUB - GRand Unified Bootloader
function list_bootloaders()
{
        for bl in burg grub
        do
                [ -x /usr/sbin/$bl-install ] || continue
                case $bl in
                burg) echo "BURG - Brand-new Universal loadeR from GRUB";;
                grub) echo "GRUB - GRand Unified Bootloader";;
                esac
        done
}

function send_bootloaders()
{
	send data bootloaders
	list_bootloaders
}

function install_bootmanager_to_target()
{
    send install_step install_bootmanager_to_target
    # force initrd update
    chroot_it update-initramfs -utk all &>/dev/null

    case "$cfg_bootloader" in
    grub)
        install_grub
        ;;
    burg)
        install_burg
        ;;
    esac
}

function install_grub()
{
    rm -f $TARGET/boot/vmlinuz $TARGET/boot/System.map $TARGET/boot/initrd.img

    apple=0
    dmidecode -s system-manufacturer | grep -q "Apple Inc." && apple=1

    # install grub
    mkdir -p "$TARGET/boot/grub"
    if [ -d /sys/firmware/efi ]; then
        if is_removeable "$cfg_bootloader_target" || [ "$apple" = 1 ]; then
            chroot_it grub-install --removable &>/dev/null
        else
            chroot_it grub-install &>/dev/null
        fi
    else
        grub-install --recheck --no-floppy --root-directory=$TARGET "$cfg_bootloader_target" &>/dev/null || \
        grub-install --force --recheck --no-floppy --root-directory=$TARGET "$cfg_bootloader_target" &>/dev/null

        # preseed grub-pc with install-target
        for path in /dev/disk/by-id/*
        do
            [ -e "$path" ] || continue
            if [ "$(readlink -f "$path")" = "$(readlink -f "$cfg_bootloader_target")" ]; then
                echo "grub-pc grub-pc/install_devices multiselect $path" | chroot_it debconf-set-selections &>/dev/null
            break
        fi
        done
    fi
    
    write_kernel_img_conf

    # gfx=auto hack
    grep -q gfx=auto /proc/cmdline && chroot_it sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=\).*/\1"quiet rw gfx=auto"/;s/\(^GRUB_CMDLINE_LINUX=\).*/\1""/' /etc/default/grub

    # update grub
    chroot_it update-grub &>/dev/null
    DEBIAN_FRONTEND=noninteractive chroot_it dpkg-reconfigure grub-pc &>/dev/null
    
    return 0
}

function install_burg()
{
    rm -f $TARGET/boot/vmlinuz $TARGET/boot/System.map $TARGET/boot/initrd.img

    # install burg
    mkdir -p "$TARGET/boot/burg"
    burg-install --recheck --no-floppy --root-directory=$TARGET "$cfg_bootloader_target" &>/dev/null || \
    burg-install --force --recheck --no-floppy --root-directory=$TARGET "$cfg_bootloader_target" &>/dev/null

#    # create device.map and save it to target
#    export device_map=$TARGET/tmp/device.map
#    get_device_map > $device_map
#    cat $device_map > $TARGET/boot/burg/device.map
#    rm -f $device_map

    # preseed burg-pc with install-target
    for path in /dev/disk/by-id/*
    do
    	[ -e "$path" ] || continue
    	if [ "$(readlink -f "$path")" = "$(readlink -f "$cfg_bootloader_target")" ]; then
            echo "burg-pc burg-pc/install_devices multiselect $path" | chroot_it debconf-set-selections &>/dev/null
	    break
	fi
    done
    
    write_kernel_img_conf

    # update burg
    chroot_it update-burg &>/dev/null
    DEBIAN_FRONTEND=noninteractive chroot_it dpkg-reconfigure burg-pc &>/dev/null
    
    return 0
}

function write_kernel_img_conf()
{
rm -f $TARGET/etc/kernel-img.conf
cat << EOT > $TARGET/etc/kernel-img.conf
# Kernel image management overrides
# See kernel-img.conf(5) for details
do_symlinks = yes
relative_links = yes
do_bootloader = no
do_bootfloppy = no
do_initrd = yes
link_in_boot = no
postinst_hook = update-$cfg_bootloader
postrm_hook   = update-$cfg_bootloader
EOT
}