无法在 /mnt/ 中执行“mkdir”并且无法执行“chmod”

无法在 /mnt/ 中执行“mkdir”并且无法执行“chmod”

我正在跟进本指南在我的 Ubuntu Azure 托管 VM 上,需要在/mnt/.对于这个问题,我只需将要创建的目录更改为“test”。运行时我不断收到以下错误消息sudo mkdir /mnt/test/

mkdir: cannot create directory ‘/mnt/test/’: Operation not permitted

当我尝试时,我收到了略有不同的错误消息cd /mnt/

-bash: cd: /mnt/: Permission denied

我的下一步是查看该目录的权限,因此我运行sudo stat /mnt/并得到:

File: /mnt/
Size: 4096          Blocks: 8          IO Block: 4096   directory
Device: 801h/2049d  Inode: 2           Links: 6
Access: (0444/dr--r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-02-17 14:19:48.658231668 +0000
Modify: 2021-02-08 12:34:19.906967600 +0000
Change: 2021-02-09 09:40:12.287101300 +0000
Birth: -

我发现我没有写入权限,因此运行sudo chmod 664 /mnt/但收到以下错误消息:

chmod: changing permissions of '/mnt/': Operation not permitted

我对 Unix 还很陌生,所以不确定如何继续。如果有任何进一步的故障排除步骤或解决方案,我们将不胜感激。

对评论的回应:

df /mnt输出:

Filesystem     1K-blocks  Used Available Use% Mounted on
/dev/sda1       32894736 49192  31151544   1% /mnt

grep | '/mnt'带有一些编辑信息的输出:

/dev/sda1 on /mnt type ext4 (rw,relatime,x-systemd.requires=cloud-init.service)
/dev/sda1 on /mnt/sdc1 type ext4 (rw,relatime)
/dev/sdc on /mnt/sdc type ext4 (rw,relatime)
//<azure storage account name>.file.core.windows.net/sih-workspace on /mnt/sih-workspace type cifs (rw,relatime,vers=3.0,cache=strict,username=**********,uid=0,noforceuid,gid=0,noforcegid,addr=<azure data centre IP>,file_mode=0777,dir_mode=0777,soft,persistenthandles,nounix,serverino,mapposix,rsize=1048576,wsize=1048576,bsize=1048576,echo_interval=60,actimeo=1)

答案1

只需使用通过.ephemeral-disk-warning创建文件的愚蠢的 systemd 单元即可。/mnt/DATALOSS_WARNING_README.txtsystemctl disable ephemeral-disk-warning.service

原因:

由于@chris-davies,我遇到了同样的情况评论:

您似乎在 /mnt 的子目录上有多个挂载,这些挂载现在已被 /mnt 本身上的另一个挂载隐藏。您需要决定哪些是您想要的,哪些是您不期望的。我的建议是,第一个操作应该是卸载 /mnt,以便其他操作再次暴露。但我不熟悉您正在使用的 Azure 文件系统实用程序,因此这不是完整的答案

$ cat /etc/rc.local
mkdir /mnt/tmp
mount --bind /mnt/tmp /tmp
chmod 1777 /tmp
# shortly after booted
$ l /mnt
total 37K
drwxr-xr-x  5 root  root  4.0K Jan 14 08:46 .
drwxr-xr-x 19 root  root    26 Jan 14 01:22 ..
drwx------  2 root  root   16K Jan 14 08:46 lost+found
drwxrwxrwt  5 root  root  4.0K Jan 14 08:46 tmp
# just after systemd unit `ephemeral-disk-warning.service` started and failed
$ l /mnt
ls: cannot access '/mnt/.': Permission denied
ls: cannot access '/mnt/..': Permission denied
ls: cannot access '/mnt/lost+found': Permission denied
ls: cannot access '/mnt/tmp': Permission denied
total 0
d????????? ? ? ? ?            ? .
d????????? ? ? ? ?            ? ..
d????????? ? ? ? ?            ? lost+found
d????????? ? ? ? ?            ? tmp

调查:

$ journalctl --no-pager --boot -g ephemeral
Jan 14 08:46:48 azure systemd[1]: Starting Write warning to Azure ephemeral disk...
Jan 14 08:46:48 azure ephemeral-disk-warning[3758]: /usr/sbin/ephemeral-disk-warning: 7: cannot create /mnt
Jan 14 08:46:48 azure audit[1]: SERVICE_START pid=1 uid=0 auid=4294967295 ses=4294967295 subj=unconfined msg='unit=ephemeral-disk-warning comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
Jan 14 08:46:48 azure root[3763]: Added ephemeral disk warning to /mnt
                                  /tmp/DATALOSS_WARNING_README.txt
Jan 14 08:46:48 azure systemd[1]: Finished Write warning to Azure ephemeral disk.
$ journalctl --no-pager --boot -u ephemeral-disk-warning
Jan 14 08:46:48 azure systemd[1]: Starting Write warning to Azure ephemeral disk...
Jan 14 08:46:48 azure ephemeral-disk-warning[3758]: /usr/sbin/ephemeral-disk-warning: 7: cannot create /mnt
Jan 14 08:46:48 azure ephemeral-disk-warning[3758]: /tmp/DATALOSS_WARNING_README.txt: Directory nonexistent
Jan 14 08:46:48 azure systemd[1]: Finished Write warning to Azure ephemeral disk.
Jan 14 08:46:48 azure ephemeral-disk-warning[3761]: chmod: cannot access '/tmp/DATALOSS_WARNING_README.txt': No such file or directory
Jan 14 08:46:48 azure ephemeral-disk-warning[3762]: chattr: No such file or directory while trying to stat /tmp/DATALOSS_WARNING_README.txt
$ systemctl cat ephemeral-disk-warning.service
[Unit]
Description=Write warning to Azure ephemeral disk
After=cloud-config.service
ConditionVirtualization=microsoft
ConditionPathIsMountPoint=/mnt
ConditionPathExists=/dev/disk/azure/resource-part1

[Service]
Type=oneshot
ExecStart=/usr/sbin/ephemeral-disk-warning
RemainAfterExit=yes
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target
$ cat /usr/sbin/ephemeral-disk-warning
#!/bin/sh
dev_resource=$(readlink -f /dev/disk/azure/resource-part1)
dev_resource_mp=$(awk '$1==R {print$2}' "R=${dev_resource}" /proc/mounts)
warn_file="${dev_resource_mp}/DATALOSS_WARNING_README.txt"

if [ ! -f "${warn_file}" ]; then
    cat > ${warn_file} <<EOM
WARNING: THIS IS A TEMPORARY DISK.

Any data stored on this drive is SUBJECT TO LOSS and THERE IS NO WAY TO
RECOVER IT.

Please do not use this disk for storing any personal or application data.

For additional details to please refer to the MSDN documentation at:
http://msdn.microsoft.com/en-us/library/windowsazure/jj672979.aspx

To remove this warning run:
    sudo chattr -i $warn_file
    sudo rm $warn_file

This warning is written each boot; to disable it:
    echo "manual" | sudo tee /etc/init/ephemeral-disk-warning.override
    sudo systemctl disable ephemeral-disk-warning.service

EOM

    chmod 0444 ${warn_file}
    chattr +i ${warn_file}
    logger "Added ephemeral disk warning to ${warn_file}"
fi
$ source <(head -n 4 /usr/sbin/ephemeral-disk-warning)
$ echo $warn_file
/mnt
/tmp/DATALOSS_WARNING_README.txt
$ echo chattr +i ${warn_file}
chattr +i /mnt
/tmp/DATALOSS_WARNING_README.txt

所以这只是另一个换行陷阱这导致将整体标记/mnt不可变的,最后我将旧式自动/etc/rc.local运行脚本转换为一个系统单元:

# https://unix.stackexchange.com/questions/471824/what-is-the-correct-substitute-for-rc-local-in-systemd-instead-of-re-creating-rc
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/path/to/the/script.sh
StandardOutput=journal+console
StandardOutput=journal+console

[Unit]
# https://unix.stackexchange.com/questions/635165/unable-to-mkdir-in-mnt-and-cannot-chmod/766859#766859
After=ephemeral-disk-warning.service

After=mnt.mount
Requires=mnt.mount
RequiredBy=anything-requiring-mnt.service
Before=anything-requiring-mnt.service

[Install]
WantedBy=multi-user.target

答案2

如果您在 /mnt 上安装了文件系统,则无法在其下创建任何内容。您可以通过运行“$ df -h /mnt”来测试这一点,我可以假设您将看到如下所示的自动地图:

已使用的文件系统大小 可用 使用% 安装于

/etc/auto.toybox 0 0 0 - /mnt

如果是这样,那么自动挂载器已声明 /mnt 作为自动映射器的挂载点,并且您尝试在 /mnt 下创建的任何内容都将不起作用。这就是许可被拒绝的原因。

如果您看到像上面一样的自动挂载,则您可能对自动挂载程序进行了错误的直接映射。查看 autofs 和 ypbind man 以及 systemctl 输出。使用 ypcat -k 命令来了解 auto.master 映射正在做什么。

使用 ypcat -k auto.master 或 ypcat -k auto.home 等...以便您了解映射。
请记住,auto.master 使用诸如 auto.home 之类的映射,如果使用 redhat 变体和 ypbind,这些映射可能位于 /etc/ypsrc/ 目录中。使用“automount -m”或“sudo automount -m”查看映射。您对 NIS 服务器上的映射所做的任何更改都需要从 /var/yp/ 运行“make”

使用 /etc/ypsrc/auto.home 将允许自动映射器将目录安装到 client:/home 目录,这样如果您“确实”需要在 /mnt 中 mkdir 那么应该将安装点移动到 /home 并现在允许您在 /mnt 中 mkdir 而不会出现可怕的“权限被拒绝”错误...但您确实不应该使用 /mnt 来创建随机目录 - 恕我直言。总而言之,您无法在已使用的挂载点上 mkdir,这可能是自动挂载/autofs 问题,或者将错误的所有者/组分配给 /mnt <,这很容易注意到并修复。

相关内容