cgroup--device systemd-cgls

 

systemd-cgls

 

2. How to use cgroups?
The user can access and manage cgroups directly and indirectly (with LXC, libvirt or Docker).
Install the necessary packages:

$ sudo apt-get install libcgroup1 cgroup-tools
Now, the enabled cgroups can be seen via proc filesystem or sysfs:

$ cat /proc/cgroups

#subsys_name    hierarchy       num_cgroups     enabled
cpuset  9       2       1
cpu     4       134     1
cpuacct 4       134     1
blkio   7       134     1
memory  5       163     1
devices 11      134     1
freezer 2       2       1
net_cls 3       2       1
perf_event      10      2       1
net_prio        3       2       1
hugetlb 8       2       1
pids    6       136     1

$ ls -l /sys/fs/cgroup/

total 0
dr-xr-xr-x 6 root root  0 Nov 13 00:55 blkio
drwxr-xr-x 2 root root 60 Nov 13 01:00 cgmanager
lrwxrwxrwx 1 root root 11 Nov 13 00:55 cpu -> cpu,cpuacct
lrwxrwxrwx 1 root root 11 Nov 13 00:55 cpuacct -> cpu,cpuacct
dr-xr-xr-x 6 root root  0 Nov 13 00:55 cpu,cpuacct
dr-xr-xr-x 3 root root  0 Nov 13 00:55 cpuset
dr-xr-xr-x 6 root root  0 Nov 13 00:55 devices
dr-xr-xr-x 3 root root  0 Nov 13 00:55 freezer
dr-xr-xr-x 3 root root  0 Nov 13 00:55 hugetlb
dr-xr-xr-x 6 root root  0 Nov 13 00:55 memory
lrwxrwxrwx 1 root root 16 Nov 13 00:55 net_cls -> net_cls,net_prio
dr-xr-xr-x 3 root root  0 Nov 13 00:55 net_cls,net_prio
lrwxrwxrwx 1 root root 16 Nov 13 00:55 net_prio -> net_cls,net_prio
dr-xr-xr-x 3 root root  0 Nov 13 00:55 perf_event
dr-xr-xr-x 6 root root  0 Nov 13 00:55 pids
dr-xr-xr-x 6 root root  0 Nov 13 00:55 systemd
cgroups can be configured directly via the sysfs. For example, let’s create a small bash script named test_cgroups.sh for demonstration:

#!/bin/bash

while :
do
    echo "Print line" > /dev/tty
    sleep 5
done
Run above script:

$ chmod +x test_cgroups.sh
$ ./test_cgroups.sh
Print line
Print line
Print line
...
...
Change directory to /sys/fs/cgroup/devices where devices represents kind of resources that allows or denies access to devices by tasks in a cgroup:

$ cd sys/fs/cgroup/devices
Then, create a directory cgroups_test_group:

# mkdir cgroups_test_group
After creation of the cgroups_test_group directory, the following files will be generated:

$ ls -l /sys/fs/cgroup/devices/cgroups_test_group

total 0
-rw-r--r-- 1 root root 0 Nov 16 02:05 cgroup.clone_children
-rw-r--r-- 1 root root 0 Nov 16 02:05 cgroup.procs
--w------- 1 root root 0 Nov 16 02:05 devices.allow
--w------- 1 root root 0 Nov 16 02:05 devices.deny
-r--r--r-- 1 root root 0 Nov 16 02:05 devices.list
-rw-r--r-- 1 root root 0 Nov 16 02:05 notify_on_release
-rw-r--r-- 1 root root 0 Nov 16 02:05 tasks
The tasks file contains PIDs (Process ID) of processes which will be attached to the cgroups_test_group, the devices.deny file contains list of denied devices. By default, a newly created group has no any limits for devices access. In order to forbid a device (in this case, it’s /dev/tty), the devices.deny file should be modified:

# echo "c 5:0 w" > devices.deny
In the above command, the c indicates that /dev/tty is a character device, 5:0 is major and minor numbers of the device. The last w is write permission, so the above command forbids tasks to write to the /dev/tty.

$ ls -l /dev/tty

crw-rw-rw- 1 root tty 5, 0 Nov 18 17:02 /dev/tty
After that, re-run the script test_cgroups.sh:

$ ./test_cgroups.sh
Print line
Print line
Print line
...
...
then add the PID of this process to the tasks file:

# echo $(pidof -x test_cgroups.sh) > /sys/fs/cgroup/devices/cgroups_test_group/tasks
The result will be as expected:

$ ./test_cgroups.sh
Print line
Print line
Print line
./test_cgroups.sh: line 5: /dev/tty: Operation not permitted
./test_cgroups.sh: line 5: /dev/tty: Operation not permitted
...
...
An other example when running docker container

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS
98225055fa39        ubuntu              "/bin/bash"         47 seconds ago      Up 30 seconds

