2

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?

Caleb
  • 11,583
  • 4
  • 35
  • 49
ring bearer
  • 157
  • 7

2 Answers2

3

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.

Kyle Smith
  • 9,563
  • 1
  • 30
  • 32
  • Thanks for the response; isn't it recommend to have swap in contiguous space ? – ring bearer Jun 09 '11 at 14:29
  • 1
    I'd be interested to hear about why that is. Never heard that before. – Kyle Smith Jun 09 '11 at 14:36
  • 3
    @ring bearer - I think you might be confused by the recommendation that the logical volume use contiguous space when using it for swap. You can use the '-C' option to lvcreate to ensure it allocates accordingly. – JimB Jun 09 '11 at 14:55
  • @JimB +1. Didn't know about the -C option. – Kyle Smith Jun 09 '11 at 14:58
  • @JimB - is /dev/sda2 and lvm swap being non contiguous a concern? – ring bearer Jun 10 '11 at 15:15
  • @ring bearer - No, two swap areas won't be a problem. The only thing you would need to avoid is if the logical volume was made up of small pieces broken up across multiple devices, or fragmented across the disk (which is a reason not to use a file as swap). – JimB Jun 10 '11 at 15:35
2

You can't directly extend your existing swap partition, because it's a physical dos partition, and not in lvm.

You you can create a new 1G lvm volume, then mkswap and swapon. The kernel will make use of both swap areas seamlessly.

JimB
  • 1,924
  • 12
  • 15