# mount Cgroup shell script
mount -t tmpfs cgroup_root /sys/fs/cgroup/
mkdir /sys/fs/cgroup/cpuset
mount -t cgroup -ocpuset cpuset /sys/fs/cgroup/cpuset
mkdir /sys/fs/cgroup/blkio
mount -t cgroup -oblkio blkio /sys/fs/cgroup/blkio/
mkdir /sys/fs/cgroup/cpuacct
mount -t cgroup -ocpuacct cpuacct /sys/fs/cgroup/cpuacct/
mkdir /sys/fs/cgroup/devices
mount -t cgroup -odevices devices /sys/fs/cgroup/devices/
mkdir /sys/fs/cgroup/cpu
mount -t cgroup -ocpu cpu /sys/fs/cgroup/cpu
mkdir /sys/fs/cgroup/memory
mount -t cgroup -omemory memory /sys/fs/cgroup/memory
root@debian:~# cat > helloworld.xml <<EOF
<domain type='lxc'>
<name>helloworld</name>
<memory>102400</memory>
<os>
<type>exe</type>
<init>/bin/sh</init>
</os>
<devices>
<console type='pty'/>
</devices>
</domain>
EOF
root@debian:~# virsh -c lxc:/// define helloworld.xml
Domain helloworld defined from helloworld.xml
root@debian:~# virsh -c lxc:/// start helloworld
Domain helloworld started
root@debian:~# virsh -c lxc:/// list
Id Name State
----------------------------------
31417 helloworld running
root@debian:~# ls /root
helloworld.xml
root@debian:~#
root@debian:~#virsh -c lxc:/// console helloworld
Connected to domain helloworld
Escape character is ^]
# ls /root
# helloworld.xml
# touch /root/hello
# ls /root
hello helloworld.xml
#
root@debian:~# ls /root
hello helloworld.xml
root@debian:~# ls /proc/self/ns
ipc net uts
root@debian:~# virsh -c lxc:/// domuuid helloworld
5c4f5baf-eed3-4d80-8dbe-f2021f37610
root@debian:~# cat /etc/apparmor.d/libvirt/libvirt-5c4f5baf-eed3-4d80-8dbe-f2021f376103
#
# This profile is for the domain whose UUID matches this file.
#
#include <tunables/global>
profile libvirt-5c4f5baf-eed3-4d80-8dbe-f2021f376103 {
#include <abstractions/libvirt-lxc>
deny /root/* wklx,
# Globally allows everything to run under this profile
# These can be narrowed depending on the container's use.
file,
capability,
network,
}
在上述apparmor的profile配置中,我们禁止该lxc 写/root目录。
root@debian:~#virsh -c lxc:/// console helloworld
Connected to domain helloworld
Escape character is ^]
# ls /root
hello helloworld.xml
# touch /root/world
touch: cannot touch `/root/world': Permission denied
网易云新用户大礼包:https://www.163yun.com/gift
本文来自网易实践者社区,经作者管强授权发布。