$ cat /sys/fs/cgroup/device/docker/98225055fa394b388e988b067b77dda61e53027ee944e4e0fd7887e19cdcf341/tasks
13556
During starting up of a docker container, docker creates a cgroup for processes in this container:

$ docker run -it ubuntu
$ top
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
   1  root      20   0   18508   1848   1444 S   0.0  0.0   0:00.01 bash
   12 root      20   0   36628   1924   1420 R   0.0  0.0   0:00.01 top
Now, the cgroup of above process will be seen on host machine:

$ systemd-cgls
Control group /:
-.slice
├─1429 /sbin/cgmanager -m name=systemd
├─docker
│ └─98225055fa394b388e988b067b77dda61e53027ee944e4e0fd7887e19cdcf341
│   └─13556 /bin/bash

 

 

 

type
type can have one of the following three values:
a — applies to all devices, both character devices and block devices
b — specifies a block device
c — specifies a character device

 

在/devices/cgroup  目录下创建目录  first,并设置禁止设备读:
root@ubuntu:/sys/fs/cgroup/devices# mkdir first
 
root@ubuntu:/sys/fs/cgroup/devices/first# echo "a 1:5 r" > devices.deny

在另外一个终端中设置:

root@ubuntu:~# cgexec -g devices:first dd if=/dev/zero of=zero bs=1M count=128 &
可见提示如下:
[1] 8973

 

root@ubuntu:/sys/fs/cgroup/devices# mkdir first
root@ubuntu:/sys/fs/cgroup/devices# ls -al
total 0
dr-xr-xr-x 10 root root   0 Sep 24 18:06 .
drwxr-xr-x 15 root root 380 Sep 24 18:06 ..
-rw-r--r--  1 root root   0 Sep 25 06:25 cgroup.clone_children
-rw-r--r--  1 root root   0 Sep 25 06:25 cgroup.procs
-r--r--r--  1 root root   0 Sep 25 06:25 cgroup.sane_behavior
drwxr-xr-x  2 root root   0 Oct 16 10:07 default
--w-------  1 root root   0 Sep 25 06:25 devices.allow
--w-------  1 root root   0 Sep 25 06:25 devices.deny
-r--r--r--  1 root root   0 Sep 25 06:25 devices.list
drwxr-xr-x  3 root root   0 Oct  9 15:45 docker
drwxr-xr-x  2 root root   0 Nov 17 19:47 first
drwxr-xr-x  4 root root   0 Oct 13 18:45 kubepods
drwxr-xr-x  4 root root   0 Oct 13 22:56 kubepods.slice
-rw-r--r--  1 root root   0 Sep 25 06:25 notify_on_release
-rw-r--r--  1 root root   0 Sep 25 06:25 release_agent
drwxr-xr-x 66 root root   0 Sep 24 18:06 system.slice
-rw-r--r--  1 root root   0 Sep 25 06:25 tasks
drwxr-xr-x  2 root root   0 Oct 31 11:10 test.slice
drwxr-xr-x  2 root root   0 Sep 24 18:06 user.slice
root@ubuntu:/sys/fs/cgroup/devices# ls first/
cgroup.clone_children  cgroup.procs  devices.allow  devices.deny  devices.list  notify_on_release  tasks
root@ubuntu:/sys/fs/cgroup/devices# 

 

原文地址:https://www.cnblogs.com/dream397/p/13996105.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


