Limiting CPU and Memory usage to users/groups(CGroups)

In this post we are going to see : Limiting CPU and Memory usage to users/groups.

In large environments, there is a chance to access single system by more than one users at a time. So, user’s can access the resources highly.

For our exercise going to use RHEL7 Operating System.

CGroups will help us to limit the resources by group of users.

We have below users created already to do practise.

[root@server ~]# grep home /etc/passwd
lbcuser1:x:1005:1007::/home/lbcuser1:/bin/bash
lbcuser2:x:1006:1008::/home/lbcuser2:/bin/bash
lbcuser3:x:1007:1009::/home/lbcuser3:/bin/bash
lbcuser:x:1008:1011::/home/lbcuser:/bin/bash

Users are assigned to below mentioned groups.

Groups:    lbcgroup, finance, admin

[root@server ~]# grep "lbcuser" /etc/group
finance:x:1003:lbcuser2
lbcgroup:x:1010:lbcuser1,lbcuser
admin:x:1012:lbcuser3

To work on this, lbcgroup package should be installed and will use /etc/cgconfig.conf and /etc/cgrules.conf to apply the rules overs the users to limit the resources use.

follow the below steps to apply rules by per Group:

[root@server ~]# vi /etc/cgconfig.conf
mount {
 cpu = /cgroup/cpu_and_mem;
 cpuacct = /cgroup/cpu_and_mem;
 memory = /cgroup/cpu_and_mem;
}

group finance {
 cpu {
 cpu.shares="250";
 }
 cpuacct {
 cpuacct.usage="0";
 }
 memory {
 memory.limit_in_bytes="1G";
 memory.memsw.limit_in_bytes="2G";
 }
}

group lbcgroup {
 cpu {
 cpu.shares="250";
 }
 cpuacct {
 cpuacct.usage="0";
 }
 memory {
 memory.limit_in_bytes="1G";
 memory.memsw.limit_in_bytes="2G";
 }
}

group admin {
 cpu {
 cpu.shares="500";
 }
 cpuacct {
 cpuacct.usage="0";
 }
 memory {
 memory.limit_in_bytes="1G";
 memory.memsw.limit_in_bytes="2G";
 }
}

While starting the server, above configuration file will mount cpu, cpuacct and memory subsystems to a cpu_and _memory cgroup.

CPU:

cpu-shares parameter used to assign the the CPU resources which is available to each and every processes. Assigning parameter values as 250, 250 and 500 for finance, lbcgroup and admin groups in cgroup will split the CPU resources in 1:1:2 ratio. If only one process is running, it doesn’t matter in which cgroup it falls. CPU limitation will be applied, when there is more than one process running.

cpuacct:

cpuacct.usage=”0″ this value is used to reset the CPU usage on cpuacct.usage and cpuacct_percpu files. These files contains the total CPU utilized time by all the process.

Memory:

memory.limit_in_bytes=”1G” parameter says that how much memory allowed to use by a cgroup.

memory.memsw.limit_in_bytes=”2G” parameter says that how much swap space allowed to use by a cgroup

cgrulesengd:

Start this daemon using below command:

[root@server ~]# systemctl start cgred

This daemon will help to move process to specific cgroup and for that we need to configure /etc/cgrules.conf like below

[root@server ~]# vi /etc/cgrules.conf
# /etc/cgrules.conf
#The format of this file is described in cgrules.conf(5)
#manual page.

@finance  cpu,memory finance
@lbcgroup cpu,memory lbcgroup
@admin    cpu        admin

Like above will assign rules to group.

In this, process will be moved to cgroup based on which user started this process and belongs to which group.

For example, Process detects the limitations from lbcgroup cgroup, which is started by lbcuser1 user and it will move to /cgroup/cpu_and_mem/lbcgroup/tasks file.

cgconfig:

Start this daemon to create hierarchy of cgroup to set the required parameters  in all cgroups.

[root@server ~]# systemctl start cgconfig

and to make all changes persistent across reboot, configure both(cgred, cgconfig) services to be started by default.

[root@server ~]# systemctl enable cgred

[root@server ~]# systemctl enable cgconfig

 

Reference: Red Hat official documentation

How to install Graphical user Interface/GUI on RedHat 7 Linux

In this post going to see How to install Graphical user Interface/GUI on RedHat Linux.

We have 7 run levels in RedHat and in that, runlevel 5 is used for GUI.

