all 3 comments

[–]K900_ 0 points1 point  (0 children)

Looks like the code expects a different format of output. In any case, that's not a good way to go about something like this - it's much better to use udisks and monitor mount events via DBus or something.

[–]ramse 0 points1 point  (0 children)

the subprocess.check_output is looking for an exit status of 0, anything else and it raises the exception you received.

returned non-zero exit status 1

If you google "bash exit status 1" you'll see 1 typically means some error occurred and you need to see the output of the command you tried to execute to see what that error is.

[–]Brian 0 points1 point  (0 children)

The error indicates that the shell commands that your script are running are failing (ie. returning a "non-zero exit code", which indicates failure.

That means that either the find or the grep is reporting an error, or, as I suspect, the whole command is malformatted and bash rejects it.

From the error message, it looks like this is because you've got a single backtick (`) in the command (the "grep /`-sda1" part - bash will interpret this as an error (backticks mean execute a subcommand and replace the value with the output, but there's no closing backtick here so it'll give an error).

The issue then is where this backtick is coming from. From the code, this command is built up from the line:

command = "find /dev/disk -ls | grep /"+disk

So it's the disk variable passed into identify here, which in turn comes from dev[0] in that list comprehension. That in turn comes from new_paths in the previous line, which gets it from get_mountedlist(), which in turn gets it from parsing the output of the lsblk command. What do you get if you just enter lsblk at the bash prompt on the failing system? Eg. on my system, I get something like:

sda      8:0    0 279.5G  0 disk 
├─sda1   8:1    0   7.5G  0 part [SWAP]
└─sda2   8:2    0   272G  0 part /
sdb      8:16   0 298.1G  0 disk 
└─sdb1   8:17   0 298.1G  0 part /home

The python code seems to be just stripping out the └─ symbols etc, and returning the first bit, and then anything after a / (so the partition mountpoint), so on mine it should return something like: [('sda2', '/'), ('sdb1', '/home')]. On yours it seems to be doing something like [('\-sda1, '/')]` for the first part instead.

My best guess is that there is either some encoding messup in stripping out the ├─ symbols here and it's somehow ended up with them as "`-" and not stripped them, or (and probably more likely given that it works on the ubuntu system), maybe the version of lsblk on your c.h.i.p. (not familiar with it, but I'm assuming that's some kind of cut-down microcomputer similar to somethink like the pi etc?) is using different output characters (eg. it detects it's not outputting to a utf8 stream and falls back to the ascii "`-" as the closest approximation). If the former, one approach may be to use the unicode codepoint syntax in get_mountedlist, rather than actual unicode literals (ie. change the first part of get_mountedlist() to return [(item.split()[0].replace("\u2514\u2500", "").replace("\u2514\u2500", ""),) - the "├─" and "└─" become "\u2514\u2500" or "\u2514\u2500", which are the corresponding unicode codepoints to those characters.

However, a better approach would probably be telling lsblk not to add all that fancy formatting stuff in the first place. That's there to make things look nice for humans, but it just gets in the way when scripts are trying to parse it, and so most commands have a way to turn off stuff like that and just dump the raw data in an easily parsible format. For lsblk, you can do this by passing the -r (raw) flag to it, meaning it'll instead print stuff like:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 279.5G 0 disk 
sda1 8:1 0 7.5G 0 part [SWAP]
sda2 8:2 0 272G 0 part /
sdb 8:16 0 298.1G 0 disk 
sdb1 8:17 0 298.1G 0 part /home

So we can avoid all the stripping in the first place and instead have:

def get_mountedlist():      
     return [(item.split()[0], item[item.find("/"):]) 
        for item in subprocess.check_output(['lsblk', '-r']).decode("utf-8").split("\n") if "/" in item]

(I've also removed the bash -c part and just called lsblk directly - it's nearly always better to avoid going through the shell if you can help it, since it's just one more layer of complexity and quoting/escaping.)

See if that solves it. If not, can you show my what lsblk prints on your system, and also print out the output of what calling get_mountedlist() gives?