Skip to content

Commit

Permalink
[installer]: Prevent filesystem corruption (#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 #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
  • Loading branch information
nazariig committed Apr 12, 2021
1 parent 9d81524 commit 6f0dbf2
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 6f0dbf2

Please sign in to comment.