lvm拉伸与快照

时间:2024-12-09 23:08:14

一.拉伸

*用fdisk分区

*构建pv

*将pv加入vg

*将pv内的pe加入lv

*通过resize将文件系统的容量增加

1.分区

[root@server3 ~]# fdisk /dev/vdb

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.

Command (m for help): n

All primary partitions are in use

Adding logical partition 6

First sector (8394752-20971519, default 8394752):

Using default value 8394752

Last sector, +sectors or +size{K,M,G} (8394752-20971519, default 20971519): +1G

Partition 6 of type Linux and of size 1 GiB is set

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0xa859763d

Device Boot      Start         End      Blocks   Id  System

/dev/vdb1            2048     2099199     1048576   8e  Linux LVM

/dev/vdb2         2099200     4196351     1048576   8e  Linux LVM

/dev/vdb3         4196352     6293503     1048576   8e  Linux LVM

/dev/vdb4         6293504    20971519     7339008    5  Extended

/dev/vdb5         6295552     8392703     1048576   8e  Linux LVM

/dev/vdb6         8394752    10491903     1048576   83  Linux

Command (m for help): t

Partition number (1-6, default 6): 6

Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0xa859763d

Device Boot      Start         End      Blocks   Id  System

/dev/vdb1            2048     2099199     1048576   8e  Linux LVM

/dev/vdb2         2099200     4196351     1048576   8e  Linux LVM

/dev/vdb3         4196352     6293503     1048576   8e  Linux LVM

/dev/vdb4         6293504    20971519     7339008    5  Extended

/dev/vdb5         6295552     8392703     1048576   8e  Linux LVM

/dev/vdb6         8394752    10491903     1048576   8e  Linux LVM

2.构建pv

[root@server3 ~]# pvcreate /dev/vdb6

Physical volume "/dev/vdb6" successfully created.

[root@server3 ~]# pvscan

PV /dev/vdb1   VG vgn             lvm2 [1008.00 MiB / 0    free]

PV /dev/vdb2   VG vgn             lvm2 [1008.00 MiB / 0    free]

PV /dev/vdb3   VG vgn             lvm2 [1008.00 MiB / 0    free]

PV /dev/vdb5   VG vgn             lvm2 [1008.00 MiB / 0    free]

PV /dev/vdb6                      lvm2 [1.00 GiB]

Total: 5 [4.94 GiB] / in use: 4 [3.94 GiB] / in no VG: 1 [1.00 GiB]

3.将pv加入vg

[root@server3 ~]# vgextend vgn /dev/vdb6

Volume group "vgn" successfully extended

[root@server3 ~]# vgdisplay

--- Volume group ---

VG Name               vgn

System ID

Format                lvm2

Metadata Areas        5

Metadata Sequence No  4

VG Access             read/write

VG Status             resizable

MAX LV                0

Cur LV                1

Open LV               1

Max PV                0

Cur PV                5

Act PV                5

VG Size               4.92 GiB

PE Size               16.00 MiB

Total PE              315

Alloc PE / Size       252 / 3.94 GiB

Free  PE / Size       63 / 1008.00 MiB  **这里显示的时未使用的pe,-l参数可以看这里

VG UUID               JjHOlH-RdcV-71dR-4wmK-NQ3N-pnRS-1y5dIV

4.将pv内的pe加入lv

[root@server3 ~]# lvresize -l +63 /dev/vdb

/dev/vdb1  /dev/vdb2  /dev/vdb3  /dev/vdb5  /dev/vdb6

[root@server3 ~]# lvresize -l +63 /dev/vgn/lvn

Size of logical volume vgn/lvn changed from 3.94 GiB (252 extents) to 4.92 GiB (315 extents).

Logical volume vgn/lvn successfully resized.

[root@server3 ~]# lvdisplay

--- Logical volume ---

LV Path                /dev/vgn/lvn

LV Name                lvn

VG Name                vgn

LV UUID                3cZBXM-Ef2z-wDLR-Fyxx-TmzD-Payq-BJ6eTm

LV Write Access        read/write

LV Creation host, time server3, 2019-01-21 20:32:58 +0800

LV Status              available

# open                 1

LV Size                4.92 GiB

Current LE             315

Segments               5

Allocation             inherit

Read ahead sectors     auto

- currently set to     8192

Block device           252:0

5.文件系统的扩展

[root@server3 ~]# df -h

/dev/mapper/vgn-lvn  3.9G   16M  3.6G   1% /mnt/lvm

[root@server3 ~]# resize2fs  /dev/vgn/lvn

resize2fs 1.42.9 (28-Dec-2013)

Filesystem at /dev/vgn/lvn is mounted on /mnt/lvm; on-line resizing required

old_desc_blocks = 1, new_desc_blocks = 1

The filesystem on /dev/vgn/lvn is now 1290240 blocks long.

[root@server3 ~]# df -h /dev/vgn/lvn

Filesystem           Size  Used Avail Use% Mounted on

/dev/mapper/vgn-lvn  4.8G   16M  4.6G   1% /mnt/lvm

二.缩小LV容量

[root@server3 ~]# pvscan

PV /dev/vdb1   VG vgn             lvm2 [1008.00 MiB / 0    free]

PV /dev/vdb2   VG vgn             lvm2 [1008.00 MiB / 0    free]

PV /dev/vdb3   VG vgn             lvm2 [1008.00 MiB / 0    free]

PV /dev/vdb5   VG vgn             lvm2 [1008.00 MiB / 0    free]

PV /dev/vdb6   VG vgn             lvm2 [1008.00 MiB / 0    free]

Total: 5 [4.92 GiB] / in use: 5 [4.92 GiB] / in no VG: 0 [0   ]

[root@server3 ~]# umount /dev/vgn/lvn

[root@server3 ~]# resize2fs /dev/vgn/lvn  4032M

resize2fs 1.42.9 (28-Dec-2013)

Please run 'e2fsck -f /dev/vgn/lvn' first.

[root@server3 ~]# e2fsck -f /dev/vgn/lvn

e2fsck 1.42.9 (28-Dec-2013)

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 4: Checking reference counts

Pass 5: Checking group summary information

/dev/vgn/lvn: 11/322560 files (0.0% non-contiguous), 40670/1290240 blocks

[root@server3 ~]# resize2fs /dev/vgn/lvn  4032M

resize2fs 1.42.9 (28-Dec-2013)

Resizing the filesystem on /dev/vgn/lvn to 1032192 (4k) blocks.

The filesystem on /dev/vgn/lvn is now 1032192 blocks long.

[root@server3 ~]# mount /dev/vgn/lvn /mnt/lvm

[root@server3 ~]# df -h /mnt/lvm/

Filesystem           Size  Used Avail Use% Mounted on

/dev/mapper/vgn-lvn  3.9G   16M  3.6G   1% /mnt/lvm

可以通过pvdisplay查看pe个数

[root@server3 ~]# lvresize -l 63 /dev/vgn/lvn

WARNING: Reducing active and open logical volume to 1008.00 MiB.

THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce vgn/lvn? [y/n]: y

Size of logical volume vgn/lvn changed from 4.92 GiB (315 extents) to 1008.00 MiB (63 extents).

Logical volume vgn/lvn successfully resized.

[root@server3 ~]# pvscan

PV /dev/vdb1   VG vgn             lvm2 [1008.00 MiB / 0    free]

PV /dev/vdb2   VG vgn             lvm2 [1008.00 MiB / 1008.00 MiB free]

PV /dev/vdb3   VG vgn             lvm2 [1008.00 MiB / 1008.00 MiB free]

PV /dev/vdb5   VG vgn             lvm2 [1008.00 MiB / 1008.00 MiB free]

PV /dev/vdb6   VG vgn             lvm2 [1008.00 MiB / 1008.00 MiB free]

Total: 5 [4.92 GiB] / in use: 5 [4.92 GiB] / in no VG: 0 [0   ]

[root@server3 ~]# pvmove /dev/vdb1 /dev/vdb6

/dev/vdb1: Moved: 7.94%

/dev/vdb1: Moved: 100.00%

[root@server3 ~]# vgreduce vgn /dev/vdb1

Removed "/dev/vdb1" from volume group "vgn"

[root@server3 ~]# pvscan

PV /dev/vdb2   VG vgn             lvm2 [1008.00 MiB / 1008.00 MiB free]

PV /dev/vdb3   VG vgn             lvm2 [1008.00 MiB / 1008.00 MiB free]

PV /dev/vdb5   VG vgn             lvm2 [1008.00 MiB / 1008.00 MiB free]

PV /dev/vdb6   VG vgn             lvm2 [1008.00 MiB / 0    free]

PV /dev/vdb1                      lvm2 [1.00 GiB]

Total: 5 [4.94 GiB] / in use: 4 [3.94 GiB] / in no VG: 1 [1.00 GiB]

[root@server3 ~]# pvremove /dev/vdb1

Labels on physical volume "/dev/vdb1" successfully wiped.

[root@server3 ~]# pvscan

PV /dev/vdb2   VG vgn             lvm2 [1008.00 MiB / 1008.00 MiB free]

PV /dev/vdb3   VG vgn             lvm2 [1008.00 MiB / 1008.00 MiB free]

PV /dev/vdb5   VG vgn             lvm2 [1008.00 MiB / 1008.00 MiB free]

PV /dev/vdb6   VG vgn             lvm2 [1008.00 MiB / 0    free]

Total: 4 [3.94 GiB] / in use: 4 [3.94 GiB] / in no VG: 0 [0   ]

三.lvm系统快照

1.快照区的创建

[root@server3 ~]# vgdisplay

--- Volume group ---

