Tuesday, September 28, 2021

thumbnail

How to extend an LVM swap partition in Linux

Swap filesystem



Swap is used if there is not enough memory available for your application. It’s normal and can be a good thing for Linux systems to use swap, even if there is still available RAM. But, it’s not just used if there is not enough memory.

How to extend LVM based swap filesystem

To increase the space for LVM Swap partition follow the steps given below:

1. Verify availability of the new space.

# lsblk
NAME                        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                           8:0    0   30G  0 disk 
\u251c\u2500sda1                        8:1    0  512M  0 part /boot
\u2514\u2500sda2                        8:2    0 29.5G  0 part 
  \u251c\u2500VolGroup_ID_001-rootlv1 253:0    0   29G  0 lvm  /
  \u2514\u2500VolGroup_ID_001-swaplv1 253:1    0  512M  0 lvm  [SWAP]
sdb                           8:16   0   30G  0 disk 
\u2514\u2500sdb1                        8:17   0   30G  0 part 
  \u2514\u2500VolGroup_ID_002-tmplv1  253:2    0   30G  0 lvm  /tmp
sdc                           8:32   0    2T  0 disk 
\u251c\u2500sdc1                        8:33   0    2T  0 part 
\u2514\u2500sdc9                        8:41   0    8M  0 part 
sdd                           8:48   0    6G  0 disk 
sr0                          11:0    1 1024M  0 rom  
# fdisk -l /dev/sdd

Disk /dev/sdd: 6442 MB, 6442450944 bytes, 12582912 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

2. Create additional partition for the new swap partition.

# fdisk /dev/sdd
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x26e84ac1.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-12582911, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-12582911, default 12582911): 
Using default value 12582911
Partition 1 of type Linux and of size 6 GiB is set

Command (m for help): t
Selected partition 1
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/sdd: 6442 MB, 6442450944 bytes, 12582912 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: 0x26e84ac1

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048    12582911     6290432   82  Linux swap / Solaris

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

3. Activate the new partition.

# partprobe

4. Verify the new partition is available.

# lsblk
NAME                        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                           8:0    0   30G  0 disk 
\u251c\u2500sda1                        8:1    0  512M  0 part /boot
\u2514\u2500sda2                        8:2    0 29.5G  0 part 
  \u251c\u2500VolGroup_ID_001-rootlv1 253:0    0   29G  0 lvm  /
  \u2514\u2500VolGroup_ID_001-swaplv1 253:1    0  512M  0 lvm  [SWAP]
sdb                           8:16   0   30G  0 disk 
\u2514\u2500sdb1                        8:17   0   30G  0 part 
  \u2514\u2500VolGroup_ID_002-tmplv1  253:2    0   30G  0 lvm  /tmp
sdc                           8:32   0    2T  0 disk 
\u251c\u2500sdc1                        8:33   0    2T  0 part 
\u2514\u2500sdc9                        8:41   0    8M  0 part 
sdd                           8:48   0    6G  0 disk 
\u2514\u2500sdd1                        8:49   0    6G  0 part 
sr0                          11:0    1 1024M  0 rom  

Note: a reboot may be needed if the change does not show at this point.

5. Create a new physical volume on the LUN.

# pvcreate /dev/sdd1

6. Add the new volume to the volume group for the swap volume. Our examples use VolGroup_ID_001 and /dev/sdd1; replace with the volume names and devices as appropriate to your deployment.

# vgextend VolGroup_ID_001 /dev/sdd1

7. Disable swapping for the associated physical volume.

# swapoff -v /dev/VolGroup_ID_001/swaplv1

8. Resize the logical volume to the desired size.

lvresize /dev/VolGroup_ID_001/swaplv1 -l +1535

9. Format the extended swap volume.

# mkswap /dev/VolGroup_ID_001/swaplv1

10. Enable the logical volume.

# swapon -va

11. View the new swap size.

# free -m
              total        used        free      shared  buff/cache   available
Mem:          32013         886       29748           9        1378       30723
Swap:          6651           0        6651
# free
total used free shared buffers cached
Mem: 1784432 196920 1587512 516 12624 77268
-/+ buffers/cache: 107028 1677404
Swap: 1257468 0 1257468

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

Powered by Blogger.