Setting disk space limits

Setting hard drive limits manually

To add hard drive limits to a vserver first do the following. Create the directories "dlimits/root" in that vserver's configuration directory. Inside the "root" folder create four files:

directory This vserver's top-level directory - usually /var/lib/vservers/NAME
inodes_total Inode limit - we don't bother with this - set it to 10000000
reserved Percentage of space to reserve for root use. 5% is "typical", but we usually just set 1% so we don't reserve too much of the space they are paying for.
space_total Limit in Kilobytes. Multiply by 1024*1024 to get Gigabytes

So to create the files for a 5GB hard drive limit for the vserver "NAME", reserving 1% of the space for root, we run the following commands:mkdir -p /etc/vservers/NAME/dlimits/root echo "/var/lib/vservers/NAME" > /etc/vservers/NAME/dlimits/root/directory echo "1000000" > /etc/vservers/NAME/dlimits/root/inodes_total echo "1" > /etc/vservers/NAME/dlimits/root/reserved echo "5242880" > /etc/vservers/NAME/dlimits/root/space_total (Hard drive limits are expressed in kilobytes. So 5242880 = 1024 * 1024 * 5 -- 1024 kilobytes in a 1 megbyte and 1024 megabytes in 1 gigabyte.) Of course, the easier thing to do may be to copy this directory from another vserver which already has the limits set you want. "cp -a /etc/vservers/VS1/dlimits /etc/vservers/NAME" Then edit any files which need changing. From now the vserver will keep track of all new and changed files. But we still need to "tag" the already existing files in the vserver directory so that the vserver software will know it should include them. In order to do this, the partition that the vservers reside on must be mounted with the "tag" option. Also note that according to the vserver docs, you cannot use "-o remount,tag" to add the tag option. You must unmount and remount the drive (which means you must stop all your running vservers first, of course). To tag all the files for a particular vserver, run the command "chxid -URx -c VSERVER_NAME VSERVER_DIRECTORY". So in our example of the vserver called "NAME", we would runchxid -URx -c NAME /var/lib/vservers/NAMEThis tags all the existing file in this vserver's "hard drive". Note that if you ever change this vserver's name, you'll need to rerun the "chxid" command with the new name/location. If not you'll see weird file access errors. (At least we have.) Finally, we need to tell the vserver program how much space is already being used. It will keep track of changes from here, but it needs to know how much space is already used. You can find that out by running "vdu --xid VSERVER_NAME --space VSERVER_DIRECTORY" and then plugging that into the command "vdlimit --xid VSERVER_NAME --set space_used=X VSERVER_DIRECTORY". We can easily combine these to easily set how much space is already used. So for our vserver called "NAME", we run:vdlimit --xid NAME -s space_used=$(vdu --xid NAME --space /var/lib/vservers/NAME | cut -d" " -f2) /var/lib/vservers/NAME vdlimit --xid NAME -s inodes_used=$(vdu --xid NAME --inode /var/lib/vservers/NAME | cut -d" " -f2) /var/lib/vservers/NAME You can run "vdlimit --xid VSERVER_NAME VSERVER_DIRECTORY" at any time to see this vserver's present usage. Here's the command and output looking at the NAME server:vserv2:~# vdlimit --xid NAME /var/lib/vservers/NAME 5401 /var/lib/vservers/NAME space_used=4696844 space_total=5242880 inodes_used=39358 inodes_total=1000000 reserved=1 The first line tells us the Context number and the directory This vserver is using 4.5GB of space out of a total of 5GB. (The "space_xxx" numbers are in Kilobytes. So space_used = 4,696,844 kilobytes = 4.5 Gigabytes.)

Editing existing hard drive limits

Once the limits are set you can change them on the fly. You just need to adust the number in the "space_total" file you created above. Then you can either reboot the vserver so it picks up the new value, or you can use the "vdlimit" command to adjust the hard drive limit in the running vserver. Assuming we want to adjust the limit from the old 5GB to 6GB. Limits are specified in Kb, so we need to multiply to find the new limit.1024 * 1024 * 6 = 6291456So "6291456" is the new limit in Kb. Assuming our vserver is called "NAME", then we runecho "6291456" > /etc/vservers/NAME/dlimits/root/space_totalThis sets the limit so it will be picked up next time the vserver starts. Then in order to change the limit in the running server, we executevdlimit --xid NAME -s space_total=6291456 /var/lib/vservers/NAMESo the command is "vdlimit --xid VSERVER_NAME -s space_total=X VSERVER_DIRECTORY". In fact, using these two techniques is not mutally exclusive. You could set a new limit in the "space_total" file but not change the running limit. Then next time the vserver restarts, it will get the new limit. You can also change the limit in the running vserver but not change it in the file. Then the new hard drive limit will be in place until the vserver restarts or you manually change it again using the "vdlimit" command.