LinuxSystemd服务(2021.07.09)目录LinuxSystemd服务(2021.07.09)一、概述二、配置文件2.1Unit2.2Service2.3Install三、开机启动四、启动服务五、查看状态六、停止服务七、重启服务一、概述本文将介绍通过systemd来实现服务的自启动。systemd是一套系统启动和管理的工具,字
opidrvabortingprocessM002ospid(3561)asaresultofORA-600ORA-27300:操作系统相关操作:semctl失败,状态为:22ORA-27301:操作系统故障消息:InvalidargumentORA-27302:错误发生在:sskgpwrm1ORA-27157:已删除了操作系统发送/等待功能ORA-27300:操作系统相关操作
安装好haproxy后,配置正确无法启动,看日志:Feb1309:32:50cluster-node2systemd:StartedHAProxyLoadBalancer.Feb1309:32:50cluster-node2haproxy-systemd-wrapper:[ALERT]043/093250(6538):Startingproxymysql-pxc-cluster:cannotbindsocket[192.168.22.3
Linux 系统与服务管理工具Systemd被曝存在3大漏洞,影响几乎所有Linux发行版。Systemd是Linux系统的基本构建块,它提供了对系统和服务的管理功能,以PID1运行并启动系统的其它部分。目前大部分Linux发行版都以Systemd取代了原有的SystemV。安全公司Qualys近日发
一、systemd查看日志文件有隐藏 systemctlstatusSERVICE-l-l选项显示完整选项 journalctl-uSERVICE使用journalct命令查看 二、写一个systemd的配置文件,让nginx服务可以开机启动[Unit]Description=nginx[Service]Type=forkingPIDFile=/varunginx.pidExec
不要在mp目录下保存文件,该目录会定期清理文件mp默认保存10天/varmp默认保存30天配置文件:/usr/libmpfiles.dmp.conf默认配置文件:#Thisfileispartofsystemd.##systemdisfreesoftware;youcanredistributeitand/ormodifyit#underthetermsofthe
Step1:查看系统默认运行级别[root@node-1html]#systemctlget-default    //图形界面graphical.target[root@node-1html]#systemctllist-units--type=target  //查看支持的运行级别Step2:更改运行级别为level3 [root@node-1html]#systemctlset-defaultm
1.安装蓝牙驱动管理#apt-getinstallblueman2.打开蓝牙驱动管理,关闭设备3.关闭蓝牙开机启动服务#systemctldisablebluetooth.service#/lib/systemd/systemd-sysv-installdisablebluetooth4.重启reboot 
dhcpcd项目地址:http://www.linuxfromscratch.org/blfs/view/stable-systemd/basicnet/dhcpcd.html1.下载dhcpcd包并校验md5wgethttp:/oy.marples.name/downloads/dhcpcd/dhcpcd-7.0.7.tar.xzmd5sum-cmd5sums2.解压并进入包目录tar-xvfdhcpcd-7.0.7.tar.xzcddhcp
1.背景首先,我们先看一下/etc/init.d/README内容:Youarelookingforthetraditionalinitscriptsin/etcc.d/init.d,andtheyaregone?Here'sanexplanationonwhat'sgoingon:Youarerunningasystemd-basedOSwheretraditionalinitscriptshavebe
早就发现了,Arch的systemd提供的那个rc-local.service貌似有问题,rc.local不会执行。因为没用rc.local,一直没管。解决方法源自这里,需要稍加改动:http://superuser.com/questions/278396/systemd-does-not-run-etc-rc-local建立文件/etc/systemd/systemclocal.service(我怕和系
转载:https://www.cnblogs.com/sparkdev/p/8521812.html我们运行linux服务器的主要目的是通过运行程序提供服务,比如mysql、webserver等。因此管理linux服务器主要工作就是配置并管理上面运行的各种服务程序。在linux系统中服务程序的管理主要由init系统负责。如同笔者在
系统:Ubuntu18.04.02K8s版本:1.13.4故障现象:安装KubeDNS后,Pod内无法ping通外网域名,访问外网IP、K8s内部域名或者IP均正常  原因分析:查看Pod中的resolv.conf:kubectlexecbusybox--cat/etcesolv.confnameserver10.96.0.10searchdefault.svc.cluster.localsvc.cl
1.journalctl :日志查看工具journalctl -n5 //查看最近3条日志journalctl -perr //查看错误日志journalctl -overbose //查看日志的详细参数journalctl --since //查看从什么时间开始的日志journalctl --until //查看到什么时间为止的日志
此案例是以一个主,三个node来部署的,当然node可以根据自己情况部署192.168.1.130master192.168.1.131node1192.168.1.132node2192.168.1.133node3合法的EnableNTPonmasterandallnodes:[root@k-master~]#yum-yinstallntp[root@k-master~]#systemctlstartntpd[r
常用安装包下载yuminstall-yepel-releaseyum-yinstallbash-completionyum-yinstallnet-toolsyum-yinstalliprouteyum-yinstallwgetvimyum-yinstalllrzsznmaptreedos2unixnctelnetyum-yinstallopenssl一、系统类型1.1sysvinit1.系统第一个进程(p
修改了/etc/systemd/system.conf以后,发现不生效?修改了/etc/systemd/system.conf以后,必须使用systemctldaemon-reexec命令才能生效,使用systemctldaemon-reload是没有用的。daemon-reload重新加载的是所有单元文件,而不是systemd本身的配置。一定要注意了。被坑了。#addin/
Manjaro启动项目及服务配置备忘===============系统服务GUI管理搜索 systemdgenie 并安装,类似Windows的服务管理。================系统启动项目的快捷方式放在如下2个地方:/etc/xdg/autostart/cd~/.config/autostart,比如:/homeom/.config/autostart/===============#net
*1、systemd查看日志文件有隐藏该如何处理?答:Centos7.x使用systemd提供的journalctl日志管理a.基本上,系统由systemd所管理,那所有经由systemd启动的服务()如果在启动或结束的过程中发生了一些问题或是正常的信息),就会将该信息由systemd-journald.service以二进制的方式记录下来,之后
环境:centos7 创建的开机启动的链接地址: /etc/systemd/system/multi-user.target.wants/ 如:[root@tiaobanjisystem]#ll/etc/systemd/system/multi-user.target.wantsotal0lrwxrwxrwx.1rootroot38Feb2812:18auditd.service->/usr/lib/systemd/system/audit