Accessing root-dir of mounted disk

peterinw

New Member
Oct 31, 2023
2
0
1
Hello all.



I want to access my usb-disk from withing the lxc-container. I'm mounting the disk with following line:

mp0: /dev/sdb3,mp=/media/usb-disk,backup=0



The root filesystem is mounted here:

rootfs: local-lvm:vm-107-disk-0,mountoptions=lazytime,size=130G



I know that I can map uid/gids using lxc.idmap in the config file. But is there a way to modify the directory /media/usb-disk?

Trying it in the container gives me an error:

~ # mkdir /media/usb-disk/test mkdir: can't create directory '/media/usb-disk/test': Permission denied

If I interpret it correctly, the root-dir of sdb3 is owned/modifiable by root(0), which is the reason why the unprivileged root(100000) in the container gets "permission denied". However, the same seems to be working with the root-dir of local-lvm:wm-107-disk-0, which is the root dir(/) of the container.
Can I use a mount option or a container option to access /media/usb-disk/?

Here are some more infos from within the container:

~ # mount /dev/mapper/pve-vm--107--disk--0 on / type ext4 (rw,lazytime,relatime,stripe=16) /dev/sdb3 on /media/usb-disk type ext4 (rw,relatime,stripe=8191)

~ # stat / File: / Size: 4096 Blocks: 8 IO Block: 4096 directory Device: fd0dh/64781d Inode: 2 Links: 20 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)

~ # stat /media/usb-disk/ File: /media/usb-disk/ Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 813h/2067d Inode: 2 Links: 18 Access: (0755/drwxr-xr-x) Uid: (65534/ nobody) Gid: (65534/ nobody)
 
I found the solution:
Outside the container, do:

mount /dev/sdb3 /mnt chown 100000:100000 /mnt umount /mnt

Now start the container.

/ # stat / File: / Size: 1024 Blocks: 2 IO Block: 1024 directory Device: fc14h/64532d Inode: 2 Links: 20 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) / # stat /media/usb/ File: /media/usb/ Size: 1024 Blocks: 2 IO Block: 1024 directory Device: fc16h/64534d Inode: 2 Links: 4 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)

This is the same which is done with the root-partitions of the containers.
They are also owned by 100000 (root in the container)

Now, within the container, I can create files and dirs and change its owner.

Basically, I had to understand that it is possible to change to owner of a mounted mountpoint, and that this change is permanent for the mounted filesystem, even after un- and remounting.
 
Last edited:
Yes and also keep in mind that you shouldn't use "/dev/sdb3". This might mount the wrong filesystem as soon as you remove or add some disks/usb-sticks. Use "/dev/disk/by-id/yourDiskIdHere-part3" you can find out by running ls -la /dev/disk/by-id | grep sdb3.