Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and manage LVM logical volumes in Linux

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to create and manage LVM logic volumes in Linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to create and manage LVM logic volumes in Linux.

Summary LVM is a mechanism of disk partition management in Linux system. LVM is essentially a virtual device driver. LVM logical devices are not limited by physical constraints, it does not have to be continuous space, it can span many physical volumes and can be resized arbitrarily. LVM is easy to manage disk space.

Characteristic physical length PE physical length is the smallest storage unit divided by combining physical volumes into volume groups, that is, the smallest storage unit of disk in a logical sense. The default PE size for LVM is 4MB. Physical volume PV physical volume is the lowest concept of LVM and the logical storage block of LVM. Physical volume and disk partition are logical correspondence. Volume groups VG volume groups are disk devices in the logical concept of LVM that are generated by combining single or multiple physical volumes. The size of the volume group depends on the capacity and number of physical volumes. Logical volumes LV logical volumes are partitions in the logical sense of LVM. You can specify how much capacity to extract from the volume group to create logical volumes, and finally format and mount logical volumes for use. Operation flow 1. Fdisk / dev/sdd # n create new partition # t convert partition format # 8e convert partition format to 8e LVM2 format 2. Create a pv, convert the sector to pe, and verify pvcreate / dev/sdd1 / dev/sde1 pvs view pv information pvdisplay view pv information 3. Create vg virtual disk vgcreate mytest / dev/sdd1 / dev/sde1 find /-name mytest # find and view vg configuration information vgs view vg virtual disk information vgdisplay view vg virtual disk information 4. Create a logical partition lvcreate-L + 5G mytest (vg name), create a partition default lvol0 find / dev-name lvol0 to find the partition location / dev/mytest/lvol0, and create a vg-named directory under the dev directory. Format the file system mkfs.ext4 / dev/mytest/lvol0 6. Mount using mkdir / root/mylv mount / dev/mytest/lvol0. / mylv 1234567891011121314151617181920212223242526 related commands fdisk command syntax: fdisk [parameters] [object]

Necessary parameters

Parameter function-l lists all partition tables-u is used with "- l" to show the number of partitions and select parameters

Parameter function-s specify partition-v version information operation instructions

Parameter function m display menu and help an active partition mark / boot partition d delete partition l display partition type n new partition p display partition information Q exit do not save t set partition number v for partition check w save modification x extended application, advanced functions pvcreate command syntax: pvcreate [options] [parameters]

Parameter function-f forces the creation of a physical volume without user confirmation-u specifies the UUID-y of the device to answer all questions "yes"-Z whether to use the first four sectors vgcreate command syntax: vgcreate [options] [parameters]

Parameter function-maximum number of logical volumes allowed to be created on l volume group-maximum number of physical volumes allowed to be added in p volume group-PE size of physical volumes on s volume group lvresize command syntax: lvresize [options] [parameters]

Parameter function-L specifies the size of the logical volume in "kKmMgGtT" bytes-l specifies the size of the logical volume (number of LE) e2fsck command syntax: e2fsck [parameter] [object]

Parameter function-a checks partition and automatically fixes if problems are found-b sets the superblock location. Www.2cto.com-B size specifies size as the chunk size-c checks partition for bad tracks-C file saves the check results to file-d to output e2fsck debug results-fe2fsck preset only checks the wrong file system, plus-f is a mandatory check-F clears the buffer cache of the hard drive before checking Avoid errors-l list records bad rail blocks added to list-d print debug results of e2fsck-f forced check-n to (read-only) open file system-p turn off interactive mode, if there is a problem automatically fix, it is the same as-a Mutav display detailed report-y enable user interaction mode classic case requirements

Creating a 20G VG; named mydisk consisting of at least two PV requires that the PE size be 16MB, and then create a 5G logical volume in the volume group with the default name lvol0; mounted to the / users directory

Create a new user arc, requiring its home directory to be / users/arc, then su switch to arc user, copy the / etc/pam.d directory to your own home directory

Expand testlv to 7G, requiring archlinux users' files not to be lost

Shrink testlv to 3G, requiring archlinux users' files not to be lost

Create a snapshot of the testlv, and try to back up data based on the snapshot and verify the snapshot

First, add two 10G disks to the virtual machine, and then use the fdisk command to partition the disk and convert the LVM format.

1. First check the disk status through the lsblk command

