简体   繁体   English

用C ++卸载USB驱动器

[英]unmount USB drive in C++

I am developing a Qt application for Linux (Ubuntu) in which I use USB drive to backup some content. 我正在为Linux(Ubuntu)开发一个Qt应用程序,我在其中使用USB驱动器备份一些内容。 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. 我有一个udev规则文件,用于在特定位置安装USB,其中ENV{mount_options}="relatime,users,umask=0,uid=user,gid=user" ,其中user表示我的用户名。

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? 我使用umount对吗?

Thanks in advance. 提前致谢。

Appropriate privilege (Linux: the CAP_SYS_ADMIN capability) is required to unmount file systems. 卸载文件系统需要适当的权限(Linux: CAP_SYS_ADMIN功能)。

Per umount the code is fine. 每次umount代码umount However you need privilege to umount devices. 但是,您需要权限才能卸载设备。

The CAP_SYS_ADMIN capability allows a process to perform various administrative tasks, namely: calling mount(), umount(). CAP_SYS_ADMIN功能允许进程执行各种管理任务,即:调用mount(),umount()。 There are two worth articles about capabilities here: 这里有两篇关于功能的文章:

Add to /etc/sudoers line: 添加到/etc/sudoers行:

user ALL=NOPASSWD: /bin/umount

where user is your user's name. 其中user是您的用户名。

Instead of using umount ... use sudo -u user umount ... 而不是使用umount ...使用sudo -u user umount ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM