From 6f0dbf2c44bac406f676cdd9c7f36cc24751b116 Mon Sep 17 00:00:00 2001 From: Nazarii Hnydyn Date: Mon, 12 Apr 2021 20:48:44 +0300 Subject: [PATCH] [installer]: Prevent filesystem corruption (#7264) This improvement reads current SONiC version directly from `/proc/cmdline`. it supports `grub/aboot/uboot` bootloaders. **Code snippet**: ```bash cat /proc/cmdline | sed -n 's/^.*loop=\/*image-\(\S\+\)\/.*$/\1/p' ``` **Description**: ``` -n don't print lines s substitute ^.* matches anything before the loop= matches kernel parameter \/*image- matches prefix \(\S\+\) matches group and assigns it to \1 \/.*$ matches anything after the \1 replace everything with p print it ``` closes #6267 #### Why I did it * To fix #6267 #### How I did it * Fixed installer scripts #### How to verify it 1. Write invalid SONiC version to sonic_version.yml 2. Run SONiC-To-SONiC update --- installer/arm64/install.sh | 8 +++++++- installer/armhf/install.sh | 8 +++++++- installer/x86_64/install.sh | 8 +++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/installer/arm64/install.sh b/installer/arm64/install.sh index 280d9f200af9..c1e20b62bf42 100755 --- a/installer/arm64/install.sh +++ b/installer/arm64/install.sh @@ -98,7 +98,13 @@ if [ "$install_env" = "onie" ]; then mount_partition elif [ "$install_env" = "sonic" ]; then demo_mnt="/host" - eval running_sonic_revision=$(cat /etc/sonic/sonic_version.yml | grep build_version | cut -f2 -d" ") + # Get current SONiC image (grub/aboot/uboot) + eval running_sonic_revision="$(cat /proc/cmdline | sed -n 's/^.*loop=\/*image-\(\S\+\)\/.*$/\1/p')" + # Verify SONiC image exists + if [ ! -d "$demo_mnt/image-$running_sonic_revision" ]; then + echo "ERROR: SONiC installation is corrupted: path $demo_mnt/image-$running_sonic_revision doesn't exist" + exit 1 + fi # Prevent installing existing SONiC if it is running if [ "$image_dir" = "image-$running_sonic_revision" ]; then echo "Not installing SONiC version $running_sonic_revision, as current running SONiC has the same version" diff --git a/installer/armhf/install.sh b/installer/armhf/install.sh index 53f2fdf99661..c85170ff256e 100755 --- a/installer/armhf/install.sh +++ b/installer/armhf/install.sh @@ -98,7 +98,13 @@ if [ "$install_env" = "onie" ]; then mount_partition elif [ "$install_env" = "sonic" ]; then demo_mnt="/host" - eval running_sonic_revision=$(cat /etc/sonic/sonic_version.yml | grep build_version | cut -f2 -d" ") + # Get current SONiC image (grub/aboot/uboot) + eval running_sonic_revision="$(cat /proc/cmdline | sed -n 's/^.*loop=\/*image-\(\S\+\)\/.*$/\1/p')" + # Verify SONiC image exists + if [ ! -d "$demo_mnt/image-$running_sonic_revision" ]; then + echo "ERROR: SONiC installation is corrupted: path $demo_mnt/image-$running_sonic_revision doesn't exist" + exit 1 + fi # Prevent installing existing SONiC if it is running if [ "$image_dir" = "image-$running_sonic_revision" ]; then echo "Not installing SONiC version $running_sonic_revision, as current running SONiC has the same version" diff --git a/installer/x86_64/install.sh b/installer/x86_64/install.sh index 7c0d311b1a8a..d40dedc9a160 100755 --- a/installer/x86_64/install.sh +++ b/installer/x86_64/install.sh @@ -467,7 +467,13 @@ if [ "$install_env" = "onie" ]; then elif [ "$install_env" = "sonic" ]; then demo_mnt="/host" - eval running_sonic_revision=$(cat /etc/sonic/sonic_version.yml | grep build_version | cut -f2 -d" ") + # Get current SONiC image (grub/aboot/uboot) + eval running_sonic_revision="$(cat /proc/cmdline | sed -n 's/^.*loop=\/*image-\(\S\+\)\/.*$/\1/p')" + # Verify SONiC image exists + if [ ! -d "$demo_mnt/image-$running_sonic_revision" ]; then + echo "ERROR: SONiC installation is corrupted: path $demo_mnt/image-$running_sonic_revision doesn't exist" + exit 1 + fi # Prevent installing existing SONiC if it is running if [ "$image_dir" = "image-$running_sonic_revision" ]; then echo "Not installing SONiC version $running_sonic_revision, as current running SONiC has the same version"