VG Name               vgn

System ID

Format                lvm2

Metadata Areas        4

Metadata Sequence No  10

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               3.94 GiB

PE Size               16.00 MiB

Total PE              252

Alloc PE / Size       63 / 1008.00 MiB

Free  PE / Size       189 / 2.95 GiB

VG UUID               JjHOlH-RdcV-71dR-4wmK-NQ3N-pnRS-1y5dIV

[root@server3 ~]# lvcreate -l 80 -s -n vv /dev/vgn/lvn

Using default stripesize 64.00 KiB.

Reducing COW size 1.25 GiB down to maximum usable size 1.00 GiB.

Logical volume "vv" created.

[root@server3 ~]# lvdisplay

--- Logical volume ---

LV Path                /dev/vgn/lvn

LV Name                lvn

VG Name                vgn

LV UUID                3cZBXM-Ef2z-wDLR-Fyxx-TmzD-Payq-BJ6eTm

LV Write Access        read/write

LV Creation host, time server3, 2019-01-21 20:32:58 +0800

LV snapshot status     source of

vv [active]

LV Status              available

# open                 1

LV Size                1008.00 MiB

Current LE             63

Segments               1

Allocation             inherit

Read ahead sectors     auto

- currently set to     8192

Block device           252:0

--- Logical volume ---

LV Path                /dev/vgn/vv

LV Name                vv

VG Name                vgn

LV UUID                az5JDg-Njjn-c51c-qdzc-ItEl-pLme-9RHnA7

LV Write Access        read/write

LV Creation host, time server3, 2019-01-22 17:40:43 +0800

LV snapshot status     active destination for lvn

LV Status              available

# open                 0

LV Size                1008.00 MiB

Current LE             63

COW-table size         1.00 GiB **快照区的实际容量

COW-table LE           64      **快照区占用的PE数量

Allocated to snapshot  0.00%

Snapshot chunk size    4.00 KiB

Segments               2

Allocation             inherit

Read ahead sectors     auto

- currently set to     8192

Block device           252:3

挂载查看一下,发现数据与原本的lvn相同

[root@server3 ~]# mkfs.ext4 /dev/vgn/vv

[root@server3 ~]# mount /dev/vgn/vv  /mnt/ks/

[root@server3 ~]# df

Filesystem          1K-blocks    Used Available Use% Mounted on

/dev/vda3            20243456 3441148  16802308  17% /

devtmpfs               493580       0    493580   0% /dev

tmpfs                  508248      84    508164   1% /dev/shm

tmpfs                  508248   13568    494680   3% /run

tmpfs                  508248       0    508248   0% /sys/fs/cgroup

/dev/vda1              201380  133424     67956  67% /boot

tmpfs                  101652      20    101632   1% /run/user/42

tmpfs                  101652       0    101652   0% /run/user/0

/dev/mapper/vgn-lvn   3998400   16120   3769816   1% /mnt/lvm

/dev/mapper/vgn-vv     999576    2524    929060   1% /mnt/ks

2.利用快照区复原系统

改变/mnt/lvm中的数据内容,然后再以快照还原

[root@server3 mnt]# df /mnt/lvm/ /mnt/ks/

Filesystem          1K-blocks  Used Available Use% Mounted on

/dev/mapper/vgn-lvn    999576 48280    883304   6% /mnt/lvm

/dev/mapper/vgn-vv     999576  2524    929060   1% /mnt/ks

[root@server3 mnt]# lvdisplay

......

--- Logical volume ---

LV Path                /dev/vgn/vv

LV Name                vv

VG Name                vgn

......

Allocated to snapshot  7.56%       快照已被使用了7.56%

Snapshot chunk size    4.00 KiB

......

[root@server3 /]# mkdir /backup      **创建一个目录来备份

[root@server3 mnt]# cd /mnt/ks

[root@server3 ks]# tar -jcvf /backup/lvm.tar.bz2 *

[root@server3 backup]# umount /mnt/ks/       **数据已备份,将快照删除

[root@server3 backup]# lvremove  /dev/vgn/vv

Do you really want to remove active logical volume vgn/vv? [y/n]: y

Logical volume "vv" successfully removed

[root@server3 backup]# umount /mnt/lvm/

[root@server3 backup]# mkfs.ext4 /dev/vgn/lvn **将原本的lv格式化

[root@server3 backup]# mount /dev/vgn/lvn  /mnt/lvm/

[root@server3 backup]# tar -jxvf /backup/lvm.tar.bz2  -C /mnt/lvm/

[root@server3 backup]# ll /mnt/lvm/

total 16

drwx------ 2 root root 16384 Jan 22 17:45 lost+found

[root@server3 backup]# df /mnt/lvm/

Filesystem          1K-blocks  Used Available Use% Mounted on

/dev/mapper/vgn-lvn    999576  2524    929060   1% /mnt/lvm

发现数据还原