Add new partitions and logical volumes, and swap to a system non-destructively

Note: This objective is oriented to work (add or extend) with the swap partition.

Case1, if you are asked for expanding the swap partition by 500 MB.

1. Check

# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv01 myvg1 -wi-ao---- 300.00m
lv02 myvg1 -wi-ao---- 100.00m
lv03 myvg1 -wi-ao---- 100.00m
root myvg1 -wi-ao---- 5.29g
swap myvg1 -wi-ao---- 716.00m

It is not advisable to modify the swap partition as we do with other fs. Instead, we must add a new partition and apply the appropriate fs:

Check with swapon command, the swap parittion:

# swapon
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 716M 0B -1

Check the free space you have:

# parted /dev/vdb unit MB print free
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 2255MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
0.03MB 1.05MB 1.02MB Free Space
1 1.05MB 263MB 262MB primary lvm
2 263MB 525MB 262MB primary lvm
3 525MB 787MB 262MB primary lvm
4 787MB 2255MB 1467MB extended
788MB 788MB 0.57MB Free Space
5 789MB 1051MB 262MB logical lvm
1051MB 2255MB 1204MB Free Space

2.Create partition:

# fdisk /dev/vdb
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.
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (2054144-4404019, default 2054144):
Using default value 2054144
Last sector, +sectors or +size{K,M,G} (2054144-4404019, default 4404019): +500M
Partition 6 of type Linux and of size 500 MiB is set
Command (m for help): t
Partition number (1-6, default 6):
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: 2254 MB, 2254858240 bytes, 4404020 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: 0xd4db8a1e
Device Boot Start End Blocks Id System
/dev/vdb1 2048 514047 256000 8e Linux LVM
/dev/vdb2 514048 1026047 256000 8e Linux LVM
/dev/vdb3 1026048 1538047 256000 8e Linux LVM
/dev/vdb4 1538048 4404019 1432986 5 Extended
/dev/vdb5 1540096 2052095 256000 8e Linux LVM
/dev/vdb6 2054144 3078143 512000 82 Linux swap / Solaris
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.

# partprobe -s
/dev/vda: msdos partitions 1 2
/dev/vdb: msdos partitions 1 2 3 4 <5 6>

3. Format partition as swap:

# mkswap /dev/vdb6
Setting up swapspace version 1, size = 511996 KiB
no label, UUID=c9f58c48-cb56-4461-94a7-93aab8b60174

4. Send entry to the fstab file:

# echo "UUID=c9f58c48-cb56-4461-94a7-93aab8b60174 swap swap defaults 0 0" >> /etc/fstab 

5. Mount the swap partition :

# swapon      //check//
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 716M 0B -1

# swapon -a

# swapon //check again//
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 716M 0B -1
/dev/vdb6 partition 500M 0B -2

Case2, if you are asked for create a swap partition in a vg :

1. Check

# lvs   
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv01 myvg1 -wi-ao---- 300.00m
lv02 myvg1 -wi-ao---- 100.00m
lv03 myvg1 -wi-ao---- 100.00m
root myvg1 -wi-ao---- 5.29g
swap myvg1 -wi-ao---- 716.00m
# vgs
VG #PV #LV #SN Attr VSize VFree
myvg1 3 5 0 wz--n- 6.48g 0
myvg2 2 0 0 wz--n- 496.00m 496.00m

2.Create partition:

 # lvcreate myvg2 -n swap01 -L 200M
Logical volume "swap01" created.
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv01 myvg1 -wi-ao---- 300.00m
lv02 myvg1 -wi-ao---- 100.00m
lv03 myvg1 -wi-ao---- 100.00m
root myvg1 -wi-ao---- 5.29g
swap myvg1 -wi-ao---- 716.00m
swap01 myvg2 -wi-a----- 200.00m

3. Format partition as swap:

# mkswap /dev/myvg2/swap01 
Setting up swapspace version 1, size = 204796 KiB
no label, UUID=b30531b1-2813-4e5d-ab50-f238fb6e9a9

4. Send entry to the fstab file:

# echo "UUID=b30531b1-2813-4e5d-ab50-f238fb6e9a9c swap swap defaults 0 0" >> /etc/fstab

5. Mount the swap partition :

# swapon 
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 716M 0B -1
/dev/vdb6 partition 500M 0B -2

# swapon -a

# swapon
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 716M 0B -1
/dev/vdb6 partition 500M 0B -2
/dev/dm-5 partition 200M 0B -3

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *