Skip to content

Instantly share code, notes, and snippets.

@1eedaegon
Last active July 25, 2022 05:48
Show Gist options
  • Save 1eedaegon/f933b62b733c31d0bedf26626e2c3b23 to your computer and use it in GitHub Desktop.
Save 1eedaegon/f933b62b733c31d0bedf26626e2c3b23 to your computer and use it in GitHub Desktop.
How to simple use LVM

How to manage disk simply using LVM2

Set up disks scalably

Pre-requirements

Install two packages

  • lvm2
  • xfs
> apt install lvm2
> apt install xfsprogs

Check disks

fdisk -l | grep sd

> fdisk -l | grep sd
Disk /dev/sda: 931 GiB, 999653638144 bytes, 1952448512 sectors
/dev/sda1     2048    1050623    1048576   512M EFI System
/dev/sda2  1050624 1952446463 1951395840 930.5G Linux filesystem
Disk /dev/sdb: 931 GiB, 999653638144 bytes, 1952448512 sectors
/dev/sdb1        2048 1952448511 1952446464  931G 8e Linux LVM

Disk partioning: LVM

  • Type fdisk [DISK]
  • Press n and ENTER twice for new partition
  • Press t and 8e(LVM)
  • Press w for quit with save
> fdisk /dev/sdb

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
> blah

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux LVM' to 'Linux LVM'.

Command (m for help): w

Create PV(Physical Volume)

> pvcreate /dev/sdb1

Create VG(Volume Group)

> vgcreate [VG NAME] /dev/sdb1

Create LV(Logical Volume)

  • pvdisplay
  • lvcreate -l [PE SIZE] vg-docker -n lv-docker

Check PE size

> pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg-docker
  PV Size               <931.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              238335
  Free PE               0
  Allocated PE          238335
  PV UUID               IP67Qw-XR65-9knM-d98h-4xt8-9AhS-GvzdCv

Create LV

> lvcreate -l 238335 vg-docker -n lv-docker

Check LV

> lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg-docker/lv-docker
  LV Name                lv-docker
  VG Name                vg-docker
  LV UUID                IxdfE9-QtvH-qBN2-sHsP-FBmT-nxPO-3qDvjh
  LV Write Access        read/write
  LV Creation host, time wolf, 2022-06-28 18:38:22 +0900
  LV Status              available
  # open                 1
  LV Size                <931.00 GiB
  Current LE             238335
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

Format logical volume

> mkfs -t xfs /dev/vg-docker/lv-docker

Mount logical volume

> mount /dev/vg-docker/lv-docker /DOCKER

Check mount disk

> df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                                63G     0   63G   0% /dev
tmpfs                               13G  2.3M   13G   1% /run
/dev/sda2                          915G   12G  857G   2% /
tmpfs                               63G     0   63G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                               63G     0   63G   0% /sys/fs/cgroup
/dev/loop0                          66M   66M     0 100% /snap/gtk-common-themes/1515
/dev/loop1                          51M   51M     0 100% /snap/snap-store/547
/dev/loop2                         219M  219M     0 100% /snap/gnome-3-34-1804/72
/dev/loop3                          56M   56M     0 100% /snap/core18/2128
/dev/sda1                          511M  5.3M  506M   2% /boot/efi
tmpfs                               13G   52K   13G   1% /run/user/1000
/dev/loop5                          56M   56M     0 100% /snap/core18/2409
/dev/loop6                          47M   47M     0 100% /snap/snapd/16010
/dev/loop7                         128K  128K     0 100% /snap/bare/5
/dev/loop8                          62M   62M     0 100% /snap/core20/1518
/dev/loop9                          82M   82M     0 100% /snap/gtk-common-themes/1534
/dev/loop10                        219M  219M     0 100% /snap/gnome-3-34-1804/77
/dev/loop11                         55M   55M     0 100% /snap/snap-store/558
/dev/loop12                        255M  255M     0 100% /snap/gnome-3-38-2004/106
/dev/loop4                         401M  401M     0 100% /snap/gnome-3-38-2004/112
/dev/mapper/vg--docker-lv--docker  931G  6.6G  925G   1% /DOCKER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment