Logical volume management lvm and logical volume lvm

Source: Internet
Author: User

Logical volume management lvm and logical volume lvm

Logical volume management LVM

1. Create a logical volume

1. Prepare the partition or hard disk

Here we use two hard disks:/dev/sdb and/dev/sdc, And/dev/sda9 and/dev/sda10. the disk size is 1 GB and the disk size is limited, I don't want to do that either.

Add partition/dev/sda9,/dev/sda10

[root@centos7 ~]# fdisk /dev/sdaWelcome 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.Command (m for help): nAll primary partitions are in useAdding logical partition 9First sector (31885312-41943039, default 31885312):Using default value 31885312Last sector, +sectors or +size{K,M,G} (31885312-41943039, default 41943039): +1GPartition 9 of type Linux and of size 1 GiB is set

Note: You must change the partition type to Linux LVM.

Command (m for help): tPartition number (1-9, default 9): 8ePartition number (1-9, default 9): 9Hex code (type L to list all codes): 8eChanged type of partition 'Linux' to 'Linux LVM'Command (m for help):

Create/dev/sda10 in the same way.

[root@centos7 ~]# lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 8:0 0 20G 0 disk├─sda9 8:9 0 1G 0 part└─sda10 8:10 0 1G 0 partsdb 8:16 0 1G 0 disksdc 8:32 0 1G 0 disk

2. Create a PV (physical volume)

Command Format: pvcreate DEVICE. You can create one DEVICE at a time or multiple devices at a time.

Command for viewing physical volumes: pvdisplay (View Details) and pvs (view simple information)

[root@centos7 ~]# pvcreate /dev/sda9Physical volume "/dev/sda9" successfully created.[root@centos7 ~]# pvcreate /dev/sda10 /dev/sdb /dev/sdcPhysical volume "/dev/sda10" successfully created.Physical volume "/dev/sdb" successfully created.Physical volume "/dev/sdc" successfully created.

Check the physical volume.

[root@centos7 ~]# pvsPV VG Fmt Attr PSize PFree/dev/sda10 lvm2 --- 1.00g 1.00g/dev/sda9 lvm2 --- 1.00g 1.00g/dev/sdb lvm2 --- 1.00g 1.00g/dev/sdc lvm2 --- 1.00g 1.00g

3. Create a volume group (VG)

Command Format: vgcreate-s pe vgname (vg name) DEVICE (DEVICE name)

-S: Specifies the physical expansion block size, which is the PE size.

Command: vgs, vgdisplay

[root@centos7 ~]# vgcreate -s 16M vg0 /dev/sda9 /dev/sda10 /dev/sdb /dev/sdc
Volume group "vg0" successfully created

View

[root@centos7 ~]# vgsVG #PV #LV #SN Attr VSize VFreevg0 4 0 0 wz--n- 3.94g 3.94g

Let's take a look at the details.

[root@centos7 ~]# vgdisplay--- Volume group ---VG Name vg0System IDFormat lvm2Metadata Areas 4Metadata Sequence No 1VG Access read/writeVG Status resizableMAX LV 0Cur LV 0Open LV 0Max PV 0Cur PV 4Act PV 4VG Size 3.94 GiBPE Size 16.00 MiBTotal PE 252Alloc PE / Size 0 / 0Free PE / Size 252 / 3.94 GiBVG UUID qmoIMg-5wQR-GKCS-dpq9-HLrt-zVIi-JBC9ZX

Everything is expected to be combined. The size is about 4 GB, which is exactly the sum of two partitions and two hard disks.

4. Create a logical volume (LV)

Command Format: lvcreate-n LVNAME (specify the LV name)-l size (specify the lv size) vg0 (the VG to be used)

-L specify the PE size

View commands (such as PV and VG): lvs and lvdisplay

[root@centos7 ~]# lvcreate -n lv0 -L 3G vg0Logical volume "lv0" created.

Created successfully. Check it.

[root@centos7 ~]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertlv0 vg0 -wi-a----- 3.00g

Let's take a look at the details.

[root@centos7 ~]# lvdisplay--- Logical volume ---LV Path /dev/vg0/lv0LV Name lv0VG Name vg0LV UUID 622i7m-uy6s-3nZI-8xSb-sxrR-cZzz-C9pwkdLV Write Access read/writeLV Creation host, time centos7.3.loacl, 2017-04-25 16:25:13 +0800LV Status available# open 0LV Size 3.00 GiBCurrent LE 192Segments 4Allocation inheritRead ahead sectors auto- currently set to 8192Block device 253:0

You can confirm that the instance has been successfully created.

Now you can use/dev/vg0/lv0 as a hard disk.

Now let's take a look at the disk information, which has changed.

[root@centos7 ~]# lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTfd0 2:0 1 4K 0 disksda 8:0 0 20G 0 disk├─sda9 8:9 0 1G 0 part│ └─vg0-lv0 253:0 0 3G 0 lvm└─sda10 8:10 0 1G 0 part└─vg0-lv0 253:0 0 3G 0 lvmsdb 8:16 0 1G 0 disk└─vg0-lv0 253:0 0 3G 0 lvmsdc 8:32 0 1G 0 disk└─vg0-lv0 253:0 0 3G 0 lvm

