Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgroupsv2 adjustments #15

Closed
wants to merge 5 commits into from
Closed

cgroupsv2 adjustments #15

wants to merge 5 commits into from

Conversation

x7upLime
Copy link

Makes the proof of concept work for cgroupsv2 scenarios:
.Adjust the logic in the cg(), to reflect the new cgroupsv2 hierarchy
.Adds a cgroup-cleanup mechanism that would work for cgroupsv2.
....commits should tell more

Tied to #14
proposes solution to #10 (even though there is already one at the end of the thread)

Makes cgroups/custom_cgroup variables global
so that they could be easily shared by cg() and cgCleanup()
Not really necessary..
it proposes a "get-an-fs" workflow in a "clean-after-reboot" fashion
I assumed "thing" would just be tossed by Mount()
and that we just needed a (/mytemp) location inside our container to use as a tmpfs
Since the "echo 1 > notify_on_release" feature from cgroupsv1
seems to have been abandoned in cgroupsv2 and
(as far as I can tell from [*]) there is no "builtin" cleanup mechanism,
I think that some extra go code would be necessary:

Initially I thought about something employing "internal/poll",
as the documentation suggested, but that would've been overcomplicating the poc..

Perhaps an explicit force-removal call, after run(),
could be the simplest implementation.

(that process check before the rmdir..
maybe its not something we really need..)

[*] https://docs.kernel.org/admin-guide/cgroup-v2.html#un-populated-notification
It also leaves the file's mod unaltered
@x7upLime x7upLime closed this Jan 16, 2024
@netskink
Copy link

hello @x7upLime , you closed this issue without it being merged. Any particular reason why? I am trying to get this to work as well and I am using your code as a reference.

FWIW, I was using a busybox rootfs. I didn't see any items in /sys. I figured it was populated by systemd. I switched to a ubuntu rootfs. I still don't see any times in /sys.

I'll let you know if using your version of the code works.

@netskink
Copy link

Hmm. Ok here is the deal.

I am using linux kernel 5.x.

If I use the code as is and tweak for mods similar to yours, I can create the files as you/she did. However, they have no effect on the number of processes.

Alternatively, if I mount sysfs I can see a similar sys directory as I do on the host. That is good. What's bad, is the result is not modifiable. Here is mounting the filesystem but using interactive mode to attempt to write to it. (despite its mounted read/write)

Screenshot 2024-01-24 at 10 51 22 AM

Here is the code for cg() during this run:

func cg() {

    // Path name for the custom v2cgroup
    cgroups := "/sys/fs/cgroup/"
    custom_cgroup := filepath.Join(cgroups, "davis")

    // Make the custom v2cgroup
    err := os.MkdirAll(custom_cgroup, 0755)
    chkerr("after mkdir custom cgroup", err)

}

@netskink
Copy link

Here is an example of the alternative method. Not mounting sysfs, making the pids.max but not having any effect. Also note, I'm creating a max pid of 3. But i'm able to spawn bash 5 times. FWIW, this is writing to the actual roofs image mounted as root. So if my rootfs/sys on host was originally just a mount point, doing without mounting sysfs means these writes are being done to the filesystem and not to memory/kernel.

Screenshot 2024-01-24 at 11 49 35 AM

In this case, cg() looks like this:

func cg() {

    // Path name for the custom v2cgroup
    cgroups := "/sys/fs/cgroup"
    custom_cgroup := filepath.Join(cgroups, "davis")

    // Make the custom v2cgroup
    err := os.MkdirAll(custom_cgroup, 0755)
    if err != nil && !os.IsExist(err) {
        panic(err)
    }

    // limit to 3 processes
    must(ioutil.WriteFile(filepath.Join(custom_cgroup, "pids.max"), []byte("3"), 0744))
    // removes the new cgroup in place after the container exits
    must(ioutil.WriteFile(filepath.Join(custom_cgroup, "notify_on_release"), []byte("1"), 0744))
    must(ioutil.WriteFile(filepath.Join(custom_cgroup, "cgroup.procs"), []byte(strconv.Itoa(os.Getpid())), 0744))

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants