Created
October 25, 2020 13:00
-
-
Save satnami/da7ee2dec5a07baa14dbc2133de088d5 to your computer and use it in GitHub Desktop.
bocker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prepare a hash. We need it to identify our container. | |
$ uuid="ps_$(shuf -i 42002-42254 -n 1)" | |
# Prepare a root dir for all the containers. | |
$ btrfs_path='/var/bocker' && cgroups='cpu,cpuacct,memory'; | |
# Prepare root filesystem based on the given `$image`. | |
$ btrfs subvolume snapshot "$btrfs_path/$image" "$btrfs_path/$uuid" > /dev/null | |
# Create a cgroup | |
$ cgcreate -g "$cgroups:/$uuid" | |
# Control cgroup resource | |
$ cgset -r cpu.shares=512 "$uuid" | |
$ cgset -r memory.limit_in_bytes=512000000 "$uuid" | |
# Execute a given `$cmd` in the cgroup. | |
# We need to create a unique namespace for the command (unshare). | |
# We also need to change the root directory (chroot). | |
# We also need to mount the runtime (/proc). | |
# Logging is a bonus (tee). | |
$ cgexec -g "$cgroups:$uuid" \ | |
ip netns exec netns_"$uuid" \ | |
unshare -fmuip --mount-proc \ | |
chroot "$btrfs_path/$uuid" \ | |
/bin/sh -c "/bin/mount -t proc proc /proc && $cmd" \ | |
2>&1 | tee "$btrfs_path/$uuid/$uuid.log" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment