Safely remove drive linux

What is the Command Line Equivalent of «Safely Remove Drive»?

What is the command line equivalent of the Nautilus feature called «Safely Remove Drive». Specifically, I am removing a USB flash drive.

5 Answers 5

The udisks command is most likely what you are looking for.

While sudo unmount /dev/sdXY will work, udisks can do this without root level (sudo) permissions.

If you have a drive /dev/sdXY , mounted, where X is a letter representing your usb disk and Y is the partition number (usually 1), you can use the following commands to safely remove the drive:

udisks --unmount /dev/sdXY udisks --detach /dev/sdX 

For a practical example, if I have the partition /dev/sdb1 mounted, I would run this to unmount and detach it:

udisks --unmount /dev/sdb1 udisks --detach /dev/sdb 

If your drive is not mounted, or was never mounted, simply use the second command:

I originally found this through this question: https://superuser.com/a/430470/176493.

Using udisks2:

In the newer ubuntu distributions (I’m unsure of when the switch occurred), udisks2 is installed instead of udisks.

Mirroring the commands above, to unmount and detach a disk with udisks2:

udisksctl unmount -b /dev/sdXY udisksctl power-off -b /dev/sdX 

Example if my drive is /dev/sdb1 :

udisksctl unmount -b /dev/sdb1 udisksctl power-off -b /dev/sdb 

Similarly to above, power-off can be used to detach the drive even if there are no partitions mounted, or no partition was ever mounted:

udisksctl power-off -b /dev/sdb 

I get Detach failed: Not Authorized when I try to run it without root privileges. (Not a serious issue, but a small correction.)

Yes, I mounted with sudo , but I also umount ed before trying to —detach . It’s a trusty release of ubuntu server.

awesome! Thank you for this! I have been trying to move all of my activities to the terminal as much as possible, and this is one that I could not figure out how to do, since I write iso’s to USB sticks all of the time, and need a way to eject them via the cli. Thanks again!

@AddisonKlinke In that case, using only the second command (—detach or power-off depending on your udisks version) should work.

The actual equivalent to Nautilus Mount/Unmount operation is gvfs-mount -m -d /dev/ice /some/directory and gvfs-mount -u /some/directory . This uses the same API that Nautilus uses, GIO virtual file system (gvfs), which provides different tools to use several services as mount points, such smb, NFS, FTP, block devices, etc.

To identify which device you need to unmount just use gvfs-mount -l which should be enough.

This solution has the peculiarity that it doesn’t require for elevated permissions, since everything is managed by the umount/gvfsd/polkit services, which further resemblances the similarity with Nautilus behavior.

This seems to be the most correct answer. Although many of us are used to thinking of udisks (suggested in other answers) as «the way» non-root users mount and unmount removable disks on the desktop, as stated here gvfs-mount may use other mechanisms. (On my Ubuntu MATE Utopic system, the udisks package isn’t even installed.)

Читайте также:  Alt linux автоматическое обновление

Once you know the device, possibly using the df info as in @rcpao answer, the best way to «eject» the disk is, imho, using the same command that the graphical interface is using:

udisksctl unmount --block-device /dev/sdc1 

I have a script to do a backup to a disk that I know will mount under /media/romano/movlin , and after the backup I do:

sync udisksctl unmount -b $(mount | grep movlin | cut -d" " -f1) 

Here, mount | grep movlin | cut -d» » -f1 will extract the device that is mounted under the label «movlin», (would be /dev/sdc1 in that case), and then it unmounts it.

Probably not. But I am quite unsure of the correct behavior of the cache in general; I sometime feel that the USB drive is still flashing after the umount. during few seconds. Let it say it’s a remnant of the old sync; sync; sync && poweroff .

@Rmano I have seen the flashing on a supposedly dismounted flash drive (usually done through a gui under KDE) many times. I try to wait it out before unplugging it. This does look (to my untrained eye) like something is not quite right. It would be even worse with a flash drive with no activity indicator light.

Rmano, since udisks is no longer present by default in Ubuntu udisksctl may be a preferred solution. udisksctl power-off —block-device /dev/sdc turns off the light on an external USB Flash drive so it might be suitable as the next command to follow yours.

lrwxrwxrwx 1 root root 9 Sep 16 10:49 usb-PNY_USB_2.0_FD_AAC3170000000954-0:0 -> ../../sdk lrwxrwxrwx 1 root root 10 Sep 16 10:49 usb-PNY_USB_2.0_FD_AAC3170000000954-0:0-part1 -> ../../sdk1 lrwxrwxrwx 1 root root 10 Sep 16 10:49 usb-PNY_USB_2.0_FD_AAC3170000000954-0:0-part2 -> ../../sdk2 
myuser@myhost:~$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/ubuntu--vg-root 1916153032 658404668 1160390336 37% / none 4 0 4 0% /sys/fs/cgroup udev 16438692 4 16438688 1% /dev tmpfs 3289976 2156 3287820 1% /run none 5120 0 5120 0% /run/lock none 16449860 18768 16431092 1% /run/shm none 102400 48 102352 1% /run/user /dev/sda1 240972 98990 129541 44% /boot /dev/sdc1 60915712 20992 60894720 1% /media/myuser/SD024-64GB /dev/sdk2 1004 876 128 88% /media/myuser/UEFI_NTFS /dev/sdk1 7970172 5798804 2171368 73% /media/myuser/NTFS 

At this point, see daboross’s answer for unmount, power off, and detach commands using ‘udisksctl’ and ‘udisks’ with your discovered mount points and disk device name.

    ‘udisksctl unmount -b’ or ‘umount’ using either /dev/sdc1 or /media/myuser/SD024-64GB.

myuser@myhost:~$ udisksctl unmount -b /dev/disk/by-id/usb-PNY_USB_2.0_FD_AAC3170000000954-0:0-part1 Unmounted /dev/sdk1. myuser@myhost:~$ udisksctl unmount -b /dev/disk/by-id/usb-PNY_USB_2.0_FD_AAC3170000000954-0:0-part2 Unmounted /dev/sdk2. 
myuser@myhost:~$ sudo umount /dev/sdc1 [sudo] password for myuser: myuser@myhost:~$ 
myuser@myhost:~$ sudo umount /media/myuser/SD024-64GB [sudo] password for myuser: myuser@myhost:~$ 
myuser@myhost:~$ udisksctl power-off -b /dev/disk/by-id/usb-PNY_USB_2.0_FD_AAC3170000000954-0:0 
  1. The device is typically of the format «/dev/sd» where is the disk, and is the partition number within that disk. Your disk may have more than one partitions mounted, so you will need to unmount all of the mounted partitions before the disk itself.

Источник

How can I safely remove a SATA disk from a running system?

I sometimes need to plug a disk into a disk bay. At other times, I have the very weird setup of connecting a SSD using a SATA-eSATA cable on my laptop while pulling power from a desktop. How can I safely remove the SATA disk from the system? This Phoronix forum thread has some suggestions:

justsumdood wrote: An(noymous)droid wrote:
What then do you do on the software side before unplugging? Is it a simple «umount /dev/sd»[drive letter]? after unmounting the device, to «power off» (or sleep) the unit:

(where X represents the device you wish to power off. for example: /dev/sdb) this will power the drive down allowing for it’s removal w/o risk of voltage surge. Does this mean that the disk caches are properly flushed and powered off thereafter? Another suggestion from the same thread:

chithanh wrote:
All SATA and eSATA hardware is physically able to be hotplugged (ie. not damaged if you insert/pull the plug). How the chipset and driver handles this is another question. Some driver/chipset combinations do not properly handle hotplugging and need a warmplug command such as the following one:

echo 0 - 0 > /sys/class/scsi_host/hostX/scan 

I doubt whether is the correct way to do so, but I cannot find some proof against it either. So, what is the correct way to remove an attached disk from a system? Assume that I have already unmounted every partition on the disk and ran sync . Please point to some official documentation if possible, I could not find anything in the Linux documentation tree, nor the Linux ATA wiki.

