系统相关
首页 > 系统相关> > Linux Cgroups介绍

Linux Cgroups介绍

作者:互联网

Linux Cgroups介绍

Cgroups 三个组件

三个组件相互的关系

kernel接口

mkdir cgroup-test
mount -t cgroup -o none,name=cgroup-test cgroup-test ./cgroup-test  
cd cgroup-test  
mkdir cgroup-1 cgroup-2
cd cgroup-1
echo $$
sh -c "echo $$" >>tasks  //将终端进程移动到cgroup-1中
cat /proc//cgroup

mount|grep memory

stress --vm-bytes 200m --vm-keep -m 1
cd /sys/fs/cgroup/memory
mkdir test-limit-memory && cd test-limit-memory
sh -c "echo "100m" > memory.limit_in_bytes"
sh -c "echo $$ > tasks"

stress --vm-bytes 200m --vm-keep -m 1


docker 是如何配置Cgroups的

docker run -itd -m 128m ubuntu
cd /sys/fs/cgroup/memory/docker/e9bc042a50c13f117a2bd8868d69234524432ecc7504bf803708a413dee3a5f3/
cat memory.limit_in_bytes

Go实现通过cgroup限制容器的资源

package main

import(


"os/exec"
"path"
"os"
"fmt"
"io/ioutil"
"syscall"
"strconv"

)
const cgroupMemoryHierarchyMount="/sys/fs/cgroup/memory"

func main(){

if os.Args[0]=="/proc/self/exe"{

fmt.Printf("current pid %d",syscall.Getpid())
fmt.Println()
cmd:=exec.Command("sh","-c",`stress --vm-bytes 200m --vm-keep -m 1`)
cmd.SysProcAttr=&syscall.SysProcAttr{}
cmd.Stdin=os.Stdin
cmd.Stdout=os.Stdout
cmd.Stderr=os.Stderr
if err:=cmd.Run();err!=nil{
fmt.Println(err)
os.Exit(1)


}
}
cmd:=exec.Command("/proc/self/exe")
cmd.SysProcAttr=&syscall.SysProcAttr{
	Cloneflags:syscall.CLONE_NEWUTS|syscall.CLONE_NEWPID|syscall.CLONE_NEWNS,}
cmd.Stdin=os.Stdin
cmd.Stdout=os.Stdout
cmd.Stderr=os.Stderr
if err:=cmd.Start();err!=nil{

fmt.Println("ERROR",err)
os.Exit(1)

}else{

fmt.Printf("%v",cmd.Process.Pid)
os.Mkdir(path.Join(cgroupMemoryHierarchyMount,"testmemorylimit"),0755)
ioutil.WriteFile(path.Join(cgroupMemoryHierarchyMount,"testmemorylimit","tasks"),[]byte(strconv.Itoa(cmd.Process.Pid)),0644)
ioutil.WriteFile(path.Join(cgroupMemoryHierarchyMount,"testmemorylimit","memory.limit_in_bytes"),[]byte("100m"),0644)




}
cmd.Process.Wait()
}



标签:subsystem,memory,Cgroups,hierarchy,cmd,介绍,cgroup,Linux,进程
来源: https://www.cnblogs.com/binarysystemloophole/p/16560377.html