首页 > 代码库 > 西部开源学习笔记BOOK2-《unit 5》

西部开源学习笔记BOOK2-《unit 5》

#########################

####### unit5.lvm #######

#########################


lvm的逻辑关系:

物理分区--->pv物理卷---->vg物理卷组----->取出一部分的叫lvm

                /\

                ||组成

                pe(卷的最小单位)

注意:那vg可不可以无限扩大? 不可以

因为xfs文件系统最大支持18EB,超过的话就无法管理。


######1.创建物理分区########

[root@desktop19 ~]# 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.

Device does not contain a recognized partition table

Building a new DOS disklabel with disk identifier 0x0e7029a1.

Command (m for help): n

Partition type:

p primary (0 primary, 0 extended, 4 free)

e extended

Select (default p):

Using default response p

Partition number (1-4, default 1):

First sector (2048-20971519, default 2048):

Using default value 2048

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

Partition 1 of type Linux and of size 500 MiB is set

Command (m for help): n

Partition type:

p primary (1 primary, 0 extended, 3 free)

e extended

Select (default p):

Using default response p

Partition number (2-4, default 2):

First sector (1026048-20971519, default 1026048):

Using default value 1026048

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

Partition 2 of type Linux and of size 500 MiB is set

Command (m for help): t

Partition number (1,2, default 2):

Hex code (type L to list all codes): 8e ##分区id号:8e Linux LVM

Changed type of partition ‘Linux‘ to ‘Linux LVM‘

Command (m for help): t

Partition number (1,2, default 2): 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/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: 0x0e7029a1

Device Boot Start End Blocks Id System

/dev/vdb1 2048 1026047 512000 8e Linux LVM

/dev/vdb2 1026048 2050047 512000 8e Linux LVM

Command (m for help): wq

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

[root@desktop19 ~]# partprobe


#######2.创建lvm#######

[root@desktop19 ~]# pvcreate /dev/vdb1 ##创建pv

Physical volume "/dev/vdb1" successfully created

[root@desktop19 ~]# vgcreate tbr /dev/vdb1 ##创建vg,取名为tbr,包含/dev/vdb1

Volume group "tbr" successfully created

[root@desktop19 ~]# lvcreate -L 100M -n lv0 tbr ##

Logical volume "lv0" created

[root@desktop19 ~]# mkfs.xfs /dev/tbr/lv0

meta-data=http://www.mamicode.com/dev/tbr/lv0 isize=256 agcount=4, agsize=6400 blks

= sectsz=512 attr=2, projid32bit=1

= crc=0

data = http://www.mamicode.com/bsize=4096 blocks=25600, imaxpct=25

= sunit=0 swidth=0 blks

naming =version 2 bsize=4096 ascii-ci=0 ftype=0

log =internal log bsize=4096 blocks=853, version=2

= sectsz=512 sunit=0 blks, lazy-count=1

realtime =none extsz=4096 blocks=0, rtextents=0

[root@desktop19 ~]# mount /dev/tbr/lv0 /mnt/

[root@desktop19 ~]# df

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/vda1 10473900 3119952 7353948 30% /

devtmpfs 927072 0 927072 0% /dev

tmpfs 942660 80 942580 1% /dev/shm

tmpfs 942660 17040 925620 2% /run

tmpfs 942660 0 942660 0% /sys/fs/cgroup

/dev/mapper/tbr-lv0 98988 5280 93708 6% /mnt

[root@desktop19 ~]# ll /dev/tbr/lv0

lrwxrwxrwx. 1 root root 7 11月 6 01:29 /dev/tbr/lv0 -> ../dm-0

[root@desktop19 ~]# ll /dev/mapper/tbr-lv0

lrwxrwxrwx. 1 root root 7 11月 6 01:29 /dev/mapper/tbr-lv0 -> ../dm-0

[root@desktop19 ~]# watch -n 1 ‘echo "===pvinfo===";pvs;echo "===vginfo===";vgs;echo

"===lvinfo===";lvs;df -h /mnt‘

===pvinfo===

PV VG Fmt Attr PSize PFree

/dev/vdb1 tbr lvm2 a-- 496.00m 396.00m

===vginfo===

VG #PV #LV #SN Attr VSize VFree

tbr 1 1 0 wz--n- 496.00m 396.00m

===lvinfo===

LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert

lv0 tbr -wi-ao---- 100.00m

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/tbr-lv0 97M 5.2M 92M 6% /mnt


############2.扩展lvm############

[root@desktop19 ~]# lvcreate -L 100M -n lv0 tbr

Logical volume "lv0" created

[root@desktop19 ~]# mkfs.xfs /dev/tbr/lv0

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/tbr-lv0 297M 5.5M 292M 2% /mnt

[root@desktop19 ~]# lvextend -L 200M /dev/tbr/lv0