[root@localhost ~] # lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 045G 0 disk ├─ sda1 8:1 0500M 0 part / boot └─ sda2 8:2 044.5G 0 part ├─ centos-root 253 sda2 040.6G 0 lvm / └─ centos-swap 253G0 lvm [SWAP] sdb 8:16 0 10G 0 disk sdc 8:32 0 10G 0 disk sr0 11:0 1 4G 0 rom 1234567892. Then use the fdisk command to edit the disk as required

[root@localhost] # fdisk / dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xce106979. Command (m for help): n Partition type: P primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-20971519, default 2048): Using default value 2048 Last sector, + sectors or + size {K Magee G} (2048-20971519) Default 20971519): Using default value 20971519 Partition 1 of type Linux and of size 10 GiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to' Linux LVM' Command (m for help): P Disk / dev/sdb: 10.7 GB, 10737418240 bytes 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I use O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xce106979 Device Boot Start End Blocks Id System / dev/sdb1 2048 20971519 10484736 8e Linux LVM12345678910111213141516171819202122232425262728293. Change the disk sdc to LVM format in the same way, and finally use the lsbkl command to view the partition

[root@localhost ~] # lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 045G 0 disk ├─ sda1 8:1 0500M 0 part / boot └─ sda2 8:2 044.5G 0 part ├─ centos-root 253 sda2 040.6G 0 lvm / └─ centos-swap 253G0 lvm [SWAP] sdb 8:16 0 10G 0 disk └─ sdb1 8:17 0 10G 0 part sdc 8:32 0 10G 0 disk └─ sdc1 8:33 0 10G 0 part sr0 11:0 1 4G 0 rom 1234567891011 create a pv with the pvcreate command And convert the sector to pe

1. Use the pvcreate command to create pv

[root@localhost ~] # pvcreate / dev/sdb1 / dev/sdc1 Physical volume "/ dev/sdb1" successfully created. Physical volume "/ dev/sdc1" successfully created.122. Use the pvs command to view

[root@localhost ~] # pvs PV VG Fmt Attr PSize PFree / dev/sda2 centos lvm2 Amuri-44.00m / dev/sdb1 lvm2-/ dev/sdc1 lvm2-create vg virtual disk using vgcreate command

1. Create a virtual disk using the vgcreate command

[root@localhost] # vgcreate mydisk-s 16m / dev/sdb1 / dev/sdc1 Volume group "mydisk" successfully created12. Use the vgs command to view the creation

[root@localhost ~] # vgs VG # PV # LV # SN Attr VSize VFree centos 120 wz--n- 44.00m mydisk 200 wz--n- 19.99g 19.99g123 next you need to use the lvcreate command to create a partition size of 5G

1. First use the lvcreate command to create the partition, and use the lvs command to view

[root@localhost] # lvcreate-L + 5G mydisk Logical volume "lvol0" created. [root@localhost ~] # lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos-wi-ao---- swap centos-wi-ao---- lvol0 mydisk-wi-a- 5.00g [root@localhost ~] # 1234567 partition needs to be formatted and mounted Use the mount command to mount it.

1. Mount the partition after creating the mount point users and creating the file system

[root@localhost ~] # mkdir users [root@localhost ~] # ls anaconda-ks.cfg users [root@localhost users] # mkfs.ext4 / dev/mydisk/lvol0 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks. Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@localhost ~] # mount / dev/mydisk/lvol0. / users/ [root@localhost ~] # ls anaconda-ks.cfg users [root@localhost ~] # cd users/ [root@localhost users] # ls lost+found12345678910111213141516171819202122 is followed by the creation of arc users

1. Create a user arc and specify its home directory. Because operating under root requires changing the permissions of the root directory, finally switch users to view and copy related files

[root@localhost] # chmod 777 / root [root@localhost ~] # useradd-d / root/users/arc arc [root@localhost ~] # cat / etc/passwd | grep arc arc:x:1003:1003::/root/users/arc:/bin/bash [root@localhost ~] # passwd arc Changing password for user arc. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. [root@localhost ~] # ls anaconda-ks.cfg users [root@localhost ~] # cd users/ [root@localhost users] # ls arc lost+found [root@localhost users] # su-arc [arc@localhost ~] $cp-a / etc/pam.d. / [arc@localhost ~] $ls pam.d12345678910111213141516171819202122 is followed by LVM partition expansion and contraction operation. The following is the operation flow

 extended logic volume: # lvextend-L [+] # [mMgGtT] / dev/VG_NAME/LV_NAME # resize2fs / dev/VG_NAME/LV_NAME # lvresize-r-l + 100%FREE / dev/VG_NAME/LV_NAME

 reduces logical volumes: umount/dev/VG_NAME/LV_NAME e2fsck-f / dev/VG_NAME/LV_NAME resize2fs / dev/VG_NAME/LV_NAME # [mMgGtT] lvreduce-L [-] # [mMgGtT] / dev/VG_NAME/LV_NAME mount

