July 14, 2022

RHEL 9.0 LVM, Extend and Swap

Introduction

Logical Volume Manager (LVM)

Physical Volumes (PVs)

Volume Groups (VGs)

Logical Volumes (LVs)

Create Partition Table, PV, VG and LV

# lsblk -fp
NAME                          FSTYPE      FSVER    LABEL UUID                                   FSAVAIL FSUSE% MOUNTPOINTS
/dev/sr0                                                                                                       
/dev/vda                                                                                                       
├─/dev/vda1                   xfs                        e8e38d31-36a2-4ad7-9668-94023cd80424    817.8M    19% /boot
└─/dev/vda2                   LVM2_member LVM2 001       y0wzxQ-mGYD-OfjS-LxuL-l2gJ-Rfgt-h5UgR6                
  ├─/dev/mapper/rhel_rhel9-root
  │                           xfs                        221f6235-b21f-48e6-befc-489e271de1f0     15.9G     6% /
  └─/dev/mapper/rhel_rhel9-swap
                              swap        1              245cf443-6a9e-4d32-b7ad-0cbf15a9020d                  [SWAP]
/dev/vdb                                                                                                       
/dev/vdc             

# man parted 
...
              mklabel label-type
                     Create a new disklabel (partition table) of label-type.   label-type  should  be  one  of  "aix",
                     "amiga", "bsd", "dvh", "gpt", "loop", "mac", "msdos", "pc98", or "sun".

              mkpart [part-type name fs-type] start end
                     Create  a  new partition. part-type may be specified only with msdos and dvh partition tables, it
                     should be one of "primary", "logical", or "extended".  name is required for GPT partition  tables
                     and  fs-type  is  optional.   fs-type  can  be  one  of "btrfs", "ext2", "ext3", "ext4", "fat16",
                     "fat32", "hfs", "hfs+", "linux-swap", "ntfs", "reiserfs", "udf", or "xfs".
...
              set partition flag state
                     Change the state of the flag on partition to state.  Supported flags are: "boot", "root", "swap",
                     "hidden", "raid", "lvm",  "lba",  "legacy_boot",  "irst",  "msftres",  "esp",  "chromeos_kernel",
                     "bls_boot" and "palo".  state should be either "on" or "off".
...

# parted /dev/vdb mklabel gpt

# parted /dev/vdb mkpart first 0G 3G
# parted /dev/vdb set 1 lvm on 

# parted /dev/vdb mkpart second 3G 6G
# parted /dev/vdb set 2 lvm on 

# parted /dev/vdb print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name    Flags
 1      1049kB  3000MB  2999MB               first   lvm
 2      3000MB  6000MB  3000MB               second  lvm

# lsblk -fp
NAME                          FSTYPE      FSVER    LABEL UUID                                   FSAVAIL FSUSE% MOUNTPOINTS
...
/dev/vdb                                                                                                       
├─/dev/vdb1                                                                                                    
└─/dev/vdb2                     

# pvcreate /dev/vdb1 /dev/vdb2

# vgcreate myvg01 /dev/vdb1

# lvcreate --name mylv01 --size 2.7G myvg01

# lsblk /dev/vdb -fp
NAME                          FSTYPE      FSVER    LABEL UUID                                   FSAVAIL FSUSE% MOUNTPOINTS
/dev/vdb                                                                                                       
├─/dev/vdb1                   LVM2_member LVM2 001       soNUus-2dYc-cTHE-OTXg-Ks1y-hQ6U-TLMgGz                
│ └─/dev/mapper/myvg01-mylv01 xfs                        5ac89db3-6bec-41f1-866d-e6afc3241ccd                  
└─/dev/vdb2                   LVM2_member LVM2 001       MIXCZT-j2af-G0e7-dN30-yeCW-NL9W-m73AFA           

# mkfs.xfs /dev/mapper/myvg01-mylv01

# mkdir /myvg01-mylv01

# vim /etc/fstab
...
/dev/mapper/myvg01-mylv01   /myvg01-mylv01    xfs   defaults    0 0

# mount -a

# echo "FOO" > /myvg01-mylv01/foo; cat /myvg01-mylv01/foo
FOO

Extend VG, LV and Resize Filesystem

# vgextend myvg01 /dev/vdb2

# lvextend --size +2.7G --resizefs /dev/mapper/myvg01-mylv01

# df -h /myvg01-mylv01/
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/myvg01-mylv01  5.4G   72M  5.4G   2% /myvg01-mylv01

Create Swap

# parted /dev/vdb mkpart third 6G 8G

# parted /dev/vdb set 3 lvm on

# pvcreate /dev/vdb3

# vgcreate myvg02 /dev/vdb3

# lvcreate --name mylv02 --size 1.8G myvg02

# mkswap /dev/mapper/myvg02-mylv02

# free 
               total        used        free      shared  buff/cache   available
Mem:         1301304      193984      770756        6976      336564      952776
Swap:        2097148           0     2097148

# swapon /dev/mapper/myvg02-mylv02

# free 
               total        used        free      shared  buff/cache   available
Mem:         1301304      194772      769936        6976      336596      951988
Swap:        3985400           0     3985400

# vim /etc/fstab
...
/dev/mapper/myvg02-mylv02   none    swap    defaults    0 0

# mount -a

No comments: