Skip to content

Commit 6935e00

Browse files
padmanarayanalguohan
authored andcommitted
[build/onie installer] Install grub for SONiC post migration from another NOS (#949)
* Install grub for SONiC post migration from another NOS * Install grub from bundled debian package instead of using ONIE's. Address review comments
1 parent 9d321fa commit 6935e00

File tree

3 files changed

+121
-5
lines changed

3 files changed

+121
-5
lines changed

build_debian.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ if [[ -d $FILESYSTEM_ROOT ]]; then
6363
fi
6464
mkdir -p $FILESYSTEM_ROOT
6565
mkdir -p $FILESYSTEM_ROOT/$PLATFORM_DIR
66+
mkdir -p $FILESYSTEM_ROOT/$PLATFORM_DIR/x86_64-grub
6667
touch $FILESYSTEM_ROOT/$PLATFORM_DIR/firsttime
6768

6869
## Build a basic Debian system by debootstrap
@@ -205,7 +206,13 @@ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y in
205206
curl \
206207
kexec-tools \
207208
less \
208-
unzip
209+
unzip \
210+
gdisk
211+
212+
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y download \
213+
grub-pc-bin
214+
215+
sudo mv $FILESYSTEM_ROOT/grub-pc-bin*.deb $FILESYSTEM_ROOT/$PLATFORM_DIR/x86_64-grub
209216

210217
## Disable kexec supported reboot which was installed by default
211218
sudo sed -i 's/LOAD_KEXEC=true/LOAD_KEXEC=false/' $FILESYSTEM_ROOT/etc/default/kexec

files/image_config/platform/rc.local

+104-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,34 @@ if [ ! -e /host/machine.conf ]; then
2929
done
3030
fi
3131

32+
migration="TRUE"
3233
umount /mnt/onie-boot
3334
fi
3435

3536
. /host/machine.conf
3637

3738
echo "install platform dependent packages at the first boot time"
3839

40+
firsttime_exit()
41+
{
42+
rm /host/image-$sonic_version/platform/firsttime
43+
exit 0
44+
}
45+
46+
# Given a string of tuples of the form field=value, extract the value for a field
47+
# In : $string, $field
48+
# Out: $value
49+
value_extract()
50+
{
51+
set -- $string
52+
for x in "$@"; do
53+
case "$x" in
54+
$field=*)
55+
value="${x#$field=}"
56+
esac
57+
done
58+
}
59+
3960
sonic_version=$(cat /etc/sonic/sonic_version.yml | grep build_version | cut -f2 -d" ")
4061

4162
if [ -f /host/image-$sonic_version/platform/firsttime ]; then
@@ -46,8 +67,7 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then
4667
platform=$onie_platform
4768
else
4869
echo "Unknown sonic platform"
49-
rm /host/image-$sonic_version/platform/firsttime
50-
exit 0
70+
firsttime_exit
5171
fi
5272

5373
# Try to take old configuration saved during installation
@@ -76,6 +96,88 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then
7696
dpkg -i /host/image-$sonic_version/platform/$platform/*.deb
7797
fi
7898

99+
# If the unit booted into SONiC from another NOS's grub,
100+
# we now install a grub for SONiC.
101+
if [ -n "$onie_platform" ] && [ -n "$migration" ]; then
102+
103+
grub_bin=$(ls /host/image-$sonic_version/platform/x86_64-grub/grub-pc-bin*.deb 2> /dev/null)
104+
if [ -z "$grub_bin" ]; then
105+
echo "Unable to locate grub package !" >> /etc/migration.log
106+
firsttime_exit
107+
fi
108+
109+
dpkg -i $grub_bin > /dev/null 2>&1
110+
if [ $? != 0 ]; then
111+
echo "Unable to install grub package !" >> /etc/migration.log
112+
firsttime_exit
113+
fi
114+
115+
# Determine the block device to install grub
116+
sonic_dev=$(blkid | grep SONiC-OS | head -n 1 | awk '{print $1}' | sed -e 's/[0-9]:.*$//')
117+
if [ -z "$sonic_dev" ]; then
118+
echo "Unable to determine sonic partition !" >> /etc/migration.log
119+
firsttime_exit
120+
fi
121+
122+
grub-install --boot-directory=/host --recheck $sonic_dev 2>/dev/null
123+
if [ $? != 0 ]; then
124+
echo "grub install failed !" >> /etc/migration.log
125+
firsttime_exit
126+
fi
127+
128+
# The SONiC "raw" build mode has already generated a proto grub.cfg
129+
# as part of the migration. Platform specific constants need to be
130+
# retrieved from installer.conf (if present) and assigned.
131+
. /usr/share/sonic/device/$platform/installer.conf
132+
133+
if [ ! -z "$CONSOLE_PORT" ]; then
134+
field="\-\-port"
135+
string=$(grep $field /host/grub.cfg)
136+
value_extract $string $field
137+
console_port=$value
138+
if [ ! -z "$console_port" ] && [ "$console_port" != "$CONSOLE_PORT" ]; then
139+
sed -i -e "s/\-\-port=$console_port/\-\-port=$CONSOLE_PORT/g" /host/grub.cfg
140+
fi
141+
echo "grub.cfg console port=$console_port & installer.conf CONSOLE_PORT=$CONSOLE_PORT" >> /etc/migration.log
142+
fi
143+
144+
if [ ! -z "$CONSOLE_DEV" ]; then
145+
field="console"
146+
string=$(grep $field /host/grub.cfg)
147+
value_extract $string $field
148+
console_dev_name=$(echo $value | sed -e "s/^.*=//" -e "s/,.*//")
149+
console_dev="${console_dev_name#ttyS}"
150+
if [ "$console_dev" != "$CONSOLE_DEV" ]; then
151+
sed -i -e "s/console=ttyS$console_dev/console=ttyS$CONSOLE_DEV/g" /host/grub.cfg
152+
fi
153+
echo "grub.cfg console dev=$console_dev & installer.conf CONSOLE_DEV=$CONSOLE_DEV" >> /etc/migration.log
154+
fi
155+
156+
if [ ! -z "$VAR_LOG_SIZE" ]; then
157+
field="var_log_size"
158+
string=$(grep $field /host/grub.cfg)
159+
value_extract $string $field
160+
var_log_size=$value
161+
if [ ! -z "$var_log_size" ] && [ "$var_log_size" != "$VAR_LOG_SIZE" ]; then
162+
sed -i -e "s/var_log_size=$var_log_size/var_log_size=$VAR_LOG_SIZE/g" /host/grub.cfg
163+
fi
164+
echo "grub.cfg var_log_size=$var_log_size & installer.conf VAR_LOG_SIZE=$VAR_LOG_SIZE" >> /etc/migration.log
165+
fi
166+
167+
# Set the root based on the label
168+
sonic_root=$(blkid | grep SONiC-OS | head -n 1 | awk '{print $1}' | sed -e 's/:.*$//')
169+
sonic_root=$(echo "$sonic_root" | sed 's/\//\\\//g')
170+
sed -i -e "s/%%SONIC_ROOT%%/$sonic_root/g" /host/grub.cfg
171+
172+
# Add the Diag and ONIE entries
173+
mount $onie_dev /mnt/onie-boot
174+
. /mnt/onie-boot/onie/grub.d/50_onie_grub >> /host/grub.cfg
175+
umount /mnt/onie-boot
176+
177+
# Initialize the SONiC's grub config
178+
mv /host/grub.cfg /host/grub/grub.cfg
179+
fi
180+
79181
rm /host/image-$sonic_version/platform/firsttime
80182
fi
81183

installer/x86_64/install.sh

+9-2
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ else
423423
demo_mnt="build_raw_image_mnt"
424424
demo_dev=$cur_wd/"%%OUTPUT_RAW_IMAGE%%"
425425
426-
mkfs.ext4 $demo_dev
426+
mkfs.ext4 -L $demo_volume_label $demo_dev
427427
428428
echo "Mounting $demo_dev on $demo_mnt..."
429429
mkdir $demo_mnt
@@ -535,6 +535,12 @@ if [ "$install_env" = "sonic" ]; then
535535
onie_menuentry=$(cat /host/grub/grub.cfg | sed "/menuentry ONIE/,/}/!d")
536536
fi
537537
538+
if [ "$install_env" = "build" ]; then
539+
grub_cfg_root=%%SONIC_ROOT%%
540+
else
541+
grub_cfg_root=$demo_dev
542+
fi
543+
538544
cat <<EOF >> $grub_cfg
539545
menuentry '$demo_grub_entry' {
540546
search --no-floppy --label --set=root $demo_volume_label
@@ -543,7 +549,7 @@ menuentry '$demo_grub_entry' {
543549
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
544550
insmod part_msdos
545551
insmod ext2
546-
linux /$image_dir/boot/vmlinuz-3.16.0-4-amd64 root=$demo_dev rw $GRUB_CMDLINE_LINUX \
552+
linux /$image_dir/boot/vmlinuz-3.16.0-4-amd64 root=$grub_cfg_root rw $GRUB_CMDLINE_LINUX \
547553
loop=$image_dir/$FILESYSTEM_SQUASHFS loopfstype=squashfs \
548554
apparmor=1 security=apparmor varlog_size=$VAR_LOG_SIZE $ONIE_PLATFORM_EXTRA_CMDLINE_LINUX
549555
echo 'Loading $demo_volume_label $demo_type initial ramdisk ...'
@@ -564,6 +570,7 @@ EOF
564570
fi
565571
566572
if [ "$install_env" = "build" ]; then
573+
cp $grub_cfg $demo_mnt/grub.cfg
567574
umount $demo_mnt
568575
else
569576
cp $grub_cfg $onie_initrd_tmp/$demo_mnt/grub/grub.cfg

0 commit comments

Comments
 (0)