Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[build/onie installer] Install grub for SONiC post migration from another NOS #949

Merged
merged 2 commits into from
Sep 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion build_debian.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ if [[ -d $FILESYSTEM_ROOT ]]; then
fi
mkdir -p $FILESYSTEM_ROOT
mkdir -p $FILESYSTEM_ROOT/$PLATFORM_DIR
mkdir -p $FILESYSTEM_ROOT/$PLATFORM_DIR/x86_64-grub
touch $FILESYSTEM_ROOT/$PLATFORM_DIR/firsttime

## Build a basic Debian system by debootstrap
Expand Down Expand Up @@ -205,7 +206,13 @@ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y in
curl \
kexec-tools \
less \
unzip
unzip \
gdisk

sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y download \
grub-pc-bin

sudo mv $FILESYSTEM_ROOT/grub-pc-bin*.deb $FILESYSTEM_ROOT/$PLATFORM_DIR/x86_64-grub

## Disable kexec supported reboot which was installed by default
sudo sed -i 's/LOAD_KEXEC=true/LOAD_KEXEC=false/' $FILESYSTEM_ROOT/etc/default/kexec
Expand Down
106 changes: 104 additions & 2 deletions files/image_config/platform/rc.local
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,34 @@ if [ ! -e /host/machine.conf ]; then
done
fi

migration="TRUE"
umount /mnt/onie-boot
fi

. /host/machine.conf

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

firsttime_exit()
{
rm /host/image-$sonic_version/platform/firsttime
exit 0
}

# Given a string of tuples of the form field=value, extract the value for a field
# In : $string, $field
# Out: $value
value_extract()
{
set -- $string
for x in "$@"; do
case "$x" in
$field=*)
value="${x#$field=}"
esac
done
}

sonic_version=$(cat /etc/sonic/sonic_version.yml | grep build_version | cut -f2 -d" ")

if [ -f /host/image-$sonic_version/platform/firsttime ]; then
Expand All @@ -46,8 +67,7 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then
platform=$onie_platform
else
echo "Unknown sonic platform"
rm /host/image-$sonic_version/platform/firsttime
exit 0
firsttime_exit
fi

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

# If the unit booted into SONiC from another NOS's grub,
# we now install a grub for SONiC.
if [ -n "$onie_platform" ] && [ -n "$migration" ]; then

grub_bin=$(ls /host/image-$sonic_version/platform/x86_64-grub/grub-pc-bin*.deb 2> /dev/null)
if [ -z "$grub_bin" ]; then
echo "Unable to locate grub package !" >> /etc/migration.log
firsttime_exit
fi

dpkg -i $grub_bin > /dev/null 2>&1
if [ $? != 0 ]; then
echo "Unable to install grub package !" >> /etc/migration.log
firsttime_exit
fi

# Determine the block device to install grub
sonic_dev=$(blkid | grep SONiC-OS | head -n 1 | awk '{print $1}' | sed -e 's/[0-9]:.*$//')
if [ -z "$sonic_dev" ]; then
echo "Unable to determine sonic partition !" >> /etc/migration.log
firsttime_exit
fi

grub-install --boot-directory=/host --recheck $sonic_dev 2>/dev/null
if [ $? != 0 ]; then
echo "grub install failed !" >> /etc/migration.log
firsttime_exit
fi

# The SONiC "raw" build mode has already generated a proto grub.cfg
# as part of the migration. Platform specific constants need to be
# retrieved from installer.conf (if present) and assigned.
. /usr/share/sonic/device/$platform/installer.conf

if [ ! -z "$CONSOLE_PORT" ]; then
field="\-\-port"
string=$(grep $field /host/grub.cfg)
value_extract $string $field
console_port=$value
if [ ! -z "$console_port" ] && [ "$console_port" != "$CONSOLE_PORT" ]; then
sed -i -e "s/\-\-port=$console_port/\-\-port=$CONSOLE_PORT/g" /host/grub.cfg
fi
echo "grub.cfg console port=$console_port & installer.conf CONSOLE_PORT=$CONSOLE_PORT" >> /etc/migration.log
fi