As you can see, the added partition and hard disk are already in vg0-lv0.

5. Create and format a file system.

mkfs.ext4 /dev/vg0/lv0[root@centos7 ~]# mkfs.ext4 /dev/vg0/lv0

6. Mount and use

[root@centos7 ~]# mkdir /mnt/lv0[root@centos7 ~]# mount /dev/vg0/lv0 /mnt/lv0

 

2. Extended LV

 

Extended command format:

lvextend -L 100G /dev/vg0/lv0lvextend -l +100G /dev/vg0/lv0lvextend -l +100%FREE /dev/vg0/lv0

Note that "-L" is the number of extensions, "-l" is the number of extensions (Increase), and "+ 100% FREE" is the 100% of the remaining vg extensions, which can be selected as needed.

Because vg0 still reserves nearly 1 GB of space, now we can extend lv0.

1 first check the original LV

[root@centos7 ~]# vgsVG #PV #LV #SN Attr VSize VFreevg0 4 1 0 wz--n- 3.94g 960.00m

2. LV Extension

[root@centos7 ~]# lvextend -r -l +100%FREE /dev/vg0/lv0

-R is convenient for resizefs.

Or a little bit of trouble, step by step

[root@centos7 ~]# lvextend -l +100%FREE /dev/vg0/lv0Size of logical volume vg0/lv0 changed from 3.00 GiB (192 extents) to 3.94 GiB (252 extents).Logical volume vg0/lv0 successfully resized.

3. Redefine the file system size

Ext File System redefinition: resize2fs/dev/vg0/lv0

Xfs File System redefinition: xfs_growfs/dev/vg0/lv0

[root@centos7 ~]# resize2fs /dev/vg0/lv0resize2fs 1.42.9 (28-Dec-2013)Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing requiredold_desc_blocks = 1, new_desc_blocks = 1The filesystem on /dev/vg0/lv0 is now 1032192 blocks long.

4. Check file system completion (or not)

[root@centos7 ~]# fsck.ext4 /dev/vg0/lv0e2fsck 1.42.9 (28-Dec-2013)/dev/vg0/lv0: clean, 20/262144 files, 35918/1032192 blocks

5. Confirm the logical volume size

[root@centos7 ~]# df -h /dev/vg0/lv0Filesystem Size Used Avail Use% Mounted on/dev/mapper/vg0-lv0 3.9G 12M 3.6G 1% /mnt/lv0

Obviously, the extension is successful.

6. Mount and use

[root@centos7 ~]# mount -o remount /dev/vg0/lv0

 

3. LV reduction

 

1. Back up data

Although data will not be lost in general, it is for the sake of insurance. Back up data first.

Check the existing data in the/mnt/lv0 directory.

[root@centos7 ~]# ls /mnt/lv0/lost+found myfile2 myfile4 myfile6 myfile8myfile1 myfile3 myfile5 myfile7 myfile9[root@centos7 ~]# mkdir /backup[root@centos7 ~]# cp -a /mnt/lv0/* /backup/

2 uninstall/dev/vg0/lv0

[root@centos7 ~]# umount /mnt/lv0/

3. Check the file system

E2fsck-f/dev/vg0/lv0 check disk completion before redefining the file system size

[root@centos7 ~]# e2fsck -f /dev/vg0/lv0e2fsck 1.42.9 (28-Dec-2013)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary information/dev/vg0/lv0: 20/262144 files (0.0% non-contiguous), 35918/1032192 blocks

4. Redefine the file system size

Resize2fs/dev/vg0/lv0 2G # The LV size after 2G bit reduction.

[root@centos7 ~]# resize2fs /dev/vg0/lv0 2Gresize2fs 1.42.9 (28-Dec-2013)Resizing the filesystem on /dev/vg0/lv0 to 524288 (4k) blocks.The filesystem on /dev/vg0/lv0 is now 524288 blocks long.

5. LV reduction

Lvreduce-L 2G/dev/vg0/lv0 # scale down to 2G

[root@centos7 ~]# lvreduce -L 2G /dev/vg0/lv0WARNING: Reducing active logical volume to 2.00 GiB.THIS MAY DESTROY YOUR DATA (filesystem etc.)Do you really want to reduce vg0/lv0? [y/n]: ySize of logical volume vg0/lv0 changed from 3.94 GiB (252 extents) to 2.00 GiB (128 extents).Logical volume vg0/lv0 successfully resized.

6. Mount and use

mount /dev/vg0/lv0 /mnt/lv0[root@centos7 ~]# mount /dev/vg0/lv0 /mnt/lv0/

View disk mounting information

[root@centos7 ~]# df -h /dev/vg0/lv0Filesystem Size Used Avail Use% Mounted on/dev/mapper/vg0-lv0 2.0G 9.0M 1.8G 1% /mnt/lv0

7. Confirm data loss

Now let's take a look at the/mnt/lv0 contents.

[root@centos7 ~]# ls /mnt/lv0/lost+found myfile2 myfile4 myfile6 myfile8myfile1 myfile3 myfile5 myfile7 myfile9

 

Perfect, data is not lost!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.