Читайте также:  Open source hypervisor linux

Источник

How to Safely Remove External Drive in Ubuntu Linux

This post describes steps one can take to eject or remove external storage devices in Ubuntu Linux safely.

When you attach external storage devices to Ubuntu Linux, it is recommended that you safely remove them before unplugging them from your computer.

Like other operating systems, Ubuntu Linux allows you to attach external devices, including USB flash drives and optical discs like CDs or DVDs. System processes and applications may use the data when external drives are connected.

If you unplug a device, you run the risk of unplugging while an application is still using it. This could result in some of your files being lost or damaged.

Below is how to safely eject external drives from Ubuntu Linux.

How to safely eject the external drive in Ubuntu Linux

As described above, if you unplug a device, you run the risk of unplugging while an application is still using it. This could result in some of your files being lost or damaged.

From the Activities overview, search for and open Files.

Ubuntu Linux File Manager has its icon on the Dock by default. Open File Manager.

Locate the device in the sidebar. It should have a small eject icon next to the name. Click the eject icon to remove or eject the device safely.

Alternatively, you can right-click the device’s name in the sidebar and select Eject.

If any of the files on the device are open and in use by an application, you will not be able to remove the device safely. You will be prompted with a window telling you Volume is busy.

Читайте также:  Почему мы выбираем linux

To safely remove the device, click Cancel.

Close all the files on the device. Click the eject icon to remove or eject the device safely.

This post showed you how to remove or eject external drives in Ubuntu Linux safely. Please use the comment form below if you find any errors above or have something to add.

Richard W

I love computers; maybe way too much. What I learned I try to share at geekrewind.com.

Источник

How to safely remove a USB external drive?

enter image description here

How I can safely remove a USB external drive? Ubuntu 12.10 does not come with this option, only to «Unmount» the disk, but not «Eject». I have tried both USB sticks and with my external hard drive.

You only need to unmount usb flash drives, but for actually spinning down external hard disks using udisks, see askubuntu.com/questions/56270.

Thanks dude, but I don’t want to use the command-line for a simple action that was enabled in older versions Ubuntu and disappeared in the new. This is frustrating.

I am running gnome shell, and when I go to the notification area, I can «Eject» my USB stuff. I don’t know what this answer is worth to you, but I just wanted to say, it seems possible.

10 Answers 10

To safely remove i.e. disconnect an external USB drive you need to manually unmount all of its partitions (represented by ‘drive icons’):

enter image description here

enter image description here

In the example above: to remove my Hitachi external USB drive I have to unmount all of the partitions on the said drive. To make things easier when formatting my drive I named (labelled) all of its partitions so that the names (labels) begin with ‘HIT’.

For further reading please refer to the bug report (the lack of either «Safely remove» or «Eject» is a bug in Ubuntu 12.10):

Don’t forget to mark ‘This bug affects me’ in the upper portion of the page.

@k-k-patel Even if «eject» and «safely remove» are virtually the same for external hard drives, none of the two are available. Only «unmount» is offered.

@hari @isoma No «unmount» and «eject/safely remove» are not the same and even for an external hard drive «eject/safely remove» is not subsumed by «unmount». For instance, consider a drive with two partitions. If the «user» unmounts one of them, the other one will remain mounted, and, as a result, if the user unplugs the drive, they face a data loss on the second partition. On the other hand, if the user «ejects/safely removes» the first partition, both will get unmounted.

Gnome Disks Utility has an option to Power off a disk.

You can first unmount the partitions as suggested in other answers or using the Gnome Disks Utility as I explained below.

Gnome Disks Utility screen

  1. access gnome-disk-utility searching for ‘Disks’ on Gnome Applications Menu or typing gnome-disks on a terminal.
  2. Select the disk in the left list
  3. Unmount the partitions clicking on each of them and clicking on the Stop button for each of them.
  4. click on the Power Off button located on the window top bar.

Источник

Оцените статью
Adblock
detector