Linux – Resizing swap partition (RHEL 5.x)

linuxpartitionrhel5swap

Following is how parted print looks like:

(parted) print                                                            

Model: VMware Virtual disk (scsi)
Disk /dev/sda: 26.8GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      32.3kB  271MB   271MB   primary  ext2         boot 
 2      271MB   1349MB  1078MB  primary  linux-swap        
 3      1349MB  26.8GB  25.5GB  primary               lvm  

The volume group created on /dev/sda3 looks like the following

  --- Volume group ---
  VG Name               rootvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  8
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                7
  Open LV               7
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               23.62 GB
  PE Size               128.00 MB
  Total PE              189
  Alloc PE / Size       162 / 20.25 GB
  Free  PE / Size       27 / 3.38 GB
  VG UUID               1Wzcpj-bNMD-cIYr-pOwA-1jdP-f9wE-wiEitV

That means there is 3.38G unused space.

I want to resize my swap partition /dev/sda2 to use 1GB out of the above space. How can I achieve that?

Best Answer

Linux can use multiple swap partitions as part of it's swap pool, so what your asking is easily acheived by creating a swap volume inside of the LVM and turning it on. Here's an overview:

Use lvcreate to create the logical volume:

lvcreate -n swap2 -L 1G rootvg

Format the space as swap space:

mkswap /dev/rootvg/swap2

Activate the swap space:

swapon /dev/rootvg/swap2

Don't forget to update your fstab to mount the new swapspace at boot time. This should be as simple as copying the existing line that mounts /dev/sda2 as swap and changing that to /dev/rootvg/swap2.

Related Topic