Use below command to change from current runlevel to GUI(before changing to graphical mode, make sure GNOME package installed):

[root@server ~]# init 5

To set GUI as default runlevel, use the below command and reboot the machine.

[root@server ~]# systemctl set-default graphical.target

If without any error runlevel switched to GUI, than package is already installed in your machine.

If you are receiving any error, than you should install the GNOME package.

Let’s see how to install the GUI in RedHat 7.

We will use yum package manager to install the GUI.

We have two types of repository that, one is online and another one is local repository.First decide which repository going to use to install GNOME package.

If you’re machine connected with internet, register in redhat repository using below command.

[root@server ~]# subscription-manager register
[root@server ~]# subscription-manager refresh
[root@server ~]# subscription-manager attach --auto

To use local repository make sure its already configured in your machine. Click here to know how to configure local yum repository.

Use the below command to check the group name for GNOME installation.

[root@server ~]# yum grouplist
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use su bscription-manager to register.
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Available Environment Groups:
 Minimal Install
 Infrastructure Server
 File and Print Server
 Basic Web Server
 Virtualization Host
 Server with GUI
Available Groups:
 Compatibility Libraries
 Console Internet Tools
 Development Tools
 Graphical Administration Tools
 Legacy UNIX Compatibility
 Scientific Support
 Security Tools
 Smart Card Support
 System Administration Tools
 System Management
Done

We should install “Server with GUI” using yum like below.

[root@server ~]# yum -y groupinstall "Server with GUI"

Now successfully installed GUI in our machine. Try to switch using “init 5” command.

Thanks for reading this post.

Reference: Red Hat Discussion

Bash shell script to run more commands which is stored in a file

Bash shell script to run more commands which is stored in a file

You might be tired by finding the right Bash shell script to run more commands which are stored in a file. Here is the script which you are searching…

In this example going to use below commands in the file to execute using the script.

df -h

uname -a

ls -l

netstat

As next step, we are going to create a file with above commands assigned to a variable. If we are using this commands directly without assigning to a variable, the script will not execute the commands properly. Because of space(df -h) which we used in between commands will be taken as a separator by the script. So that only we are saving the commands in a variable.

[root@server ~]# cat > cmd.lst
DF=`df -h`
UNAME=`uname -a`
CAT=`cat /etc/resolv.conf`
NET=`netstat` 
^c
[root@server ~]# 

Then create the script to run the commands which are stored in cmd.lst file

[root@server ~]# cat > cmdscritp.sh
#!/bin/bash
sh cmd.lst
for comm in `cat cmd.lst`
do
 for cmd in `cat cmd.lst | awk -F "=" {'print $1'}`
 do
 echo "Going to execute:" $cmd
 eval $cmd
 if [[ $? -eq "0" ]]
 then
 echo $cmd "executed successfully..."
 else
 echo "last command didn't executed properly. So, Exiting from the script..."
 exit
 fi
 done
done

and now provide the execute permission using chmod command for both files.

[root@server ~]# chmod +x cmd.lst
[root@server ~]# chmod +x cmdscritp.sh

[root@server ~]# ll | grep cmd
-rwxr-xr-x. 1 root root 69 Dec 28 23:58 cmd.lst
-rwxr-xr-x. 1 root root 326 Dec 28 23:58 cmdscritp.sh

Now run the script

[root@server ~]# ./cmdscript.sh 
cmd.txt: line 4: netstat: command not found
cmdscript.sh cmd.txt diff2.sh diff.sh lvcheck.txt post pre sh
Goind to execute: df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 10474496 2097740 8376756 21% /
devtmpfs 1887604 0 1887604 0% /dev
tmpfs 1894500 0 1894500 0% /dev/shm
tmpfs 1894500 8444 1886056 1% /run
tmpfs 1894500 0 1894500 0% /sys/fs/cgroup
tmpfs 378900 0 378900 0% /run/user/0
tmpfs 378900 0 378900 0% /run/user/1000
df executed successfully...
Goind to execute: uname
Linux
uname executed successfully...
Goind to execute: ls
cmdscript.sh cmd.txt diff2.sh diff.sh lvcheck.txt post pre sh
ls executed successfully...
Goind to execute: net
./cmdscript.sh: line 8: net: command not found
last command didnt executed properly. So, Exiting from the script...

 

Script working successfully. Thanks for reading the post and support.