Recently I have been on a journey to use block devices like a HDD partition in unprivilaged containers safely. After many different methods and struggels i have come to a, in my opinion balanced resolution.
- Block devices Here my solution is to create a mount directory and changing it's ownership to 100000:100000. Those settings translated to lxc container permissions make it be owned by root:root, because every uid and gid in container in Proxmox by default is created by adding 100000 to it's original uid and gid more here. Mounting of course can be achieved by manually mounting for example /dev/sdc2 in /mnt/storage which has correctly set ownership or adding a new rule in /etc/fstab. After all that a mount point can be added to container's configuration file
--mp[n] [volume=]<volume> ,mp=<Path> [,acl=<1|0>] [,backup=<1|0>] [,mountoptions=<opt[;opt...]>] [,quota=<1|0>] [,replicate=<1|0>] [,ro=<1|0>] [,shared=<1|0>] [,size=<DiskSize>]
Use volume as container mount point. Use the special syntax STORAGE_ID:SIZE_IN_GiB to allocate a new volume.
- Character devices (serial device example)
In this situation all we have to do is to create a suitable udev rule which will set owner and group of our /dev/tty device to 100000 and 100000. A built in utility called udevadm is very helpfull in creating udev rules. I suggest
udevadm info /dev/(your device)orudevadm info -a /dev/(your device). Here is an example of such a rule
KERNEL=="ttyCH343USB0", SUBSYSTEM=="tty", MODE="0664", GROUP="100000", OWNER="100000"
When our rule is ready we can add a mount entry in container's configuration file. This time it's done by an lxc configuration. Here is an exaple of such a line:
lxc.mount.entry: /dev/ttyCH343USB0 dev/ttyCH343USB0 none bind,optional,create=file 0 0
And it all would be to rainbowy if it worked. The catch is it works untill a device is disconnected and reconnected, at that point it looses all it's permissions inside the container and is unusable. The only known to me workaround is to create a privilaged container or simply reboot the container
Probably changing udev rule’s mode to
0666would make the device still usable after its reconnected