-
Notifications
You must be signed in to change notification settings - Fork 0
/
setboot.sh
executable file
·223 lines (198 loc) · 5.75 KB
/
setboot.sh
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
if [ ! "${HAVESCRIPTUTILS:+present}" ]; then
echo "This script must be run under voyage.update" >&2
exit
fi
#
# Function make_lilo_conf()
# Creates two lilo config files on $TARGET_MOUNT - one for installation
# (/etc/lilo.install.conf) and one for the runtime system
# (/etc/lilo.conf). Uses the VOYAGE_SYSTEM_xxxx and TARGET_xxxx
# environment variables to control the generation
make_lilo_conf() {
local liloconf newlilo sercmd serapp
# First decide whether console is normal or serial
if [ $VOYAGE_SYSTEM_CONSOLE == serial ]; then
delay="delay=20"
sercmd="serial=0,${VOYAGE_SYSTEM_SERIAL}n8"
serapp="console=ttyS0,${VOYAGE_SYSTEM_SERIAL}n8 "
else
delay="delay=1"
sercmd=""
serapp=""
fi
#
# Generate our lilo config file
# Note that the 'boot', 'disk' and 'bios' params allow us
# to run lilo on our host machine, setting up the disk for
# our target machine.
#
cat > $TARGET_MOUNT/etc/lilo.install.conf << EOM
#
# This file generated automatically by $0
# on `date`
#
boot = $TARGET_DISK
disk = $TARGET_DISK
bios = 0x80
$delay
$sercmd
vga=normal
default=Linux
image=/vmlinuz
label=Linux
initrd=/initrd.img
read-only
append="root=LABEL=ROOT_FS ${serapp}reboot=bios"
image=/vmlinuz.old
label=LinuxOLD
initrd=/initrd.img.old
read-only
append="root=LABEL=ROOT_FS ${serapp}reboot=bios"
optional
EOM
sed -e "/disk =/d;/bios =/d" -e "s#${TARGET_DISK}#/dev/hda#" \
$TARGET_MOUNT/etc/lilo.install.conf > \
$TARGET_MOUNT/etc/lilo.conf
}
update_lilo()
{
local liloconf newlilo
echo "" >&2
echo "Running lilo ...." >&2
# Generate the configuration file
make_lilo_conf
# Set up /dev and /proc on target to use host devices
mount -o bind /dev ${TARGET_MOUNT}/dev
mount -o bind /proc ${TARGET_MOUNT}/proc
chroot $TARGET_MOUNT lilo -C /etc/lilo.install.conf
# Save the exit status, to check after umount'ing device stuff
res=$?
# Undo the previous 2 mounts
umount ${TARGET_MOUNT}/proc
umount ${TARGET_MOUNT}/dev
# Check the saved exit status
if [ $res -ne 0 ]; then
err_quit "Failure during chroot to $MOUNTDISK to run lilo"
fi
}
#
# For a grub install, we try check whether the boot partition seems to
# already have grub installed. If it does, then we only need to add
# the current kernel image to the menu.
#
# If grub is not yet installed, we must install it.
#
update_grub()
{
local console datestr fname gp prolog res
# if the boot partition is separate from the main one,
# mount it on $TARGET_MOUNT/rw, otherwise it will be
# $TARGET_MOUNT/boot
# $ghome is the name relative to the voyage target directory
# $gp is the full pathname to where the grub directory is
if [ $BOOTSTRAP_PART -ne $TARGET_PART ]; then
ghome=/rw
gp=${TARGET_MOUNT}${ghome}
mount ${TARGET_DISK}${BOOTSTRAP_PART} $gp || \
err_quit "Failed to mount ${TARGET_DISK}${BOOTSTRAP_PART}" \
" on $gp"
else
ghome=/boot
gp=${TARGET_MOUNT}${ghome}
fi
if [ ! -d ${gp}/grub ]; then
# create the grub directory in the boot partition
mkdir ${gp}/grub
# copy the grub files into it
echo "Copy grub files from ${TARGET_MOUNT} to ${gp}/grub"
if [ -d ${TARGET_MOUNT}/lib/grub/i386-pc/ ] ; then
cp ${TARGET_MOUNT}/lib/grub/i386-pc/* ${gp}/grub
elif [ -d ${TARGET_MOUNT}/usr/lib/grub/i386-pc/ ] ; then
cp ${TARGET_MOUNT}/usr/lib/grub/i386-pc/* ${gp}/grub
elif [ -d ${TARGET_MOUNT}/lib/grub/x86_64-pc/ ] ; then
cp ${TARGET_MOUNT}/lib/grub/x86_64-pc/* ${gp}/grub
elif [ -d ${TARGET_MOUNT}/usr/lib/grub/x86_64-pc/ ] ; then
cp ${TARGET_MOUNT}/usr/lib/grub/x86_64-pc/* ${gp}/grub
else
err_quit "Can't find grub files - exiting"
fi
# create a grub device map for the installation
dm="/grub/device.map"
echo "(hd0) $TARGET_DISK" > ${gp}${dm}
# We are going to 'chroot' over to our target, but because
# we will be running 'grub' there, we will need the /dev
# directory from our current system.
mount -o bind /dev $TARGET_MOUNT/dev
echo "Setting up grub under chroot $TARGET_MOUNT"
# note the arithmetic evaluation (grub uses '0' as
# the first partition)
res=`chroot $TARGET_MOUNT /usr/sbin/grub \
--device-map=${ghome}${dm} 2>&1 <<EOM
setup (hd0) (hd0,$(($BOOTSTRAP_PART-1)))
quit
EOM`
if [ $? -ne 0 ]; then
umount $TARGET_MOUNT/dev
err_quit "Trouble running grub - dialog was: $res"
fi
umount $TARGET_MOUNT/dev
rm -f ${gp}${dm}
fi
# common code whether grub already installed or not
if [ $VOYAGE_SYSTEM_CONSOLE == serial ]; then
prolog="serial --speed=$VOYAGE_SYSTEM_SERIAL
terminal serial
"
console=" console=ttyS0,${VOYAGE_SYSTEM_SERIAL}n8"
else
prolog=""
console=""
fi
# sanity - if menu.lst doesn't exist, create it
# (this will always happen on a new installation)
if [ ! -f ${gp}/grub/menu.lst ]; then
cat <<EOM > ${gp}/grub/menu.lst
#
# This file generated automatically by $0
# on `date`
#
$prolog
timeout 5
default 0
EOM
fi
# This will test if /initrd.img exist, then grub will enable initrd
# and append to menu.lst
if [ -f ${TARGET_MOUNT}/initrd.img ] ; then
VOYAGE_INITRD="initrd /initrd.img"
fi
# generate a label with today's date
# and append to menu.lst
datestr=`date +%d%b%y`
cat <<EOM >> ${gp}/grub/menu.lst
title voyage-linux-$datestr
root (hd0,$(($TARGET_PART-1)))
kernel /vmlinuz root=LABEL=ROOT_FS ${console}
${VOYAGE_INITRD}
EOM
if [ $BOOTSTRAP_PART -ne $TARGET_PART ]; then
umount ${TARGET_DISK}${BOOTSTRAP_PART} || \
err_quit "Failed to unmount ${TARGET_DISK}${BOOTSTRAP_PART}"
fi
}
###############################################
# Mainline code starts here #
###############################################
if [ $SYSTEM_BOOTSTRAP != grub -a \
$SYSTEM_BOOTSTRAP != lilo ]; then
select_target_boot
fi
if [ $SYSTEM_BOOTSTRAP == lilo ]; then
echo "Installing lilo"
make_lilo_conf
update_lilo
else
echo "Installing grub"
update_grub
fi