Skip to main content
LPIC-1 Exam 101

102.1 Design hard disk layout Part 2

By January 19, 2014No Comments
Weight 2
Description Candidates should be able to design a disk partitioning scheme for a Linux system.

Key Knowledge Areas:

  • Allocate filesystems and swap space to separate partitions or disks.
  • Tailor the design to the intended use of the system.
  • Ensure the /boot partition conforms to the hardware architecture requirements for booting.
  • Knowledge of basic features of LVM.

The following is a partial list of the used files, terms and utilities:

  • / (root) filesystem.
  • /var filesystem.
  • /home filesystem.
  • swap space.
  • mount points.
  • partitions.

It is normal, although not necessary, for Linux to use virtual memory or swap space. The swap space that makes up the virtual memory can exists as specific Linux swap partitions, (disk space) or as files within a traditional Linux file-system which, of course, ultimately is also on disk. Data that is stored in physical memory will be swapped to disk when the amount of free RAM decreases giving the ability to the system to have more RAM than is physically installed. The downside to this is that access to the virtual memory will be very much slower than access to the physical memory. The process of swapping data to and from virtual memory is known as paging and in some operating systems the swapfile is known as the pagefile.

To display your current swap space on a Linux OS we can use the command swapon(/sbin/swapon) with the -s option.

swapon -s

From the output of the system above we can see that we have one partition in use for swap and it is about 1.5GB the size showing as 1K units. We can see no virtual memory is in use and the priority is set to -1. The priority is used when more than one swap space is in use; the swap space with the highest number will be used first. If no priority is set then the number illustrates the order in which the swap was mounted, -1 first, a second un-prioritized area would be -2 and so on. The priority of -1 is still higher than a value of -2; so the first partition is used before the second.

Controlling how aggressively swap space is used

As mentioned before, swap space will be used when the physical memory free space diminishes; however rather than waiting until the RAM is completely exhausted swap space is used ahead of time. There is a configuration setting:

/proc/sys/vm/swappiness

This has a default value of 60 on some systems, on the latest version of Red Hat, (version 7), the value defaults to 30. The range of possible values is from 0 through to 100, where 0 only used swap when there is no RAM available and 100 most aggressively used swap:

  • 0: only use swap to avoid out of memory errors
  • 30: default Red Hat Enterprise Linux 7
  • 60: common default value on many systems
  • 100: most aggressive swap usage

To set a new value we can use the command:

sysctl -w vm.swappiness=20

To make the setting permanent add this line to your /etc/sysctl.conf

vm.swappiness=20

Test the performance changes yourself and create your own bench-march for your servers, desktops and laptops needs. Many advocate setting the value to 100 to keep the RAM free for what is most commonly accessed; the rest of the application bloat-ware can exist in virtual memory. Where you perhaps have servers with many GBs of RAM and you do not want swap to be used then tell the kernel your thoughts by setting the value to 0. Communication with the kernel is the key here; it cannot read your thoughts, not in this version anyway.

 

Setting a partition for swap

Having created the swap partition earlier, /dev/sdb1, we should set the swap area on the partition with mkswap(/usr/sbin/mkswap). This process writes to the first cylinder of the partition to invalidate and existing file system and write the meta-data for the swap area reading the system page size from the kernel. This specifies the minimum area to be written to when swapping occurs; often this is 4KB and can be read yourself with the command.

getconf PAGESIZE

 

Using mkswap we set the swap area:

mkswap /dev/sdb1

If we wanted to set the label we would use the -L option.

Create Swap Files

As well as swap existing on partitions swap space can exist as a file. Although not ideal this may be necessary because you do not have available un-partitioned space or perhaps you just want some temporary additional swap space to cater for a once of task or event. Firstly we must create a file, much as we first have to create a partition but we will use dd(/bin/dd) to create the file.

dd if=/dev/zero of=/swap count=100 bs=1024k

Here we create a swap file at the root of the file-system made up of 100 1024k blocks. Now that we have the file then again we can create the swap area as before:

mkswap -L swapfile /swap

Here we demonstrate the use of the -L option to set the label. Normally labels are not needed for files but can become alternatives to mounting devices. Finally we should secure this file to ensure that is is accessible only to the root user:

chmod 600 /swap 

Mounting swap space

Setting the swap space does not enable the swap file or partition to be used; they have to be mounted and we use the swapon command to mount swap files and partitions. To make these settings persistent we would add settings to the file: /etc/fstab.

We saw at the start of this tutorial that we could display swap space with swapon -s. The command retrieves this information from the file /proc/swaps.

cat /proc/swaps

We can see that as we have not mounted the swap partition or swap file they are not in use. If we remember that the priority of -1 indicates that no priority was set during the mount.

If we use swapon to mount the two new swap areas and display the swap space after

swapon /dev/sdb1; swapon /root; swapon -s

 

We see now that the partition priority is from -1 to -3. The first swap space to be used will be /dev/dm-0, then sdb1 etc. If we want to control the swap space we can turn off all swaps with swapoff and then set the swaps back on with the desired priority:

swapoff -a
 swapon -p 0 /dev/dm-0
 swapon -p 10 /swap
 swapon -p 20 /dev/sdb1
 swapon -s

Now the highest priority is /dev/sdb1 and this will be used first. On a reboot of the machine the only swap space to be used will be the original /dev/dm-0. To make these setting persistent we need to add them to the /etc/fstab

Adding these lines to the bottom of the file should suffice in our case.

/dev/sdb1  swap  swap  sw,pri=20 0 0
 /swap  swap  swap  sw,pri=10 0 0

We can leave the original swap reference untouched and it will default to the priority of -1 and will be used last. We can test these settings once saved by turning of the swaps:

swapoff -a

To read the swap settings from the /etc/fstab file we can use

swapon -a

This will mount all swap spaces defined in the /etc/fstab without us having to wait for a reboot to find out we have misspelled something.