Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unmount USB drive in C++

Tags:

c++

linux

ubuntu

qt

I am developing a Qt application for Linux (Ubuntu) in which I use USB drive to backup some content. Application should unmount the target drive after copying stuff. I have a udev rule file to mount USB at a specific location with ENV{mount_options}="relatime,users,umask=0,uid=user,gid=user" where user represents my user name.

I tried using this without any luck.

const char* usb = "/mnt/mountpoint/usbdrive";
if (!umount(usb))
{
  qDebug() << "Device unmounted";
}
else
{
  qDebug() << "Can't unmount" << strerror(errno); //this prints Operation not permitted
}

Could someone please help me here? Am I using umount right?

Thanks in advance.

like image 735
ramtheconqueror Avatar asked May 21 '13 13:05

ramtheconqueror


People also ask

How do I unmount a USB drive?

On Windows, you can unmount a drive by clicking the USB icon in the bottom-right side of the screen and then clicking Eject.

What is USB unmount mounting?

usb {mount | unmount} Description. Enables or disables the inserted USB drive. Command context. Manager ( # )

How do I unmount USB from laptop?

Open File Explorer by pressing Windows key + E . On the left, click This PC. On the right, right-click your USB flash drive. Select Eject.


1 Answers

Appropriate privilege (Linux: the CAP_SYS_ADMIN capability) is required to unmount file systems.

Per umount the code is fine. However you need privilege to umount devices.

The CAP_SYS_ADMIN capability allows a process to perform various administrative tasks, namely: calling mount(), umount(). There are two worth articles about capabilities here:

  • CAP_SYS_ADMIN: the new root
  • Overview of Linux capabilities (List of capabilities)
like image 119
masoud Avatar answered Sep 24 '22 00:09

masoud