Dynamically adjust disk capacity through lvm in Linux

Source: Internet
Author: User
LVM: logicalvolumemanager (Logical Disk Management). lvm shields the underlying disk layout and facilitates dynamic disk capacity adjustment. Step 1, use the fdisk tool to convert a disk to partition 2 in linux, use the pvcreate command to convert a linux partition to a physical volume (pv) 3, and use the vgcreate command to process the created physical volume into a paper (

LVM: logical volume manager (logical Disk Management). lvm shields the underlying disk layout and facilitates dynamic adjustment of disk capacity.

1. creation steps:

1. use the fdisk tool to convert a disk to a linux partition.

2. use the pvcreate command to convert a linux partition to a physical volume (pv)

3. use the vgcreate command to process the created physical volume into a volume (vg)

4. use the lvcreate command to group the volume into several logical volumes (lv)

5. Format, mount, and dynamically adjust the logical volume without affecting the data of the logical volume.

II. Procedure

View linux partition fdisk-l and convert unused space to linux physical volume

[Root @ rh ~] # Fdisk/dev/sdo

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. Changes will remain in memoryonly,

Until you decide to write them. After that, of course, theprevious

Content won't be recoverable.

The number of cylinders for this disk is set to 2610.

There is nothing wrong with that, but this is larger than1024,

And coshould in certain setups cause problems:

1) software that runs at boot time (e.g., old versions ofLILO)

2) booting and partitioning software from other OSs

(E.g., dos fdisk, OS/2 FDISK)

Warning: invalid flag 0x0000 of partition table 4 will becorrected by w (rite)

Command (m for help): n

Command action

E extended

P primary partition (1-4)

P

Partition number (1-4): 1

First cylinder (1-2610, default 1 ):

Using default value 1

Last cylinder or + size or + sizeM or + sizeK (1-2610, default2610): 500 m

Command (m for help): n

Command action

E extended

P primary partition (1-4)

P

Partition number (1-4): 2

First cylinder (501-2610, default 501 ):

Using default value 501

Last cylinder or + size or + sizeM or + sizeK (501-2610, default2610): 500 m

Value out of range.

Last cylinder or + size or + sizeM or + sizeK (501-2610, default2610): 200 m

Value out of range.

Last cylinder or + size or + sizeM or + sizeK (501-2610, default2610 ):

Using default value 2610

Command (m for help): w

The partition table has been altered!

Calling ioctl () to re-read partition table.

Syncing disks.

-- View linux partition information

[Root @ rh ~] # Fdisk-l

....

Device Boot Start End Blocks Id System

/Dev/sdn1 1 2610 20964793 + 83 Linux

Disk/dev/sdo: 21.4 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065*512 = 8225280 bytes

Device Boot Start End Blocks Id System

/Dev/sdo1 1 500 4016218 + 83 Linux

/Dev/sdo2 501 2610 16948575 83 Linux

[Root @ rh ~] # Pvcreate/dev/sdo

Sdo sdo1 sdo2

Convert a linux physical partition to a physical volume

Convert physical partition/dev/sdo {1, 2} to physical volume

[Root @ rh ~] # Pvcreate/dev/sdo {1, 2}

Physical volume "/dev/sdo1" successfully created

Physical volume "/dev/sdo2" successfully created

Use pvscan to view physical volume information

-- View the physical volume information. all physical information is displayed.

[Root @ rh ~] # Pvscan

PV/dev/sdo1 lvm2 [3.83 GB]

PV/dev/sdo2 lvm2 [16.16 GB]

Total: 2 [19.99 GB]/in use: 0 [0]/in no VG: 2 [19.99 GB]

Use pvdisplay to view detailed physical volume parameters

-- View the details of each physical volume

[Root @ rh ~] # Pvdisplay

"/Dev/sdo1" is a new physical volume of "3.83 GB"

--- NEW Physical volume ---

PV Name/dev/sdo1

VG Name

PVs Size 3.83 GB

Allocatable NO

PE Size (KByte) 0

Total PE 0

Free PE 0

Allocated PE 0

Pv uuid 8Kq0Ct-q0Hh-KKK5-Ooh2-0U3w-BDov-2oLr39

"/Dev/sdo2" is a new physical volume of "16.16 GB"

--- NEW Physical volume ---

PV Name/dev/sdo2

VG Name

PVs Size 16.16 GB

Allocatable NO

PE Size (KByte) 0

Total PE 0

Free PE 0

Allocated PE 0

Pvuuid z2kGU1-wI2g-7N2B-EnLd-bVJP-hnqV-v9rpvF

When the physical volume is not used, we can delete the physical volume.

-- Use pvremove/dev/sd01 to delete a physical volume

3. Volume Group (vg) converts a physical volume to a volume Group

-- Convert/dev/sd0 [1, 2} of a physical volume to a volume group named vg01

[Root @ rh ~] # Vgcreate vg01/dev/sdo

Sdo sdo1 sdo2

[Root @ rh ~] # Vgcreate vg01/dev/sdo {1, 2}

Volume group "vg01" successfully created

[Root @ rh ~] # Vgdisplay

--- Volume group ---

VG Name vg01

System ID

Format lvm2

Metadata Areas 2

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 2

Act PV 2

VG Size 19.99 GB

PE Size 4.00 MB

Total PE 5117

Alloc PE/Size 0/0

Free PE/Size 5117/19 .99 GB

Vg uuid 8am3ag-cmb5-ol3s-z2w3-ow1_l087-tmaz1r

If the above parameter is not added, the default size of the extended block pe is 4 m. if you use vgcreate-s 8 m vg01/dev/sd0 {1, 2}, the extended block size is 8 m.

Use vgdisplay to view detailed information about a volume Group

-- View information of all physical volumes

[Root @ rh ~] # Vgdisplay

--- Volume group ---

VG Name vg01

System ID

Format lvm2

Metadata Areas 4

Metadata Sequence No 11

VG Access read/write

VG Status resizable

Max lv 0

Cur LV 1

Open LV 1

Max PV 0

Cur PV 4

Act PV 4

VG Size 59.97 GB

PE Size 4.00 MB

Total PE 15353

Alloc PE/Size 1667/6 .51 GB

Free PE/Size 13686/53 .46 GB

Vg uuid 8am3ag-cmb5-ol3s-z2w3-ow1_l087-tmaz1r

You can also use vgdisplay-v/dev/vg01 to view the details of a specific volume group.

Use vgscan to view the volume group information

-- View the volume group information

[Root @ rh ~] # Vgscan

Reading all physical volumes. This may take a while...

Found volume group "vg01" using metadata type lvm2

[Root @ rh ~] # Pvcreate/dev/sd

Sda sdb sdc1 sde sdf1 sdh sdk sdm sdn1 sdo2

Sda1 sdb1 sdd sde1 sdg sdi sdm1 sdo

Sda2 sdc sdd1 sdf sdg1 sdj sdl1 sdn sdo1

Extend the volume group vgextend to add a physical volume to an existing volume Group

Create a new physical volume

[Root @ rh ~] # Pvcreate/dev/sdc1

Wiping software RAID md superblock on/dev/sdc1

Physical volume "/dev/sdc1" successfully created

-- Add a new physical volume to the vg01 volume group.

[Root @ rh ~] # Vgextend vg01/dev/sdc1

Volume group "vg01" successfully extended

IV. create and manage logical volume lv

Dividing 6 GB space from vg01

[Root @ rh ~] # Lvcreate-L 6g-n data vg01

Logical volume "data" created

Format logical volumes

[Root @ rh ~] # Mkfs-t ext3/dev/vg01/data

Mke2fs 1.39 (29-may-2006)

Filesystem label =

OS type: Linux

Block size = 4096 (log = 2)

Fragment size = 4096 (log = 2)

786432 inodes, 1572864 blocks

78643 blocks (5.00%) reserved for the super user

First data block = 0

Maximum filesystem blocks = 1610612736

48 block groups

32768 blocks per group, 32768 fragments per group

16384 inodes per group

Superblock backups stored on blocks:

32768,983 04, 163840,229 376, 294912,819 200, 884736

Writing inode tables: done

Creating journal (32768 blocks ):

Done

Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 32 mountsor

180 days, whichever comes first. Use tune2fs-c or-I tooverride.

You can also format it using mkfs. ext3/dev/vg01/data.

UseLvsCan View logical volume information

[Root @ rh ~] # Lvscan