Extending logical volume lv0 to 200.00 MiB

Logical volume lv0 successfully resized

[root@desktop19 ~]# lvextend -L 500M /dev/tbr/lv0

Extending logical volume lv0 to 500.00 MiB

Insufficient free space: 75 extents needed, but only 74 available

[root@desktop19 ~]# lvextend -L 300M /dev/tbr/lv0

Extending logical volume lv0 to 300.00 MiB

Logical volume lv0 successfully resized

[root@desktop19 ~]# xfs_growfs /dev/tbr/lv0

[root@desktop19 ~]# pvcreate /dev/vdb2

Physical volume "/dev/vdb2" successfully created

[root@desktop19 ~]# vgextend tbr /dev/vdb2

Volume group "tbr" successfully extended

[root@desktop19 ~]# lvextend -L 600M /dev/tbr/lv0

Extending logical volume lv0 to 600.00 MiB

Logical volume lv0 successfully resized

===pvinfo===

PV VG Fmt Attr PSize PFree

/dev/vdb1 tbr lvm2 a-- 496.00m 0

/dev/vdb2 tbr lvm2 a-- 496.00m 392.00m

===vginfo===

VG #PV #LV #SN Attr VSize VFree

tbr 2 1 0 wz--n- 992.00m 392.00m

===lvinfo===

LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert

lv0 tbr -wi-ao---- 600.00m

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/tbr-lv0 297M 5.5M 292M 2% /mnt

[root@desktop19 ~]# xfs_growfs /dev/tbr/lv0

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/tbr-lv0 597M 5.8M 591M 1% /mnt


#############3.缩减lvm##############

[root@desktop19 ~]# umount /mnt

[root@desktop19 ~]# mkfs.ext4 /dev/tbr/lv0

[root@desktop19 ~]# e2fsck -f /dev/tbr/lv0

[root@desktop19 ~]# resize2fs /dev/tbr/lv0 400M

[root@desktop19 ~]# mount /dev/tbr/lv0 /mnt/

[root@desktop19 ~]# lvreduce -L 400M /dev/tbr/lv0

WARNING: Reducing active and open logical volume to 400.00 MiB

THIS MAY DESTROY YOUR DATA (filesystem etc.)

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

Reducing logical volume lv0 to 400.00 MiB

Logical volume lv0 successfully resized

[root@desktop19 ~]# pvmove /dev/vdb1 /dev/vdb2

/dev/vdb1: Moved: 13.0%

/dev/vdb1: Moved: 100.0%

[root@desktop19 ~]# vgreduce tbr /dev/vdb1

===pvinfo===

PV VG Fmt Attr PSize PFree

/dev/vdb1 lvm2 a-- 500.00m 500.00m

/dev/vdb2 tbr lvm2 a-- 496.00m 96.00m

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

===pvinfo===

PV VG Fmt Attr PSize PFree

/dev/vdb2 tbr lvm2 a-- 496.00m 96.00m

pvmove的时候耐心等待,不要ctrl+c(操作数据时要小心)


#####特殊情况#####

1.如果先缩减了物理卷组的大小,而没有缩减文件系统的大小。(卸载再挂载之后会有问题)

需要卸载2次的问题:##df查看时,只显示第一次挂载的设备

[root@desktop19 ~]# lvcreate -L 10M -n lv0-backup -s /dev/tbr/lv0

Rounding up size to full physical extent 12.00 MiB

Logical volume "lv0-backup" created

[root@desktop19 ~]# mount /dev/tbr/lv0-backup /mnt

[root@desktop19 ~]# df

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/vda1 10473900 3123860 7350040 30% /

devtmpfs 927072 0 927072 0% /dev

tmpfs 942660 80 942580 1% /dev/shm

tmpfs 942660 17056 925604 2% /run

tmpfs 942660 0 942660 0% /sys/fs/cgroup

/dev/mapper/tbr-lv0 588352 912 551972 1% /mnt

[root@desktop19 ~]# umount /mnt/

[root@desktop19 ~]# df

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/vda1 10473900 3123860 7350040 30% /

devtmpfs 927072 0 927072 0% /dev

tmpfs 942660 80 942580 1% /dev/shm

tmpfs 942660 17056 925604 2% /run

tmpfs 942660 0 942660 0% /sys/fs/cgroup

/dev/mapper/tbr-lv0 588352 912 551972 1% /mnt

[root@desktop19 ~]# umount /mnt/

[root@desktop19 ~]# df

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/vda1 10473900 3123860 7350040 30% /

devtmpfs 927072 0 927072 0% /dev

tmpfs 942660 80 942580 1% /dev/shm

tmpfs 942660 17056 925604 2% /run

tmpfs 942660 0 942660 0% /sys/fs/cgroup


西部开源学习笔记BOOK2-《unit 5》