Requirements:
wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.12.24.tar.xz
wget http://hydra.azilian.net/3.12.24-config
cp 3.12.24-config .config
tar xfj linux-3.12.24.tar.xz
cd linux-3.12.24
git init
git add .
git commit -a -m 'initial'
Tasks:
- Patch /proc/cpuinfo, so when you are not in the main CGroup, it will show you only the CPU cores you are allowed to use.
- Patch /proc/stat, so when you are not in the main CGroup, it will show you only the CPU cores you are allowed to use.
- Patch /proc/meminfo, so when you are not in the main CGroup, it will show you the total and free memory that is set to your current CGroup.
- Patch /proc/partitions, so when you are not in the main CGroup, it will show you only the partitions/devices that you are allowed to use.
Tips:
- File systems code is located under the ‘fs’ directory. So the proc file system code is located under ‘fs/proc’ directory.
- When making your kernel use ‘-jX’ where X is the number of CPU cores you have +2. So if you have 4 cores, you should use -j6.
make -jX - Control Groups are Linux mechanism to impose limits to group of processes instead of per-process as it is normal. We will use this functionalities, to set limits to some resources.
cgroup - / |- cpuset.cpus (used for the /proc/cpuinfo limit) |- devices.allow (used to set the block device limit, | used for /proc/partitions limit) |- devices.list (used to read the limits imposed using | devices.allow |- memory.limit_in_bytes (used for the /proc/meminfo limit)
- Finding the current control group you are in. The kernel exposes one global pointer ‘current’ which is a pointer to the process currently working. ‘current’ is of type task_struct. The task_struct structure is defined in ‘include/linux/sched.h’. You must find where the control groups code reside, but keep in mind that most of the functionality around control groups uses this type of structure ‘cgroup_subsys_state’ and it is regulary shortend to only ‘css’.
Testing your code
After successful compilation you should be able to boot a VM with your new kernel. There are a few commands that are the same for all tests:
mkdir /cgroup
mount -t cgroup none /cgroup
mkdir /cgroup/tt
echo 0 > /cgroup/tt/cpuset.cpus
echo 0 > /cgroup/tt/cpuset.mems
echo $$ > /cgroup/tt/tasks
Then for testing the cpuinfo:
cat /proc/cpuinfo
Then for testing the stat:
cat /proc/stat
Then for testing the meminfo:
echo 268435456 > /cgroup/tt/memory.limit_in_bytes
cat /proc/meminfo
Then for testing the partitions:
echo 'b 8:1 rw' > /cgroup/tt/devices.allow
cat /proc/partitions