ACTIVE '/dev/vg01/data' [6.00 GB] inherit

Use lvdisplay to view logical volume parameters

[Root @ rh ~] # Lvdisplay

--- Logical volume ---

LV Name/dev/vg01/data

VG Name vg01

Lvuuid KwarPL-APks-8VhN-hSVB-WgVG-m0EP-6H6ifd

LV Write Access read/write

LV Status available

# Open 0

LV Size 6.00 GB

Current LE 1536

Segments 1

Allocation inherit

Read ahead sectors auto

-Currently sets to 256

Block device 253: 0

You can also use lvdisplay-v/dev/vg01/data to view the details of a logical volume.

To delete a volume group, you can use

Vgremove/dev/vg01

Use lvextend to increase the logical volume size and resize it online

Expand the logical volume from the volume group vg01 and change the logical volume size to 7 GB.

[Root @ rh ~] # Lvextend-L + 1g/dev/vg01/data

Extending logical volume data to 7.00 GB

Logical volume data successfully resized

[Root @ rh ~] # Lvscan

ACTIVE '/dev/vg01/data' [7.00 GB] inherit

Use resize2fs to update the file system size recognized by the system. this takes effect immediately.

Yes. the added logical volume takes effect immediately.

[Root @ rh ~] # Resize2fs/dev/vg01/data

Resize2fs 1.39 (29-May-2006)

Resizing the filesystem on/dev/vg01/data to 1835008 (4 k) blocks.

The filesystem on/dev/vg01/data is now 1835008 blocks long.

To use lvreduce to reduce the logical volume size, you must use an offline method --> uninstall the file system first.

[Root @ rh ~] # Lvreduce-L-500 m/dev/vg01/data

WARNING: switching active logical volume to 6.51 GB

This may destroy your data (filesystem etc .)

Do you really want to reduce data? [Y/n]: y

Elastic Cing logical volume data to 6.51 GB

Logical volume data successfully resized

[Root @ rh ~] # Lvscan

ACTIVE '/dev/vg01/data' [6.51 GB] inherit

Make the reduced logical volume take effect immediately

To shrink a logical volume, you must uninstall the file system first, and the reduced space must be larger than the current capacity occupied by the file; otherwise, information is lost.

[Root @ rh ~] # Resize2fs/dev/vg01/data

Resize2fs 1.39 (29-May-2006)

Resizing the filesystem on/dev/vg01/data to 1707008 (4 k) blocks.

Resize2fs: Can't read an block bitmap while trying to resize/dev/vg01/data

View logical volume size

[Root @ rh ~] # Lvscan

ACTIVE '/dev/vg01/data' [6.51 GB] inherit

Delete logical volume

Lvremove/dev/vg01/data

5. Mount logical volumes

[Root @ rh ~] # Mkdir/quota

[Root @ rh ~] # Mount/dev/vg01/data/quota/

[Root @ rh ~] # Df-hT

File system type capacity available in use % mount point

/Dev/sda1 ext3 67G 20G 44G 32%/

Tmpfs 357 M 0 357 M 0%/dev/shm

/Dev/mapper/vg01-data

Ext3 6.9G 142 M 6.5G 3%/quota

Automatic mounting upon startup

[Root @ rh ~] # Vi/etc/fstab

LABEL = // ext3 defaults 1 1

Tmpfs/dev/shm tmpfs defaults 0 0

Devpts/dev/pts devpts gid = 5, mode = 620 0 0

Sysfs/sys sysfs defaults 0 0

Proc/proc defaults 0 0

LABEL = SWAP-sda2 swap defaults 0 0

/Dev/md0/raid ext3 defaults 0 0

/Dev/md11/traid ext3 defaults 0 0

/Dev/vg01/data/quota ext3 defaults 0 0

~

~

~

~

~

~

Entering Ex mode. Type "visual" to go to Normal mode.

: Wq!

"/Etc/fstab" 9L, 575C written

6. The Logical Volume Snapshot management function can freeze the data in the volume. similar to taking a photo of the data, you can permanently save and create the current status of the snapshot.

Create a Volume Snapshot. the snapshot name of the lvcreate-L raw logical volume is 15%-20%-s-n. the Source logical volume name

It is similar to creating a logical volume, but the parameter-s is added.

[Root @ rh ~] # Lvcreate-L 1g-s-n snaplv1/dev/vg01/data

Logical volume "snaplv1" created

The status is snapshot.

[Root @ rh ~] # Lvscan

ACTIVE Original '/dev/vg01/data' [6.51 GB] inherit

ACTIVE Snapshot '/dev/vg01/snaplv1' [1.00 GB] inherit

Create a mount point and mount it

[Root @ rh ~] # Mkdir/snap

[Root @ rh ~] # Mount/dev/vg01/snaplv1/snap/

[Root @ rh ~] # Lvremove/dev/vg01/snaplv1

Can't remove open logical volume "snaplv1"

Detach a snapshot

[Root @ rh ~] # Umount/snap/

Delete a snapshot

[Root @ rh ~] # Lvremove/dev/vg01/snaplv1

Do you really want to remove active logical volume "snaplv1 "? [Y/n]: y

Logical volume "snaplv1" successfully removed

7. If an error occurs in the partition of the physical hard disk in one day, the hard disk must be replaced. lvm provides the pvmove tool to transfer data from one physical volume to another.

Load a new physical volume to the volume Group

[Root @ rh ~] # Pvcreate/dev/sdb1

Wiping software RAID md superblock on/dev/sdb1

Physical volume "/dev/sdb1" successfully created

Add the new physical volume to the vg01 volume group.

[Root @ rh ~] # Vgextend vg01/dev/sdb1

Volume group "vg01" successfully extended

[Root @ rh ~] # Lvscan

ACTIVE '/dev/vg01/data' [6.51 GB] inherit

Move data on a physical volume to a newly added physical volume

[Root @ rh ~] # Pvmove/dev/sdb1/dev/sdo1

No data to move for vg01

Use vgreduce to detach the damaged disk/dev/sdb1 from the volume Group

Vgreduce vg01/dev/sdb1

[Root @ rh ~] # Pvscan

PV/dev/sdo1 VG vg01 lvm2 [3.83 GB/3.83 GB free]

PV/dev/sdo2 VG vg01 lvm2 [16.16 GB/16.16 GB free]

PV/dev/sdc1 VG vg01 lvm2 [19.99 GB/13.48 GB free]

PV/dev/sdb1 VG vg01 lvm2 [19.99 GB/19.99 GB free]

Total: 4 [59.97 GB]/in use: 4 [59.97 GB]/in no VG: 0 [0]

If you want to migrate the entire lvm disk to another computer

Unmount all logical volumes in the volume Group

[Root @ rh ~] # Umount/dev/vg01/data

Use vgchange to change the volume group to an invalid configuration.

[Root @ rh ~] # Vgchange-a n vg01

Use vgexport to export the volume Group

0 logical volume (s) in volume group "vg01" now active

[Root @ rh ~] # Vgexport vg01

Volume group "vg01" successfully exported

Install the lvm disk on the target computer

Use pvscan to scan all physical volumes so that linux can drive these physical volumes

[Root @ rh ~] # Pvscan

PV/dev/sdo1 is in exported VG vg01 [3.83 GB/3.83 GB free]

PV/dev/sdo2 is in exported VG vg01 [16.16 GB/16.16 GBfree]

PV/dev/sdc1 is in exported VG vg01 [19.99 GB/13.48 GBfree]

PV/dev/sdb1 is in exported VG vg01 [19.99 GB/19.99 GBfree]

Total: 4 [59.97 GB]/in use: 4 [59.97 GB]/in no VG: 0 [0]

Import volume Group

[[Root @ rh ~] # Vgimport vg01

Volume group "vg01" successfully imported

Modify the volume group to a valid configuration

[Root @ rh ~] # Vgchange-a y vg01

1 logical volume (s) in volume group "vg01" now active

Mount a logical volume

[Root @ rh ~] # Mount/dev/vg01/data/quota/

View disk mounting information

Df-hT

10. logical volume creation and deletion recommendation steps

Sequence of creating logical volume lv: linux partition ----> physical volume pv ----> Volume group vg ----> logical volume lv -----> mount to file system

Sequence of logical volume deletion: unmount the file system ----> logical volume lv ----> Volume group vg ----> physical volume pv -----> linux partition

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.