if [ ! -z "$CONSOLE_DEV" ]; then
field="console"
string=$(grep $field /host/grub.cfg)
value_extract $string $field
console_dev_name=$(echo $value | sed -e "s/^.*=//" -e "s/,.*//")
console_dev="${console_dev_name#ttyS}"
if [ "$console_dev" != "$CONSOLE_DEV" ]; then
sed -i -e "s/console=ttyS$console_dev/console=ttyS$CONSOLE_DEV/g" /host/grub.cfg
fi
echo "grub.cfg console dev=$console_dev & installer.conf CONSOLE_DEV=$CONSOLE_DEV" >> /etc/migration.log
fi

if [ ! -z "$VAR_LOG_SIZE" ]; then
field="var_log_size"
string=$(grep $field /host/grub.cfg)
value_extract $string $field
var_log_size=$value
if [ ! -z "$var_log_size" ] && [ "$var_log_size" != "$VAR_LOG_SIZE" ]; then
sed -i -e "s/var_log_size=$var_log_size/var_log_size=$VAR_LOG_SIZE/g" /host/grub.cfg
fi
echo "grub.cfg var_log_size=$var_log_size & installer.conf VAR_LOG_SIZE=$VAR_LOG_SIZE" >> /etc/migration.log
fi

# Set the root based on the label
sonic_root=$(blkid | grep SONiC-OS | head -n 1 | awk '{print $1}' | sed -e 's/:.*$//')
sonic_root=$(echo "$sonic_root" | sed 's/\//\\\//g')
sed -i -e "s/%%SONIC_ROOT%%/$sonic_root/g" /host/grub.cfg

# Add the Diag and ONIE entries
mount $onie_dev /mnt/onie-boot
. /mnt/onie-boot/onie/grub.d/50_onie_grub >> /host/grub.cfg
umount /mnt/onie-boot

# Initialize the SONiC's grub config
mv /host/grub.cfg /host/grub/grub.cfg
fi

rm /host/image-$sonic_version/platform/firsttime
fi

Expand Down
11 changes: 9 additions & 2 deletions installer/x86_64/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ else
demo_mnt="build_raw_image_mnt"
demo_dev=$cur_wd/"%%OUTPUT_RAW_IMAGE%%"

mkfs.ext4 $demo_dev
mkfs.ext4 -L $demo_volume_label $demo_dev

echo "Mounting $demo_dev on $demo_mnt..."
mkdir $demo_mnt
Expand Down Expand Up @@ -535,6 +535,12 @@ if [ "$install_env" = "sonic" ]; then
onie_menuentry=$(cat /host/grub/grub.cfg | sed "/menuentry ONIE/,/}/!d")
fi

if [ "$install_env" = "build" ]; then
grub_cfg_root=%%SONIC_ROOT%%
else
grub_cfg_root=$demo_dev
fi

cat <<EOF >> $grub_cfg
menuentry '$demo_grub_entry' {
search --no-floppy --label --set=root $demo_volume_label
Expand All @@ -543,7 +549,7 @@ menuentry '$demo_grub_entry' {
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_msdos
insmod ext2
linux /$image_dir/boot/vmlinuz-3.16.0-4-amd64 root=$demo_dev rw $GRUB_CMDLINE_LINUX \
linux /$image_dir/boot/vmlinuz-3.16.0-4-amd64 root=$grub_cfg_root rw $GRUB_CMDLINE_LINUX \
loop=$image_dir/$FILESYSTEM_SQUASHFS loopfstype=squashfs \
apparmor=1 security=apparmor varlog_size=$VAR_LOG_SIZE $ONIE_PLATFORM_EXTRA_CMDLINE_LINUX
Copy link
Collaborator

@lguohan lguohan Sep 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the rc.local, you also need to replace the VAR_LOG_SIZE in case the VAR_LOG_SIZE is overriden in the installer.conf

echo 'Loading $demo_volume_label $demo_type initial ramdisk ...'
Expand All @@ -564,6 +570,7 @@ EOF
fi

if [ "$install_env" = "build" ]; then
cp $grub_cfg $demo_mnt/grub.cfg
umount $demo_mnt
else
cp $grub_cfg $onie_initrd_tmp/$demo_mnt/grub/grub.cfg
Expand Down