LVM逻辑卷管理 练习

1.lvm逻辑卷管理
lvm逻辑卷管理是是指:在磁盘分区与文件系统之间添加的逻辑层,提供一个抽象的卷组,使得管理者可以忽略底层磁盘布局,从而实现对分区的灵活动态调整,包括物理卷PV,卷组VG,逻辑卷LV
##1.LVM建立##

首先创建一个逻辑卷

LVM逻辑卷管理 练习

1.划分物理分区并把分区id修改为8e

pvs|pvdisplay                    
vgs|vgdisplay
lvs|lvdisplay

watch -n 1 \
'echo "=== pvinfo ==="\
;pvs\
;echo "=== vginfo ==="\
;vgs\
;echo === lvinfo ===\
;lvs'

pvcreate  /dev/vdb1                            把/dev/vdb1加进我们的PV中
vgcreate  westos /dev/vdb1                    把/dev/vdb1加进我们的VG中
lvcreate  -L 100M -n lv0 westos                创建lv0在我们的westos中大小为100M

LVM逻辑卷管理 练习

mkfs.xfs  /dev/westos/lv0                    格式化为xfs
mount /dev/westos/lv0  /mnt/                挂载

LVM逻辑卷管理 练习

扩大
lvextend -L 200M /dev/westos/lv0          扩大lv0
mount /dev/mapper/westos-lv0 /mnt            挂载
xfs_growfs  /dev/westos/lv0                 扩大文件系统

LVM逻辑卷管理 练习

当/dev/vdb1 磁盘大小不够时,可以添加一个/dev/vdb2,然后:

pvcreate  /dev/vdb2                            把/dev/vdb2加进我们的PV中
vgextend  westos  /dev/vdb2                    把/dev/vdb2加进我们的VG中
lvextend -L 600M /dev/westos/lv0            扩大lv0
mount /dev/mapper/westos-lv0 /mnt            挂载
xfs_growfs  /dev/westos/lv0                    扩大文件系统

umount /mnt/                                卸载
e2fsck -f /dev/westos/lv0                    检测设备
resize2fs  /dev/westos/lv0  550M            缩减文件系统

注意:xfs文件系统可以扩大,但是不可以缩减,如图,在这里我们需要格式化为ext4格式。


LVM逻辑卷管理 练习


[[email protected] ~]# mkfs.ext4 /dev/westos/lv0

LVM逻辑卷管理 练习

LVM逻辑卷管理 练习


lvreduce -L 550M /dev/westos/lv0             缩减设备

当/dev/vdb2空闲时。可以将其移除:

vgreduce westos  /dev/vdb2                    把VG中的/dev/vdb2移除
pvremove  /dev/vdb1                            把PV中的/dev/vdb2移除
pvmove  /dev/vdb1 /dev/vdb2                    移动数据


ext4文件系统可以扩大,也可以缩减

lvcreate  -L 10M -n lv0-backup -s /dev/westos/lv0           建立lvm快照



练习



LVM逻辑卷管理 练习

首先将虚拟机desktop重置

LVM逻辑卷管理 练习

1.
[[email protected] ~]# du -sh
28M    .
[[email protected] ~]# df -h              ## 查看系统磁盘设备和容量
Filesystem          Size  Used Avail Use% Mounted on
/dev/vda1            10G  3.1G  7.0G  31% /
devtmpfs            906M     0  906M   0% /dev
tmpfs               921M   84K  921M   1% /dev/shm
tmpfs               921M   17M  904M   2% /run
tmpfs               921M     0  921M   0% /sys/fs/cgroup
/dev/mapper/vg0-vo  473M  2.3M  442M   1% /home
[[email protected] ~]# umount /home/                         ##卸载/dev/mapper/vg0-vo设备
[[email protected] ~]# e2fsck -f /dev/mapper/vg0-vo        ##检测/dev/mapper/vg0-vo设备 
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/mapper/vg0-vo: 24/126976 files (0.0% non-contiguous), 26573/507904 blocks

LVM逻辑卷管理 练习

