9

I am using a USB with an embedded device running linux. The issue is that in some cases when I unmount the usb by giving the command umount /media/sda1 the unmount is successful but when I proceed to remove the usb I get an error saying unable to mark fs as dirty and when I plug it back in I get the error Volume was not properly unmounted. Some data may be corrupt. Please run fsck. Is there any other command on the linux terminal which I can use to safely eject the usb? so that the usb device is powered off before I unplug it.

Update: I tried using sync but the error is still there as shown below:

root@(none):~# sync
root@(none):~# umount /media/sda1
root@(none):~# ls /media/sda1
root@(none):~# [  296.021241] usb 2-1: USB disconnect, device number 3
[  296.026879] sd 1:0:0:0: [sda] Synchronizing SCSI cache
[  296.031175] sd 1:0:0:0: [sda]
[  296.033743] Result: hostbyte=0x01 driverbyte=0x00
[  296.048283] FAT-fs (sda1): unable to read boot sector to mark fs as dirty
malik12
  • 191
  • 2
    You could try sync. – garethTheRed Jun 17 '16 at 06:47
  • Are there any side effects as It says here http://unix.stackexchange.com/questions/90657/how-to-remove-a-usb-drive-without-worrying-if-its-been-unmounted that 'sync is bad for lifetime of the device' and i am unsure about the side effects of flush – malik12 Jun 17 '16 at 07:01
  • Bad or not - what's the effect of not syncing? Corrupt files. The article you linked to suggests that syncing each time you write to the device (for example after a few minutes of editing a document) is bad as you write too many times which in theory reduces the lifetime of the device. Syncing just before you umount is common sense as you want your data to be written to the device don't you? – garethTheRed Jun 17 '16 at 07:08
  • as others mentioned, use sync before umount – magor Jun 17 '16 at 07:23
  • @garethTheRed Yes you are right i will try it with sync. Thanks – malik12 Jun 17 '16 at 07:33
  • @garethTheRed I tried sync command but it had no effect i was getting the same error as edited in my post – malik12 Jun 17 '16 at 10:42
  • 4
    FWIW, umount does a sync on the partition automatically. – Stephen Harris Jun 17 '16 at 11:26
  • Did you try @jimmij 's solution? It looks promising :-) – garethTheRed Jun 17 '16 at 11:40
  • I came to this question because I wanted to know about "how to remove USB device of any type" and not only USB mass storage/block device. – humanityANDpeace Jul 21 '18 at 12:17

1 Answers1

8

eject /dev/sda will try and safely remove the device from the kernel and make it safe to remove. You can verify it's removed by looking on /dev to see if the partition entry has been removed (the base device may still show up).

e.g. I just plugged in a USB stick and it showed as /dev/sdg and the partition as sdg1. I can unmount it and it still shows, but after the eject it disappears

$ ls /dev/sdg*
/dev/sdg  /dev/sdg1

$ df | grep sdg
/dev/sdg1       59632764  47460364   9136496  84% /media/sweh/music

$ umount /dev/sdg1

$ ls /dev/sdg*
/dev/sdg  /dev/sdg1

$ sudo eject /dev/sdg

$ ls /dev/sdg*       
/dev/sdg
  • my linux version apparently doesn't have that command

    root@(none):~# eject /dev/sda1 -sh: eject: command not found

    – malik12 Jun 17 '16 at 11:38