1. The operations to extend logical volumes are as follows

[root@localhost users] # lvextend-L + 2G / dev/mydisk/lvol0 Size of logical volume mydisk/lvol0 changed from 5.00 GiB (1280 extents) to 7.00 GiB (1792 extents). Logical volume mydisk/lvol0 successfully resized. [root@localhost users] # df-h Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0 / devtmpfs 1.9G 0 1.9G 0 0 / dev/shm tmpfs 1.9G 12M 1.9G 1% / run tmpfs 1.9G 0 1 .9G 0% / sys/fs/cgroup / dev/mapper/centos-root 41G 1.7G 39G 5% / / dev/sda1 497M 167M 331M 34% / boot tmpfs 378M 0378M 0% / run/user/0 / dev/mapper/mydisk-lvol0 4.8G 21M 4.6G 1% / root/users [root@localhost users] # resize2fs / dev/mydisk/lvol0 resize2fs 1. 42.9 (28-Dec-2013) Filesystem at / dev/mydisk/lvol0 is mounted on / root/users On-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on / dev/mydisk/lvol0 is now 1835008 blocks long. Root@localhost users] # df-h Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0% / devtmpfs 1.9G 0 1.9G 0% / dev/shm tmpfs 1.9G 12M 1.9G 1% / run tmpfs 1.9G 0 1. 9G 0% / sys/fs/cgroup / dev/mapper/centos-root 41G 1.7G 39G 5% / / dev/sda1 497M 167M 331M 34% / boot tmpfs 378M 0378M 0% / run/user/0 / dev/mapper/mydisk-lvol0 6.8G 23M 6.4G 1% / root/users12345678910111213141516171819202122232425262728 is the last snapshot operation The process is as follows

Create a snapshot # lvcreate-l 64-s-n snap-data-p r / dev/vg0/data mount snapshot # mkdir-p / mnt/snap # mount-o ro/dev/vg0/snap-data / mnt/snap restore snapshot # umount/dev/vg0/snap-data # umount/dev/vg0/data # lvconvert-merge / dev/vg0/snap-data delete snapshot # umount/mnt/databackup # lvremove/dev/vg0/databackup

1. Take the arc user you just created as an example to create a snapshot for it

[root@localhost users] # lvcreate-l 64-s-n arc-snap-p rw / dev/mydisk/lvol0 Logical volume "arc-snap" created.12. Next, we can switch arc users to do various operations.

[root@localhost users] # su-arc Last login: Mon Mar 30 22:11:03 CST 2020 on pts/0 [arc@localhost ~] $ls pam.d [arc@localhost] $echo jahfkjahgsfj > 1 [arc@localhost ~] $ls 1 pam.d1234563. Now we need to mount the snapshot and check the snapshot.

[root@localhost] # mkdir-p / arc/snap [root@localhost ~] # mount-o rw / dev/mydisk/arc-snap / arc/snap/ [root@localhost ~] # cd / arc/snap/ [root@localhost snap] # ls arc lost+found [root@localhost snap] # cd arc [root@localhost arc] # ls pam.d [root@localhost arc] # 123456784. Finally, we need to get back to where we were before the snapshot.

[root@localhost arc] # cd ~ [root@localhost ~] # umount / dev/mydisk/arc-snap [root@localhost ~] # umount / dev/mydisk/lvol0 [root@localhost ~] # lvconvert-merge / dev/mydisk/arc-snap Merging of volume mydisk/arc-snap started. Mydisk/lvol0: Merged: 89.02% mydisk/lvol0: Merged: 100.00% [root@localhost] # 12345675. Now let's log in to arc users to check the situation.

[root@localhost ~] # mount / dev/mydisk/lvol0. / users/ [root@localhost ~] # su-arc Last login: Mon Mar 30 22:34:55 CST 2020 on pts/0 [arc@localhost ~] $ls pam.d [arc@localhost ~] $so far, I believe you have a deeper understanding of "how to create and manage LVM logical volumes in Linux". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report