[[email protected] ~]# resize2fs /dev/mapper/vg0-vo 250M            ##减缩文件系统至250M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vg0-vo to 256000 (1k) blocks.
The filesystem on /dev/mapper/vg0-vo is now 256000 blocks long.

[[email protected] ~]# lvreduce -L 250M /dev/vg0/vo                               ##J减缩设备                                            
  Rounding size to boundary between physical extents: 252.00 MiB
  WARNING: Reducing active logical volume to 252.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vo? [y/n]: y
  Reducing logical volume vo to 252.00 MiB

  Logical volume vo successfully resized


[[email protected] ~]# mount -a                            ## 自动挂载
[[email protected] ~]# df -h
Filesystem          Size  Used Avail Use% Mounted on
/dev/vda1            10G  3.1G  7.0G  31% /
devtmpfs            906M     0  906M   0% /dev
tmpfs               921M   84K  921M   1% /dev/shm
tmpfs               921M   17M  904M   2% /run
tmpfs               921M     0  921M   0% /sys/fs/cgroup
/dev/mapper/vg0-vo  234M  2.1M  216M   1% /home
[[email protected] ~]#



2.

[[email protected] ~]# dd if=/dev/zero of=/swapfile bs=1M count=512     ##截取大小为512M的文件
512+0 records in
512+0 records out
536870912 bytes (537 MB) copied, 4.45746 s, 120 MB/s
[[email protected] ~]# mkswap /swapfile                                                      ##格式化为swp格式
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=e8e3ae0d-3bf0-4c44-a7ae-2ac1bb07df94
[[email protected] ~]# vim /etc/fstab

[[email protected] ~]# mount -a

[[email protected] ~]# swapon -s
Filename                Type        Size    Used    Priority
/swapfile                                  file    524284    0    -1

LVM逻辑卷管理 练习LVM逻辑卷管理 练习






3.

[[email protected] ~]# 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
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 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): +1G
Partition 2 of type Linux and of size 1 GiB is set
Command (m for help): t
Partition number (1,2, default 2): 2
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: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM
/dev/vdb2         1026048     3123199     1048576   8e  Linux LVM

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[[email protected] ~]# partprobe                             ##同步分区
LVM逻辑卷管理 练习

[[email protected] ~]# pvcreate /dev/vdb2                  ##把/dev/vdb2加入到PV中

  Physical volume "/dev/vdb2" successfully created
[[email protected] ~]# vgcreate -s 8M westos /dev/vdb2                 ##把/dev/vdb2加入到VG
  Volume group "westos" successfully created
[[email protected] ~]# lvcreate -l 100 -n westoslv0 westos               ##
  Logical volume "westoslv0" created
[[email protected] ~]# pvs
  PV         VG     Fmt  Attr PSize    PFree  
  /dev/vdb1  vg0    lvm2 a--   496.00m 244.00m
  /dev/vdb2  westos lvm2 a--  1016.00m 216.00m
[[email protected] ~]# vim /etc/fstab

[[email protected] ~]# mkfs.ext4 /dev/mapper/westos-westoslv0

LVM逻辑卷管理 练习

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
51296 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=209715200
7 block groups
32768 blocks per group, 32768 fragments per group
7328 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done


[[email protected] ~]# mkdir /mnt/westos

[[email protected] ~]# vim /etc/fstab

LVM逻辑卷管理 练习

[[email protected] ~]# mount -a
[[email protected] ~]# df
Filesystem                   1K-blocks    Used Available Use% Mounted on
/dev/vda1                     10473900 3673720   6800180  36% /
devtmpfs                        927072       0    927072   0% /dev
tmpfs                           942660      80    942580   1% /dev/shm
tmpfs                           942660   17048    925612   2% /run
tmpfs                           942660       0    942660   0% /sys/fs/cgroup
/dev/mapper/vg0-vo              239527    2083    220549   1% /home

/dev/mapper/westos-westoslv0    789904    1608    730952   1% /mnt/westos

LVM逻辑卷管理 练习