非特權 LXC 容器中的 GPIO

非特權 LXC 容器中的 GPIO

我目前嘗試將 GPIO 存取到非特權 LXC 應用程式容器。將來這個容器將執行一個應用程式。但現在(在開發過程中)我只是在容器中啟動正常的 bash:

sudo lxc-execute -n gpio-client -f /var/proj/gpio-client/lxc.conf -- bin/bash

當我使用特權容器執行此操作時(意味著我的 lxc.conf 不包含任何 UID 或 GID 映射),我可以使用 GPIO。
GPIO 可以透過 /sys/class/gpio 中的檔案進行存取(下列輸出來自使用上述指令啟動的特權容器內的 bash):

bash-4.3# cd /sys/class/gpio/
bash-4.3# ls -l
total 0
-rwxrwx---    1 gpio-cli 1001          4096 Feb 20 12:41 export
lrwxrwxrwx    1 gpio-cli 1001             0 Feb 20 12:41 gpiochip890 -> ../../devices/gpiochip2/gpio/gpiochip890
lrwxrwxrwx    1 gpio-cli 1001             0 Feb 20 12:41 gpiochip898 -> ../../devices/gpiochip1/gpio/gpiochip898
lrwxrwxrwx    1 gpio-cli 1001             0 Feb 20 12:41 gpiochip906 -> ../../devices/soc0/amba/e000a000.gpio/gpio/gpiochip906
-rwxrwx---    1 gpio-cli 1001          4096 Feb 20 12:41 unexport

例如,我可以匯出 GPIO:

bash-4.3# echo 898 > export
bash-4.3# ls -l
total 0
-rwxrwx---    1 gpio-cli 1001          4096 Feb 20 14:06 export
lrwxrwxrwx    1 gpio-cli 1001             0 Feb 20 14:06 gpio898 -> ../../devices/gpiochip1/gpio/gpio898
lrwxrwxrwx    1 gpio-cli 1001             0 Feb 20 12:41 gpiochip890 -> ../../devices/gpiochip2/gpio/gpiochip890
lrwxrwxrwx    1 gpio-cli 1001             0 Feb 20 12:41 gpiochip898 -> ../../devices/gpiochip1/gpio/gpiochip898
lrwxrwxrwx    1 gpio-cli 1001             0 Feb 20 12:41 gpiochip906 -> ../../devices/soc0/amba/e000a000.gpio/gpio/gpiochip906
-rwxrwx---    1 gpio-cli 1001          4096 Feb 20 12:41 unexport

當我使用“ps -a”列印主機上正在運行的進程時,我可以看到以 root 身份運行的 bash:

      587 root       0:00 lxc-execute -n gpio-client -f /var/proj/gpio-client/lxc.conf -- bin/bash
      598 root       0:00 /init.lxc.static -- bin/bash
      615 root       0:00 bin/bash

但是,當我將UID 和GID 映射插入lxc.conf(以及/etc/subuid 和/etc/subgid 中的subuids 和subgid)時,當我嘗試訪問/sys/ 時,我總是收到“權限被拒絕」的訊息類別/GPIO:

bash-4.3# cd /sys/class/
bash-4.3# ls -l
total 0
drwxrwx---    2 65534    gpio             0 Feb 20 14:11 gpio
bash-4.3# cd gpio/
bash: cd: gpio/: Permission denied

這是我的 lxc.conf:

# *************************************************************************
# * general configuration
# *************************************************************************

# container name
lxc.utsname = gpio-client

# *************************************************************************
# * configure networking
# *************************************************************************

lxc.network.type          =   veth
lxc.network.flags         =   up
lxc.network.link          =   local_br0
lxc.network.name          =   lxcnet0
lxc.network.hwaddr        =   4a:49:43:49:79:ac
lxc.network.ipv4          =   10.10.0.98
lxc.network.ipv4.gateway  =   10.10.0.1

# *************************************************************************
# * configure container filesystem
# *************************************************************************

# set rootfs
lxc.rootfs = /var/proj/gpio-client/rootfs

# mount needed directories from host
lxc.mount.entry=/lib                lib             none    ro,bind     0   0
lxc.mount.entry=/sys/class/gpio     sys/class/gpio  none    rw,bind     0   0
lxc.mount.entry=/sys/devices        sys/devices     none    rw,bind     0   0
lxc.mount.entry=/usr/lib            usr/lib         none    ro,bind     0   0
lxc.mount.entry=/usr/bin            usr/bin         none    ro,bind     0   0
lxc.mount.entry=/bin                bin             none    ro,bind     0   0

# *************************************************************************
# * configure privileges of container
# *************************************************************************

# map user and group ids
lxc.include = /usr/share/lxc/config/userns.conf
# comment the following out for a privileged container:
lxc.id_map = u 0 1000000 65536
lxc.id_map = g 0 1000000 65536

這是正在運行的 Linux 系統:

zedboard-zynq7:/var/proj/gpio-client$ cat /proc/version 
Linux version 4.6.0-rt1-xilinx-v2016.3 (sebastian@ubuntu1604vm) (gcc version 6.2.0 (GCC) ) #5 SMP PREEMPT RT Fri Apr 7 13:43:09 CEST 2017

該系統是使用 Yocto 為 AVNET-Zedboard(具有 Xilinx Zynq-SoC)建構的。

有誰知道或建議如何運行此程式或在哪裡可以找到解決方案/資訊?我試著讓它運行幾個小時,但不幸的是我沒有成功...

最好的問候
塞巴斯蒂安

相關內容