LVM Basic Operations

From WikiMLT
Revision as of 19:58, 2 July 2022 by Spas (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

As­sum­ing we want to cre­ate LVM and we want to oc­cu­py the en­tire disk space at /​​​dev/​​​sdb. You can fdisk or gdisk to cre­ate a new GPT par­ti­tion ta­ble that will wipe all par­ti­tions and cre­ate new par­ti­tions you need:

  • If the de­vice /​​​dev/​​​sdb will be used used as boot de­vice you can cre­ate two par­ti­tions one for /​​​boot – Lin­ux ext4 and one for the root fs / – Lin­ux LVM.
  • If the de­vice /​​​dev/​​​sdb won't be used as boot de­vice you can cre­ate on­ly one for LVM.

Cre­ate par­ti­tion ta­ble:

sudo fdisk /dev/sdb
#fdisk
# create a new empty GPT partition table
Command (m for help): g
# add a new partition
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-488397134, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-488397134, default 488397134): +1G

Created a new partition 1 of type 'Linux filesystem' and of size 1 GiB.
# add a new partition
Command (m for help): n
Partition number (2-128, default 2):
First sector (2099200-488397134, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-488397134, default 488397134):

Created a new partition 2 of type 'Linux filesystem' and of size 231.9 GiB.
# change a partition type
Command (m for help): t
Partition number (1,2, default 2): 2
Partition type (type L to list all types): 31

Changed type of partition 'Linux filesystem' to 'Linux LVM'.
# print the partition table
Command (m for help): p
Disk /dev/sdb: 232.91 GiB, 250059350016 bytes, 488397168 sectors
Disk model: CT250MX500SSD1
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 005499CA-7834-434B-9C36-5306537C8CF1

Device       Start       End   Sectors   Size Type
/dev/sdb1     2048   2099199   2097152     1G Linux filesystem
/dev/sdb2  2099200 488397134 486297935 231.9G Linux LVM

Filesystem/RAID signature on partition 1 will be wiped.
#  verify the partition table
Command (m for help): v
No errors detected.
Header version: 1.0
Using 2 out of 128 partitions.
A total of 0 free sectors is available in 0 segments (the largest is (null)).
# write table to disk and exit
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

For­mat the first par­ti­tion /​​​dev/​​​sdb1 to ext4:

sudo mkfs.ext4 /dev/sdb1
#mke2fs
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: a6a72cfe-46f1-4caa-b114-6bf03f1efe7f
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

Cre­ate LVM phys­i­cals vol­ume at the sec­ond par­ti­tion /​​​dev/​​​sdb1:

sudo pvcreate /dev/sdb2
#lvm2 pvc
Physical volume "/dev/sdb2" successfully created.

Cre­ate LVM vol­ume group at /​​​dev/​​​sdb1:

sudo vgcreate lvm-vm-group /dev/sdb2
#lvm2 vgc
Physical volume "/dev/sdb2" successfully created.
  • lvm-vm-group is the name of the group, it is mater of your choice.

Cre­ate LVM log­i­cal vol­ume at lvm-vm-group:

sudo lvcreate -n vm-win-01 -L 60g lvm-vm-group
#lvm2 lvc
Logical volume "vm-win-01" created.
  • vm-win-01 is the name of the log­i­cal de­vice, it is mater of your choice.

Check the re­sult:

 lsblk | grep -P 'sdb|lvm'
#Out­put
├─sdb1                           8:17   0     1G  0 part
└─sdb2                           8:18   0 231.9G  0 part
  └─lvm--vm--group-vm--win--01 253:0    0    60G  0 lvm

Ref­er­ences: