Using LVM for an entire hard drive
On my new KVM system, I had a SSD for the Ubuntu OS, but wanted a separate LVM Logical Volume for the KVM-related files/folder, such as ISO files, KVM images, etc.
LVM understands hard drives without a partition table, and you can use the entire drive for LVM without partitioning. The drawback is that we'll have to use the whole device as physical volume, but that's exactly what I want to achieve.
Steps
-
Erase the current partition table on the drive, which isn't necessary with newer versions of
pvcreate
, but doesn't hurt anything:sudo dd if=/dev/zero of=/dev/sdb bs=512 count=1 1+0 records in 1+0 records out 512 bytes copied, 0.000714602 s, 716 kB/s
-
Create the Physical Volume (PV):
sudo pvcreate /dev/sdb Physical volume "/dev/sdb" successfully created.
-
Create the Volume Group:
sudo vgcreate kvm-vg /dev/sdb Volume group "kvm-vg" successfully created
-
Display the new Volume Group (VG):
sudo vgdisplay /dev/kvm-vg --- Volume group --- VG Name kvm-vg System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 1 Act PV 1 VG Size 931.51 GiB PE Size 4.00 MiB Total PE 238467 Alloc PE / Size 0 / 0 Free PE / Size 238467 / 931.51 GiB VG UUID 4F1YAS-cprX-inKs-eMdp-5aYy-tfCx-e6oAmB
-
Create the Logical Volume (LV), using all of the space on the VG (drive):
sudo lvcreate -n kvm-lv -l 100%FREE kvm-vg
-
Display the new Logical Volume we just created:
sudo lvdisplay kvm-vg/kvm-lv --- Logical volume --- LV Path /dev/kvm-vg/kvm-lv LV Name kvm-lv VG Name kvm-vg LV UUID Nu21c6-Bqhm-9eyN-07EU-o1UH-0UV3-kDkplg LV Write Access read/write LV Creation host, time ubuntu-kvm, 2021-06-09 20:34:21 +0000 LV Status available # open 0 LV Size 931.51 GiB Current LE 238467 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
-
Format the Logical Volume:
sudo mkfs.ext4 /dev/kvm-vg/kvm-lv mke2fs 1.45.5 (07-Jan-2020) Creating filesystem with 244190208 4k blocks and 61054976 inodes Filesystem UUID: 6ac3e69e-26bf-41c4-80f8-1cf255617584 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000, 214990848 Allocating group tables: done Writing inode tables: done Creating journal (262144 blocks): done Writing superblocks and filesystem accounting information: done
-
Create the '/kvm' directory, where we will keep all of our KVM-related files (ISOs, VMs, etc):
sudo mkdir /kvm
-
Get the block ID of the newly created LV:
sudo blkid /dev/sda2: UUID="2e0048b6-113d-46b3-9ee5-7dc82f55647e" TYPE="ext4" PARTUUID="a5c6d238-ab41-40a2-b6ac-7094d8f29353" /dev/sda3: UUID="xiRfps-E3kw-YvcU-zsWN-FHG2-kIhC-I03vEv" TYPE="LVM2_member" PARTUUID="c5a52c6f-4bf7-49c8-b3eb-5a4c9ec96320" /dev/mapper/ubuntu--vg-ubuntu--lv: UUID="6026a797-54ab-49f0-a224-471298225cf5" TYPE="ext4" /dev/loop0: TYPE="squashfs" /dev/loop1: TYPE="squashfs" /dev/loop2: TYPE="squashfs" /dev/sdb: UUID="J4nd3U-fbRo-4CrX-a24z-Vcoh-X6Zf-C7iIae" TYPE="LVM2_member" /dev/sda1: PARTUUID="84c8ca8d-0f2a-4700-b168-676b8e34d18a" /dev/mapper/kvm--vg-kvm--lv: UUID="6ac3e69e-26bf-41c4-80f8-1cf255617584" TYPE="ext4"
The LV we just created has a UUID of 6ac3e69e-26bf-41c4-80f8-1cf255617584
-
Verify that the UUID exists in the /dev/disk/by-uuid/ directory:
ls -l /dev/disk/by-uuid/6ac3e69e-26bf-41c4-80f8-1cf255617584 lrwxrwxrwx 1 root root 10 Jun 9 20:53 /dev/disk/by-uuid/6ac3e69e-26bf-41c4-80f8-1cf255617584 -> ../../dm-1
-
Create a fstab entry for the LV, so it gets mounted as /kvm on bootup, by adding this line to the bottom of the /etc/fstab file.
We can do this by adding this line to /etc/fstab as root (must be root and not using
sudo
):/dev/disk/by-uuid/6ac3e69e-26bf-41c4-80f8-1cf255617584 /kvm ext4 defaults 0 0
-
Reload fstab, so the new mount point shows up:
sudo mount -a
-
Verify the new directory shows up:
df -h /kvm Filesystem Size Used Avail Use% Mounted on /dev/mapper/kvm--vg-kvm--lv 916G 77M 870G 1% /kvm
Not necessary, but I always like to double check, reboot, then verify that /kvm is mounted after rebooting.
References:
Man page for pvcreate http://manpages.ubuntu.com/manpages/focal/en/man8/pvcreate.8.html
LVM set up on Ubuntu https://www.maketecheasier.com/lvm-set-up-ubuntu/