wartorious
Posts: 10
Joined: Wed Nov 04, 2015 11:47 am

Eject/dismount a USB with code on a Pi

Wed Nov 22, 2017 4:03 pm

Hello.

I've been trying to work out how to make it so I can safely disconnect a USB flash drive from a Pi using code. (so I can use this as part of a Python program I'm writing.)

I made a post on StackOverflow here:

https://stackoverflow.com/questions/474 ... 4_47431418

Thanks! :mrgreen:

-David.
www.dwbeck.com

User avatar
DougieLawson
Posts: 42748
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Eject/dismount a USB with code on a Pi

Thu Nov 23, 2017 8:18 am

sudo apt install eject
sudo eject /dev/sda
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

cjan
Posts: 1107
Joined: Sun May 06, 2012 12:00 am

Re: Eject/dismount a USB with code on a Pi

Thu Nov 23, 2017 10:35 am

right corner on menu, there isn't has a eject button?

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Eject/dismount a USB with code on a Pi

Thu Nov 23, 2017 11:59 am

I interpreted the OP as being that he wanted to actually do it in Python - that is, not as either:

1) Shell commands
2) Clicks in a GUI

However, if you follow the link over the his stackexchange post, he is calling it as "solved", using the "eject" command (presumably called via the os.system hook in his Python code).

It is probably possible to actually do this in Python, but its going to be easier to just do a "system()" call to the shell command ("eject").
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

buteman
Posts: 151
Joined: Tue Jul 03, 2012 9:06 am
Location: Barrow upon Humber

Re: Eject/dismount a USB with code on a Pi

Thu Nov 23, 2017 10:21 pm

Depending on what your flash drive is seen as you can do something like:

Code: Select all

cmd = "sudo umount /dev/sdb1"
os.system(cmd)
If you are running it as the pi user it doesn't ask for your password
There are few rewards greater than accomplishing a difficult task. Just because something doesn't cost money doesn't mean there is no cost. Every success makes the next one easier. Of course that takes determination and patience.

User avatar
DougieLawson
Posts: 42748
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Eject/dismount a USB with code on a Pi

Fri Nov 24, 2017 5:52 am

buteman wrote:
Thu Nov 23, 2017 10:21 pm
Depending on what your flash drive is seen as you can do something like:

Code: Select all

cmd = "sudo umount /dev/sdb1"
os.system(cmd)
If you are running it as the pi user it doesn't ask for your password
The mount and umount commands have the suid bit, so anyone can run it without sudo (as long as they're umount'ing something they've mounted).

The other problem with your code is identifying that device /dev/sdb or filesystem /dev/sdb1 is the one you want to eject or umount.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

whatTodo
Posts: 5
Joined: Sun Aug 04, 2019 3:44 am

Re: Eject/dismount a USB with code on a Pi

Sat Sep 21, 2019 11:30 pm

I've seen two answers for this question, and they both seem to work. However, my pi still says "Drive was removed without ejecting" using both

Code: Select all

sudo umount /media/pi/FLASH 
and

Code: Select all

sudo eject /dev/sda
Is there a way to do this from the command line that won't make the pi complain, or is this just how it is?

cspan
Posts: 390
Joined: Sat Jun 10, 2017 1:03 pm
Location: USA

Re: Eject/dismount a USB with code on a Pi

Sat Jan 18, 2020 6:14 pm

I'd like to add that I get the behavior as well after using SD Card Copier in Raspbian Buster GUI, making a clone of my microSD card from which Raspbian runs.

Once the copy is complete, there is no Eject icon on the menu bar across the top. Nor is there any indication of a mounted drive using the file manager (I think it usually shows up under /media/pi). But if I then go pull out my USB card reader (which has the cloned card), I get
Drive was removed without ejecting
Please use menu to eject before removal
[I also get this behavior in RPD with Buster for x86, even though SD Card Copier was unable to create partitions on the intended target, a microSD card (source was a laptop HDD).

Is this error message above a real issue? It comes up even though the OS doesn't otherwise show something that can be ejected. Thanks -

mink1956
Posts: 4
Joined: Mon Feb 10, 2020 7:50 pm

Re: Eject/dismount a USB with code on a Pi

Tue Feb 11, 2020 7:28 am

[Sorry if this is a thread hijack, but the partial fix below might help someone.]
I'm getting the same behavior after a successful copy by SD CARD COPIER.
A banner on the Panel:
Drive was removed without ejecting
Please use menu to eject before removal
Checking with File Manager shows nothing mounted, so I don't think it is real; perhaps the unmounting by SD Card Copier did not get communicated? I think it might be intermittent too.
The banner is quite persistent, but running this clears it (and other panel bugs such as 100% pcmanfm seen on Panel as >24% cpu and 100% single core htop ):

Code: Select all

lxpanelctl restart
Is there is a better way to handle
Working via TightVnc client to RealVNC on RPi 4B (I believe both my Rev 1.1 and 1.2). I think it might happen when directly on the machines too sometime, but I don't usually work that way. The TightVNC sessions attach to the same screen as a live session rather than spawn a new one.
Slightly annoying, but ... :?
cheers

Return to “Raspberry Pi OS”