]>
Commit | Line | Data |
---|---|---|
ff3bae7b MT |
1 | #!/bin/bash |
2 | ############################################################################ | |
3 | # # | |
4 | # This file is part of the IPFire Firewall. # | |
5 | # # | |
6 | # IPFire is free software; you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation; either version 2 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # IPFire is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with IPFire; if not, write to the Free Software # | |
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # | |
19 | # # | |
20 | # Copyright (C) 2014 IPFire Team <info@ipfire.org>. # | |
21 | # # | |
22 | ############################################################################ | |
23 | ||
24 | GRUB_INSTALL_ARGS="--no-floppy --recheck" | |
25 | ||
26 | function find_root_device() { | |
27 | # rootfs / rootfs rw 0 0 | |
28 | ||
29 | local root | |
30 | local dev mp fs flags rest | |
31 | while read -r dev mp fs flags rest; do | |
32 | # Skip unwanted entries | |
33 | [ "${dev}" = "rootfs" ] && continue | |
34 | ||
35 | if [ "${mp}" = "/" ] && [ -b "${dev}" ]; then | |
36 | root="$(basename "${dev}")" | |
37 | break | |
38 | fi | |
39 | done < /proc/mounts | |
40 | ||
41 | # Get the actual device from the partition that holds / | |
42 | if [ -n "${root}" ]; then | |
43 | while [ -n "${root}" ]; do | |
44 | if [ -e "/sys/block/${root}" ]; then | |
45 | echo "${root}" | |
46 | return 0 | |
47 | fi | |
48 | ||
49 | # Remove last character | |
50 | root="${root::-1}" | |
51 | done | |
52 | fi | |
53 | ||
54 | return 1 | |
55 | } | |
56 | ||
57 | function root_device_is_mdraid() { | |
58 | local device="${1}" | |
59 | ||
60 | [ -d "/sys/block/${device}/md" ] | |
61 | } | |
62 | ||
63 | function mdraid_get_slaves() { | |
64 | local device="${1}" | |
65 | ||
66 | local slave | |
67 | for slave in /sys/block/${device}/slaves/*; do | |
68 | basename "${slave}" | |
69 | done 2>/dev/null | |
70 | } | |
71 | ||
72 | function grub_update_config() { | |
73 | echo "Updating configuration..." | |
74 | ||
75 | if ! grub-mkconfig -o /boot/grub/grub.cfg &>/dev/null; then | |
76 | echo "Could not update configuration. Aborting." >&2 | |
77 | return 1 | |
78 | fi | |
79 | ||
80 | return 0 | |
81 | } | |
82 | ||
83 | function grub_install() { | |
84 | local device="${1}" | |
85 | ||
86 | echo "Installing GRUB on ${device}..." | |
87 | ||
88 | if [ ! -b "${device}" ]; then | |
89 | echo "${device} does not exist or is not a block device" >&2 | |
90 | return 1 | |
91 | fi | |
92 | ||
93 | local args | |
94 | for args in "" "--force"; do | |
95 | if grub-install ${GRUB_INSTALL_ARGS} ${args} "${device}" &>/dev/null; then | |
96 | return 0 | |
97 | fi | |
98 | done | |
99 | ||
100 | echo "Could not install GRUB on ${device}" >&2 | |
101 | return 1 | |
102 | } | |
103 | ||
104 | function main() { | |
105 | # Find the root device | |
106 | local device="$(find_root_device)" | |
107 | if [ -z "${device}" ]; then | |
108 | echo "Could not find root device. Aborting." >&2 | |
109 | exit 1 | |
110 | fi | |
111 | ||
112 | echo "Found root device: /dev/${device}" | |
113 | ||
114 | # Update configuration files | |
115 | grub_update_config || exit $? | |
116 | ||
117 | # Handle mdraid devices | |
118 | if root_device_is_mdraid "${device}"; then | |
119 | local slave | |
120 | for slave in $(mdraid_get_slaves "${device}"); do | |
121 | grub_install "/dev/${slave}" | |
122 | done | |
123 | ||
124 | # Handle normal block devices | |
125 | else | |
126 | grub_install "/dev/${device}" | |
127 | fi | |
128 | ||
129 | return 0 | |
130 | } | |
131 | ||
132 | # Run main function | |
133 | main |