Skip to content

Commit

Permalink
[installer]: Prevent filesystem corruption (sonic-net#7264)
Browse files Browse the repository at this point in the history
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 <image_version>
loop=      matches <loop> kernel parameter
\/*image-  matches <image_version> prefix
\(\S\+\)   matches <image_version> group and assigns it to \1
\/.*$      matches anything after the <image_version>
\1         replace everything with <image_version>
p          print it
```
closes sonic-net#6267

#### Why I did it
* To fix sonic-net#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
  • Loading branch information
nazariig authored and raphaelt-nvidia committed May 13, 2021
1 parent 7c7c015 commit 5ea0fe9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion installer/arm64/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion installer/armhf/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion installer/x86_64/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5ea0fe9

Please sign in to comment.