2

I have a shell script that can only be run from a live session (LiveCD or Live USB) because it shrinks the root filesystem of an actual installation, which can only be done when the filesystem is not mounted.

If the script is run from the live session, it should pursue its execution. If not, it should fail.

I cannot find a way to differentiate the live session and a "normal" user session, as the live session has a look-and-feel very close to the installed system sessions. The only thing I can tell so far is that live sessions always have ubuntu as the hostname, but I cannot rely on that since the user can choose this when installing Ubuntu.

Is there a way of identifying this in a shell script / command line?

(I hesitated between posting on Ask Ubuntu or Stack Overflow, I can delete the post and re-post on Stack Overflow if more appropriate)

2 Answers 2

3

There are many distinguishing features between a live session and normal session. At the first shot it comes in my mind that there is no real user in a live session. One can exploit this.

So, there would not be any gid greater than 999 in /etc/passwd corresponds to any real user except nobody user.

$ awk -F: '($3>=1000){print $1}' /etc/passwd
nobody

The following script should work,

#!/bin/bash

var=$(awk -F: '($3>=1000){print $1}' /etc/passwd | wc -l)
if [[ $var -gt 1 ]]; then
    echo "normal session"
else
    echo "live session"
fi
1
  • This solution works well, thanks. However, since my script deals with partitions and filesystems, I chose @terdon's solution to be consistent. If the script has to change someday because of this test, then there is a good chance that the rest of script itself must be updated itself anyway.
    – astorije
    Apr 2, 2014 at 17:43
1

No, the name is arbitrary, you can't assume anything based on that. However, the Live session does have a few quirks such as:

$df -h | grep -w /
/cow        2.0G   42M   1.9G  3% /

As you can see, the special device /cow is mounted on /. I'm not sure how portable this is, I doubt it will be the same for non Ubuntu Linuxes and it may also change in future releases but as long as it's not an actual device in /dev you can test for this very easily:

df | grep -w / | grep -q 'cow' && echo "Live session" || echo "Normal install"

Explanation:

  • df : print mounted file systems
  • grep -w / : print only the line that shows what is mounted on /. The -w option matches whole words only so that only / and not for example /home will be printed.
  • grep -q cow : The -q suppresses output, grep will exit with status >0 (error)if cow was not found and 0 (correct) if it was.
  • && echo "Live session" : PrintLive sessionif thegrep` was succesful
  • || echo "Normal install" : Else, print Normal install.

EDIT BY THE OP

In the end, here is the solution I implemented in my script, if it's somewhat useful:

#!/bin/bash

if [ ! $(df | grep -w / | grep -q '/cow') ]; then
  printf "This script must be run from a live session.\n"
  exit 1
fi
2
  • This solution works like a charm. For further references, although I accepted this answer, please note that @souravc's solution works too and I chose this one only because it was more suitable for my needs. The other one must be considered too for other purposes.
    – astorije
    Apr 2, 2014 at 17:45
  • I added a snippet with the solution I actually implemented, thanks!
    – astorije
    Apr 2, 2014 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .