linux man 类型 p,为Podman自定义graphroot目录设置SELinux上下文类型

我想设置自定义目录来存储使用Podman创建的容器的数据,如何将目录的文件类型(及其内容)更改为Podman使用的上下文类型?在运行SELinux的系统上,所有进程和文件都以表示安全相关信息的方式标记。如果您尝试使用存储在/var/lib/containers以外的目录中的数据创建一个容器,则会获得权限被拒绝的提示。

c27cf68d2c4d283b0dcbd276fbca53a3.png

我将在CentOS 8服务器上进行演示,让我们将SELinux置于执行模式:

$ sudo setenforce 1

$ sestatus

SELinux status: enabled

SELinuxfs mount: /sys/fs/selinux

SELinux root directory: /etc/selinux

Loaded policy name: targeted

Current mode: enforcing

Mode from config file: enforcing

Policy MLS status: enabled

Policy deny_unknown status: allowed

Memory protection checking: actual (secure)

Max kernel policy version: 31

安装提供podman的Container工具:

sudo dnf module install container-tools

让我们通过运行helloworld容器来确认podman是否正在按预期运行:

$ podman run --rm hello-world

Trying to pull docker.io/library/hello-world...

Getting image source signatures

Copying blob 0e03bdcc26d7 done

Copying config bf756fb1ae done

Writing manifest to image destination

Storing signatures

Hello from Docker!

This message shows that your installation appears to be working correctly.

为了生成此消息,Docker采取了以下步骤:

1.Docker客户端联系了Docker守护进程。

2.Docker守护程序从Docker Hub中提取了“hello-world”映像。(amd64)

3.Docker守护程序从该映像创建了一个新容器,该容器运行可执行文件,生成您当前正在读取的输出。

4.Docker守护程序将该输出流式传输到Docker客户端,该客户端将其发送到您的终端。

要尝试更具野心的东西,可以使用以下命令运行Ubuntu容器:

$ docker run -it ubuntu bash

使用免费的Docker ID共享图像、自动化工作流程以及更多功能:

https://hub.docker.com/

有关更多示例和想法,请访问:

https://docs.docker.com/get-started/

确认容器的当前root目录设置:

$ podman info | grep -i root

rootless: false

GraphRoot: /var/lib/containers/storage

RunRoot: /var/run/containers/storage

我们创建一个用于存储数据的自定义目录:

sudo mkdir -p /data/containers

更新设置并将目录更改为上面创建的目录:

$ sudo vi /etc/containers/storage.conf

# Primary Read/Write location of container storage

#graphroot = "/var/lib/containers/storage"

graphroot = "/data/containers"

尝试运行一个容器:

# podman run --rm -it  ubuntu bash

Getting image source signatures

Copying blob 0f3630e5ff08 done

Copying blob d72e567cc804 done

Copying blob b6a83d81d1f4 done

Copying config 9140108b62 done

Writing manifest to image destination

Storing signatures

bash: error while loading shared libraries: libc.so.6: cannot change memory protections

从输出中我得到了错误消息:

bash: error while loading shared libraries: libc.so.6: cannot change memory protections

让我们为目录/data/containers设置正确的SELinux类型,然后重试:

sudo semanage fcontext -a -e /var/lib/containers /data/containers

sudo restorecon -R -vv /data/containers

如果找不到semanage命令,请使用以下命令进行安装:

sudo yum install policycoreutils-python-utils -y

确认SELinux上下文类型:

$ ls -dZ /data/containers/

unconfined_u:object_r:container_var_lib_t:s0 /data/containers/

确认类型是否已设置为container_var_lib_t。

重新运行容器:

# podman run --rm -it  ubuntu bash

root@615b27ff4e87:/# cat /etc/os-release

NAME="Ubuntu"

VERSION="20.04.1 LTS (Focal Fossa)"

ID=ubuntu

ID_LIKE=debian

PRETTY_NAME="Ubuntu 20.04.1 LTS"

VERSION_ID="20.04"

HOME_URL="https://www.ubuntu.com/"

SUPPORT_URL="https://help.ubuntu.com/"

BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"

PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"

VERSION_CODENAME=focal

UBUNTU_CODENAME=focal

root@615b27ff4e87:/# exit

exit

至此,容器已成功启动。

相关主题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值