Bookmark this page

Practice: Adding and Enabling Swap Space

In this lab, you will create a swap partition and enable it for use.

Resources:
Machines: serverX

Outcomes:

Your serverX host will have 500 MiB of swap space running on its second disk.

  • Log into serverX.

  • Switch to root using sudo -i.

No swap partition was created during the installation of serverX. During peak usage, the server has been running out of physical memory. You have ordered additional RAM and are anxiously waiting for its arrival. In the meantime, you decide to alleviate the problem by enabling swap space on the second disk. To make sure that the newly added swap space is always available for use, you will also need to configure it to be enabled upon boot.

Once you have completed your work, reboot your serverX machine and verify that the swap space is available after the reboot.

  1. Create a 500 MiB partition on /dev/vdb of type Linux swap.

    1. Use fdisk to modify the second disk.

      [root@serverX ~]# fdisk /dev/vdb
    2. Print the original partition table, then create a new partition that is 500 MiB in size.

      Command (m for help): p
      
      Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
      Units = sectors of 1 * 512 = 512 bytes
      Sector size (logical/physical): 512 bytes / 512 bytes
      I/O size (minimum/optimal): 512 bytes / 512 bytes
      Disk label type: dos
      Disk identifier: 0xfd41a9d3
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/vdb1            2048     2099199     1048576   83  Linux
      
      Command (m for help): n
      Partition type:
         p   primary (1 primary, 0 extended, 3 free)
         e   extended
      Select (default p): p
      Partition number (2-4, default 2): 2
      First sector (2099200-20971519, default 2099200): Enter
      Using default value 2099200
      Last sector, +sectors or +size{K,M,G} (2099200-20971519, default 20971519): +500M
      Partition 2 of type Linux and of size 500 MiB is set
      
      Command (m for help): p
      
      Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
      Units = sectors of 1 * 512 = 512 bytes
      Sector size (logical/physical): 512 bytes / 512 bytes
      I/O size (minimum/optimal): 512 bytes / 512 bytes
      Disk label type: dos
      Disk identifier: 0xfd41a9d3
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/vdb1            2048     2099199     1048576   83  Linux
      /dev/vdb2         2099200     3123199      512000   83  Linux
                  
    3. Set the newly created partition to type Linux swap.

      Command (m for help): t
      Partition number (1,2, default 2): 2
      Hex code (type L to list all codes): L
      
      ...
       1 FAT12  27 Hidden NTFS Win  82 Linux swap / So c1  DRDOS/sec (FAT-
      ...
      Hex code (type L to list all codes): 82
      Changed type of partition 'Linux' to 'Linux swap / Solaris'
      
      Command (m for help): p
      
      Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
      Units = sectors of 1 * 512 = 512 bytes
      Sector size (logical/physical): 512 bytes / 512 bytes
      I/O size (minimum/optimal): 512 bytes / 512 bytes
      Disk label type: dos
      Disk identifier: 0xfd41a9d3
      
      Device Boot    Start      End   Blocks  Id  System
      /dev/vdb1       2048  2099199  1048576  83  Linux
      /dev/vdb2    2099200  3123199   512000  82  Linux swap / Solaris
    4. Save the partition table changes.

      Command (m for help): w
      The partition table has been altered!
      
      Calling ioctl() to re-read partition table.
      
      WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
      The kernel still uses the old table. The new table will be used at
      the next reboot or after you run partprobe(8) or kpartx(8)
      Syncing disks.
    5. Run partprobe to make the kernel aware of the partition table change.

      [root@serverX ~]# partprobe
  2. Initialize the newly created partition as swap space.

    [root@serverX ~]# mkswap /dev/vdb2
    Setting up swapspace version 1, size = 511996 KiB
    no label, UUID=74f8f3e1-6af3-4e51-9ab5-c48e52bf4a7b
  3. Enable the newly created swap space.

    1. Creating and initializing swap space does not yet enable it for use, as shown by the free and swapon -s command.

      [root@serverX ~]# free
                   total       used       free     shared    buffers     cached
      Mem:       1885252     557852    1327400      17096       1080     246040
      -/+ buffers/cache:     310732    1574520
      Swap:            0          0          0
      [root@serverX ~]# swapon -s
      [root@serverX ~]#
    2. Enable the newly created swap space.

      [root@serverX ~]# swapon /dev/vdb2
    3. Verify that the newly created swap space is now available.

      [root@serverX ~]# swapon -s
      Filename				Type		Size	Used	Priority
      /dev/vdb2                              	partition	511996	0	-1
      
    4. Disable the swap space.

      [root@serverX ~]# swapoff /dev/vdb2
    5. Verify that the swap space is disabled.

      [root@serverX ~]# swapon -s
      [root@serverX ~]#
  4. Configure the new swap space so that it is enabled upon boot.

    1. Determine the UUID of the new swap partition on the second disk.

      [root@serverX ~]# blkid /dev/vdb2
      /dev/vdb2: UUID="74f8f3e1-6af3-4e51-9ab5-c48e52bf4a7b" TYPE="swap"
    2. Add an entry to /etc/fstab.

      UUID=74f8f3e1-6af3-4e51-9ab5-c48e52bf4a7b swap swap defaults 0 0
    3. Test enabling the swap space using the entry just added to /etc/fstab.

      [root@serverX ~]# swapon -a
    4. Verify that the new swap space was enabled.

      [root@serverX ~]# swapon -s
      Filename                      Type            Size    Used    Priority
      /dev/vdb2                     partition       511996  0       -1
      
  5. Reboot serverX. After the server has rebooted, log in and verify that swap space is enabled.

    [student@serverX ~]# swapon -s
    Filename                      Type            Size    Used    Priority
    /dev/vdb2                     partition       511996  0       -1
    
Revision: rh134-7-c643331