LJason

LJason

野生程序猴子
twitter
github
nintendo switch
steam
bilibili
email

Linux's Black Technology - LVM

A few days ago, I used a Raspberry Pi to download the source code of Android 6.0. I had mounted a 32GB USB flash drive and a 16GB memory card. Online sources said that the Android 4.4 source code was over 20GB, so I thought 32GB should be enough for 6.0.

But who knew that the 32GB (actually only 30GB) was completely filled up, and it even prompted that there was not enough disk space.

Unfortunately, my 3TB external hard drive couldn't be mounted on the Raspberry Pi due to insufficient voltage, and I didn't have any storage larger than 32GB at hand. So I wondered if I could expand the USB flash drive (Note: Expanding is harmful!).

Later, I came across LVM (LVM is not for expanding).

Logical Volume Manager (LVM) is a logical volume management function provided by the Linux kernel. It creates a logical layer above the hard disk partition to facilitate the management of the hard disk partition system. - Wikipedia: Logical Volume Manager

The concept may be difficult to understand, so I drew a picture below (only to realize that I hadn't installed PS after reinstalling the system):

00

For users, there are multiple physical hardware devices such as USB flash drives and memory cards, but for Linux, there is only one partition.

Let's take downloading the Android source code as an example to help everyone understand.

I want to download the source code of Android 6.0. Since a 32GB USB flash drive is not large enough, I use LVM to merge a 32GB USB flash drive and a 16GB memory card into one partition (please backup before formatting).

Preparations#

Use the fdisk -l command to view the disks:

01

You can see that the actual capacity is 30GB for the USB flash drive and 14.6GB for the memory card. Then let's start merging. First, install LVM:

apt-get install lvm2

Since both are in NTFS format, they need to be formatted into LVM format. First, select the memory card (without "1"):

fdisk /dev/sda

First, p to view the partitions:

02

You can see that there is only one partition. Then d to select the partition to delete (since there is only one partition, it will be directly deleted without options):

03

Then n to create a new partition, just press Enter for all options:

04

Again, p to view the partitions:

05

You can see that the type has changed to Linux. However, to use LVM, the ID needs to be changed to 8e. Use t to change the partition type:

06

Since there is only one partition, it will directly select partition 1. Enter the partition type 8e, and then p to view again:

07

You can see that the ID has changed to 8e, and the type has changed to Linux LVM. Finally, w to write the changes:

08

Do the same for the remaining partition. The operations are the same. After finishing, use fdisk -l to view the partitions:

09

After both partitions have changed to Linux LVM type, we can officially start working with LVM.

Create Physical Volumes#

First, create physical volumes using the command pvcreate /dev/sda1 /dev/sdb1 /dev/sdc1 ....... You can create one or more. In this case, it is:

pvcreate /dev/sda1 /dev/sdb1

10

The creation is successful. You can use pvs to view the physical volumes:

11

Create Volume Group#

The command to create a volume group is:

vgcreate VolGroup /dev/sda1 /dev/sdb1

After creating, use vgs to view:

12

You can see that the size of the volume group is 44.56g, which is the sum of the USB flash drive and the memory card. Use vgdisplay VolGroup to view the details of the newly created volume group VolGroup:

13

Create Logical Volume#

In the details of VolGroup, we can see that the Total PE is 11408, which means the allocatable size is: 4M * 11408 = 45632M = 44.5625G

We use all 11408 PEs to create a logical volume named storage:

lvcreate -l 11408 -n storage VolGroup

Then use lvs to view:

14

Create a volume using the ext4 file system, and then mount it to /mnt/storage (remember to execute mkdir /mnt/storage to create the /mnt/storage folder first, I have created it before). Finally, use df -h to view the partition:

mkfs.ext4 /dev/VolGroup/storage
mount /dev/VolGroup/storage /mnt/storage

15

Mount on Startup#

Add a line to /etc/fstab:

/dev/mapper/VolGroup-storage /mnt/storage ext4 defaults 0 0

Then restart to see if it is automatically mounted.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.