From 3dc503e65966dcc4b7b0d332af2bb8c54f7aaff7 Mon Sep 17 00:00:00 2001 From: padman Date: Sat, 9 Sep 2017 12:21:50 -0400 Subject: [PATCH 1/2] Install grub for SONiC post migration from another NOS --- build_debian.sh | 5 ++- files/image_config/platform/rc.local | 52 ++++++++++++++++++++++++++++ installer/x86_64/install.sh | 20 ++++++++--- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/build_debian.sh b/build_debian.sh index 21c370753592..49e231afbbfc 100755 --- a/build_debian.sh +++ b/build_debian.sh @@ -205,7 +205,10 @@ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y in curl \ kexec-tools \ less \ - unzip + unzip \ + xz-utils \ + gdisk + ## Disable kexec supported reboot which was installed by default sudo sed -i 's/LOAD_KEXEC=true/LOAD_KEXEC=false/' $FILESYSTEM_ROOT/etc/default/kexec diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local index 8b8db4a261c3..8ad4cf31a599 100755 --- a/files/image_config/platform/rc.local +++ b/files/image_config/platform/rc.local @@ -29,6 +29,7 @@ if [ ! -e /host/machine.conf ]; then done fi + migration="TRUE" umount /mnt/onie-boot fi @@ -76,6 +77,57 @@ 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 based on ONIE grub. + if [ -n "$onie_platform" ] && [ -n "$migration" ]; then + mkdir -p /host/image-$sonic_version/tmp + cd /host/image-$sonic_version/tmp + + # Extract ONIE's ramdisk which contain's the ONIE grub + mount $onie_dev /mnt/onie-boot + cp /mnt/onie-boot/onie/initrd.img-3.2.69-onie initrd.img-3.2.69-onie.xz + xz -d initrd.img-3.2.69-onie.xz + mv initrd.img-3.2.69-onie initrd.img-3.2.69-onie.cpio + cpio -idv < initrd.img-3.2.69-onie.cpio + cpio_retval=$? + + # Get the device and grub directory to be used for grub-install + sonic_dev=$(blkid | grep SONiC-OS | head -n 1 | awk '{print $1}' | sed -e 's/[0-9]:.*$//') + grub_platform_dir=`find . -name kernel.img -print` + grub_platform_dir=$(dirname "${grub_platform_dir}") + + # Install the grub for SONiC + if [ "$cpio_retval" -eq 0 ] && [ -n "$sonic_dev" ] && [ -n "$grub_platform_dir" ]; then + grub-install --boot-directory=/host --recheck $sonic_dev --directory=/host/image-$sonic_version/tmp/$grub_platform_dir 2>/dev/null + else + echo "Unable install grub" >> /etc/migration.log + 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 and assigned. + . /usr/share/sonic/device/$platform/installer.conf + + sed -i -e "s/%%CONSOLE_PORT%%/$CONSOLE_PORT/g" /host/grub.cfg + sed -i -e "s/%%CONSOLE_DEV%%/$CONSOLE_DEV/g" /host/grub.cfg + + # 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 + . /mnt/onie-boot/onie/grub.d/50_onie_grub >> /host/grub.cfg + + # Set the SONiC's grub config + mv /host/grub.cfg /host/grub/grub.cfg + + # Cleanup + cd /host/image-$sonic_version + rm -fr /host/image-$sonic_version/tmp + umount /mnt/onie-boot + fi + rm /host/image-$sonic_version/platform/firsttime fi diff --git a/installer/x86_64/install.sh b/installer/x86_64/install.sh index 46942d6d58c3..ddcc7d68d883 100755 --- a/installer/x86_64/install.sh +++ b/installer/x86_64/install.sh @@ -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 @@ -484,8 +484,13 @@ trap_push "rm $grub_cfg || true" [ -r ./platform.conf ] && . ./platform.conf -DEFAULT_GRUB_SERIAL_COMMAND="serial --port=${CONSOLE_PORT} --speed=${CONSOLE_SPEED} --word=8 --parity=no --stop=1" -DEFAULT_GRUB_CMDLINE_LINUX="console=tty0 console=ttyS${CONSOLE_DEV},${CONSOLE_SPEED}n8 quiet" +if [ "$install_env" = "build" ]; then + DEFAULT_GRUB_SERIAL_COMMAND="serial --port=%%CONSOLE_PORT%% --speed=${CONSOLE_SPEED} --word=8 --parity=no --stop=1" + DEFAULT_GRUB_CMDLINE_LINUX="console=tty0 console=ttyS%%CONSOLE_DEV%%,${CONSOLE_SPEED}n8 quiet" +else + DEFAULT_GRUB_SERIAL_COMMAND="serial --port=${CONSOLE_PORT} --speed=${CONSOLE_SPEED} --word=8 --parity=no --stop=1" + DEFAULT_GRUB_CMDLINE_LINUX="console=tty0 console=ttyS${CONSOLE_DEV},${CONSOLE_SPEED}n8 quiet" +fi GRUB_SERIAL_COMMAND=${GRUB_SERIAL_COMMAND:-"$DEFAULT_GRUB_SERIAL_COMMAND"} GRUB_CMDLINE_LINUX=${GRUB_CMDLINE_LINUX:-"$DEFAULT_GRUB_CMDLINE_LINUX"} export GRUB_SERIAL_COMMAND @@ -535,6 +540,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 <> $grub_cfg menuentry '$demo_grub_entry' { search --no-floppy --label --set=root $demo_volume_label @@ -543,7 +554,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 echo 'Loading $demo_volume_label $demo_type initial ramdisk ...' @@ -564,6 +575,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 From c8bff7afdebd32e2467b868a83efe5b86c495aca Mon Sep 17 00:00:00 2001 From: padman Date: Fri, 15 Sep 2017 10:47:32 -0400 Subject: [PATCH 2/2] Install grub from bundled debian package instead of using ONIE's. Address review comments --- build_debian.sh | 6 +- files/image_config/platform/rc.local | 110 +++++++++++++++++++-------- installer/x86_64/install.sh | 9 +-- 3 files changed, 87 insertions(+), 38 deletions(-) diff --git a/build_debian.sh b/build_debian.sh index 49e231afbbfc..9bdda3451009 100755 --- a/build_debian.sh +++ b/build_debian.sh @@ -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 @@ -206,9 +207,12 @@ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y in kexec-tools \ less \ unzip \ - xz-utils \ 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 diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local index 8ad4cf31a599..d3ecba7d8518 100755 --- a/files/image_config/platform/rc.local +++ b/files/image_config/platform/rc.local @@ -37,6 +37,26 @@ fi 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 @@ -47,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 @@ -77,39 +96,73 @@ 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 based on ONIE grub. + # 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 - mkdir -p /host/image-$sonic_version/tmp - cd /host/image-$sonic_version/tmp - # Extract ONIE's ramdisk which contain's the ONIE grub - mount $onie_dev /mnt/onie-boot - cp /mnt/onie-boot/onie/initrd.img-3.2.69-onie initrd.img-3.2.69-onie.xz - xz -d initrd.img-3.2.69-onie.xz - mv initrd.img-3.2.69-onie initrd.img-3.2.69-onie.cpio - cpio -idv < initrd.img-3.2.69-onie.cpio - cpio_retval=$? + 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 - # Get the device and grub directory to be used for grub-install + 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]:.*$//') - grub_platform_dir=`find . -name kernel.img -print` - grub_platform_dir=$(dirname "${grub_platform_dir}") + if [ -z "$sonic_dev" ]; then + echo "Unable to determine sonic partition !" >> /etc/migration.log + firsttime_exit + fi - # Install the grub for SONiC - if [ "$cpio_retval" -eq 0 ] && [ -n "$sonic_dev" ] && [ -n "$grub_platform_dir" ]; then - grub-install --boot-directory=/host --recheck $sonic_dev --directory=/host/image-$sonic_version/tmp/$grub_platform_dir 2>/dev/null - else - echo "Unable install grub" >> /etc/migration.log + 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 and assigned. + # retrieved from installer.conf (if present) and assigned. . /usr/share/sonic/device/$platform/installer.conf - sed -i -e "s/%%CONSOLE_PORT%%/$CONSOLE_PORT/g" /host/grub.cfg - sed -i -e "s/%%CONSOLE_DEV%%/$CONSOLE_DEV/g" /host/grub.cfg + 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/:.*$//') @@ -117,15 +170,12 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then 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 - # Set the SONiC's grub config + # Initialize the SONiC's grub config mv /host/grub.cfg /host/grub/grub.cfg - - # Cleanup - cd /host/image-$sonic_version - rm -fr /host/image-$sonic_version/tmp - umount /mnt/onie-boot fi rm /host/image-$sonic_version/platform/firsttime diff --git a/installer/x86_64/install.sh b/installer/x86_64/install.sh index ddcc7d68d883..358da41071b5 100755 --- a/installer/x86_64/install.sh +++ b/installer/x86_64/install.sh @@ -484,13 +484,8 @@ trap_push "rm $grub_cfg || true" [ -r ./platform.conf ] && . ./platform.conf -if [ "$install_env" = "build" ]; then - DEFAULT_GRUB_SERIAL_COMMAND="serial --port=%%CONSOLE_PORT%% --speed=${CONSOLE_SPEED} --word=8 --parity=no --stop=1" - DEFAULT_GRUB_CMDLINE_LINUX="console=tty0 console=ttyS%%CONSOLE_DEV%%,${CONSOLE_SPEED}n8 quiet" -else - DEFAULT_GRUB_SERIAL_COMMAND="serial --port=${CONSOLE_PORT} --speed=${CONSOLE_SPEED} --word=8 --parity=no --stop=1" - DEFAULT_GRUB_CMDLINE_LINUX="console=tty0 console=ttyS${CONSOLE_DEV},${CONSOLE_SPEED}n8 quiet" -fi +DEFAULT_GRUB_SERIAL_COMMAND="serial --port=${CONSOLE_PORT} --speed=${CONSOLE_SPEED} --word=8 --parity=no --stop=1" +DEFAULT_GRUB_CMDLINE_LINUX="console=tty0 console=ttyS${CONSOLE_DEV},${CONSOLE_SPEED}n8 quiet" GRUB_SERIAL_COMMAND=${GRUB_SERIAL_COMMAND:-"$DEFAULT_GRUB_SERIAL_COMMAND"} GRUB_CMDLINE_LINUX=${GRUB_CMDLINE_LINUX:-"$DEFAULT_GRUB_CMDLINE_LINUX"} export GRUB_SERIAL_COMMAND