Removing hard drive limits

To remove the limits you simply delete the entire "dlimits" directory from the vserver's configuration folder and then use the "vdlimit" command to remove the limits from running vservers. The command is "vdlimit --xid VSERVER_NAME --remove VSERVER_DIRECTORY". So to remove the limits from our vserver called "NAME", we runrm -rf /etc/vservers/NAME/dlimits vdlimit --xid NAME --remove /var/lib/vservers/NAME That's it. Now the hard drive limits are removed and this virtual machine can access the entire partition that is created on.

Setting disk limits with "vs-disk.pl"

The "easy" way to set hard drive limits is to use the vs-disk.pl script. The syntax is "vs-disk.pl VSERVER_NAME" to view the disk limits and "vs-disk.pl VSERVER_NAME LIMIT" to set a hard drive limit of LIMIT Gigbytes. (You can add an optional 'G' or 'M' to the limit to specify whether the number is Gigabytes or Megabytes. The default is Gigabytes if you don't specify anything.) Here we are checking for disk limit on the vserver "lennytest"vs-disk.pl lennytest "lennytest" disk limit:    NOT ASSIGNED "lennytest" disk usage:    NOT AVAILABLE There are no limits assigned. This means that to find out how much "lennytest" is using, we need to run a 'du' on its directory. Let's use the vs-disk.pl script to add a 5GB disk limit to "lennytest".vs-disk.pl lennytest 5G Tagging all the files in this vserver. Please wait ........................ Done! Calculating existing space. Please wait ....... Done! "lennytest" disk limit:    5.0G "lennytest" disk usage:    540M "lennytest" disk  free:    4.5G Since Gigabytes is assumed if you don't specify 'M' or 'G', in the above command we could have just entered '5' instead of '5G'. The first time you set a limit it can take quite a long time to "tag" all the existing files and then calculate the space, depending on the size of the vserver files. The script will print out a period every second to show you it's running. Now if you run the "df" command from inside the guest you'll see these same figures.vserver lennytest exec df -h Filesystem Size Used Avail Use% Mounted on /dev/hdv1 5.0G 540M 4.5G 11% / The guest can only access 5GB of the host's hard drive because of the limit we have placed on it. You can remove a disk limit by setting the limit to "0". So "vs-disk.pl VSERVER_NAME 0" will remove any disk limits and allow the vserver unlimited access to the host's hard drive. Here we'll remove the disk limit from the "lennytest" vserver.vs-disk.pl lennytest 0 Removing disk limits. Please wait . Done! "lennytest" disk limit: NOT ASSIGNED "lennytest" disk usage: NOT AVAILABLE Now we're back to where we started. The "lennytest" vserver can access all the host's hard drive. If you run a "df" inside the guest, you'll see the host's hard drive size and usage, rather than the guest's.vserver lennytest exec df -h Filesystem Size Used Avail Use% Mounted on /dev/hdv1 912G 623G 243G 72% / 912GB is the size of the host's drive (or the partition that this vserver is mounted on) and 623GB is the space used by all the files on the host. Here's the corresponding host information for our vserver partition:df -h /dev/drbd1 912G 623G 243G 72% /home/drbd1 This script does not set limits on inodes and it only reserves 1% of the guest's drive for 'root'. Obviously you can change the script to set whatever values you want. A vserver's files only need to be tagged once, the first time you set a disk limit. From then on, the kernel will tag any new files created in that vserver. So if you are changing the hard disk limit on a vserver where the files have already been tagged, you can use the "-x/--xclude" switch with vs-disk.pl and it will NOT tag the files again. vs-disk.pl -x lennytest 5 sets a 5GB disk limit without tagging the files again. This can save considerable time if the vserver has a lot of files. On a vserver with 500GB of files, it takes our system almost 5 minutes to tag all the files and calculate the space using a new disk limit. If the files are already tagged and you use the "-x" switch to avoid tagging them again, it only takes 30 seconds to recalculate the space. Just make the sure the files are already tagged before you use the "-x" switch. Setting disk limits without properly tagging the files is useless at best and could cause your vserver to lock up at worst. If in doubt, let the vs-disk.pl script tag all the files again. It doesn't hurt anything and the vserver runs normally during the process. vs-disk.pl is a GPL'ed perl script. If you find any problems or make any improvements, please let us know so we can incorporate them.