Disconnect USB stick in C ++

I am developing a Qt application for Linux (Ubuntu) in which I use a USB drive to back up some content. The application should unmount the target disk after copying the files. I have a udev rule file for connecting USB in a specific place using ENV{mount_options}="relatime,users,umask=0,uid=user,gid=user" , where the user represents my username.

I tried using this with no 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 } 

Can someone help me here? Am I using umount correctly?

Thanks in advance.

+4
source share
2 answers

An appropriate privilege (Linux: CAP_SYS_ADMIN ) is required to unmount file systems.

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

The CAP_SYS_ADMIN function allows a process to perform various administrative tasks, namely: mount (), umount (). There are two articles on the merits:

+9
source

Add to the line /etc/sudoers :

 user ALL=NOPASSWD: /bin/umount 

where user is your username.

Instead of using umount ... use sudo -u user umount ...

0
source

Source: https://habr.com/ru/post/1481956/


All Articles