Linux文件系统LVM建立

LVM的建立过程:

Linux文件系统LVM建立_第1张图片

通过fdisk分区:

1.划分物理分区并把分区格式变为lvm:可参考博客:http://blog.csdn.net/dream_ya/article/details/79158238

[root@dream /]# fdisk -l
    Device Boot      Start         End      Blocks   Id   System
 /dev/vdb1            2048     2099199     1048576   8e   Linux LVM
 /dev/vdb2         2099200     4196351     1048576   8e   Linux LVM
 pvs|pvdisplay                                                         ###查看pv
 vgs|vgdisplay                                                         ###查看vg
 lvs|lvdisplay                                                         ###查看lvm

通过watch命令监控状态

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

Linux文件系统LVM建立_第2张图片

建立pv

 [root@dream /]# pvcreate /dev/vdb1                                  ###把/dev/vdb1建立成pv
 WARNING: xfs signature detected on /dev/vdb1 at offset 0. Wipe it? [y/n] y
   Wiping xfs signature on /dev/vdb1.
   Physical volume "/dev/vdb1" successfully created

Linux文件系统LVM建立_第3张图片

建立vg

 [root@dream /]# vgcreate -s 2M vg0 /dev/vdb1                       ###-s:vg中pe(最小单位)的大小,名字取为vg0
   Volume group "vg0" successfully created

Linux文件系统LVM建立_第4张图片

从vg中取出lvm

 [root@dream /]# lvcreate -L 100M -n lv0 vg0                         ###从vg中取出100M建立lvm
   Logical volume "lv0" created

Linux文件系统LVM建立_第5张图片

格式化后挂载

 [root@dream /]# mkfs.xfs /dev/vg0/lv0          
 meta-data=/dev/vg0/lv0           isize=256    agcount=4,  agsize=6400 blks
          =                       sectsz=512   attr=2,  projid32bit=1
          =                       crc=0
 data     =                       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@dream /]# mount /dev/vg0/lv0 /mnt
 [root@dream /]# df -h
 Filesystem           Size  Used Avail Use% Mounted on
 /dev/vda1             10G  3.0G  7.1G  30% /
 devtmpfs             459M     0  459M   0% /dev
 tmpfs                474M   80K  474M   1% /dev/shm
 tmpfs                474M   13M  462M   3% /run
 tmpfs                474M     0  474M   0% /sys/fs/cgroup
 /dev/mapper/vg0-lv0   97M  5.2M   92M   6% /mnt

LVM在线拉伸

1.当vg中的容量够用时

 [root@dream /]# lvextend -L 500M /dev/vg0/lv0                             ###扩展lvm的容量(直接从vg中取)拉伸到50M  
   Extending logical volume lv0 to 500.00 MiB
   Logical volume lv0 successfully resized
 [root@dream /]# xfs_growfs /dev/vg0/lv0                                   ###扩大文件系统xfs
 meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=4, agsize=6400 blks
          =                       sectsz=512   attr=2, projid32bit=1
          =                       crc=0
 data     =                       bsize=4096   blocks=25600, imaxpct=25
          =                       sunit=0      swidth=0 blks
 naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
 log      =internal               bsize=4096   blocks=853, version=2
          =                       sectsz=512   sunit=0 blks, lazy-count=1
 realtime =none                   extsz=4096   blocks=0, rtextents=0
 data blocks changed from 25600 to 128000

我们可以发现设备还在挂载中但却实现了拉伸

Linux文件系统LVM建立_第6张图片

2.当vg中的容量不够用时
把vdb2转换成pv

 [root@dream /]# pvcreate /dev/vdb2
 WARNING: xfs signature detected on /dev/vdb2 at offset 0. Wipe it? [y/n] y
   Wiping xfs signature on /dev/vdb2.
   Physical volume "/dev/vdb2" successfully created

Linux文件系统LVM建立_第7张图片

把vdb2加到vg0中

 [root@dream /]# vgextend vg0 /dev/vdb2
   Volume group "vg0" successfully extended

可以发现vg0的容量变为了2G

Linux文件系统LVM建立_第8张图片

拉伸到1.5G

 [root@dream /]# lvextend -L 1.5G /dev/vg0/lv0 
   Extending logical volume lv0 to 1.50 GiB
   Logical volume lv0 successfully resized
 [root@dream /]# xfs_growfs /dev/vg0/lv0 
 meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=20, agsize=6400 blks
          =                       sectsz=512   attr=2, projid32bit=1
          =                       crc=0
 data     =                       bsize=4096   blocks=128000, imaxpct=25
          =                       sunit=0      swidth=0 blks
 naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
 log      =internal               bsize=4096   blocks=853, version=2
          =                       sectsz=512   sunit=0 blks, lazy-count=1
 realtime =none                   extsz=4096   blocks=0, rtextents=0
 data blocks changed from 128000 to 393216

Linux文件系统LVM建立_第9张图片

LVM的在线拉伸便说完了,但它有个缺陷并不能在线缩小,如果想可以缩小,可以把其格式化为ext4格式,但这中并不支持在线拉伸

ext4文件系统中拉伸

 [root@dream /]# umount /mnt/
 [root@dream /]# df -h
 Filesystem      Size  Used Avail Use% Mounted on
 /dev/vda1        10G  3.0G  7.1G  30% /
 devtmpfs        459M     0  459M   0% /dev
 tmpfs           474M   80K  474M   1% /dev/shm
 tmpfs           474M   13M  462M   3% /run
 tmpfs           474M     0  474M   0% /sys/fs/cgroup
 [root@dream /]# mkfs.ext4 /dev/vg0/lv0                                         ###格式化为ext4格式

拉伸到1.8G

 root@dream /]# lvextend -L 1.8G /dev/vg0/lv0 
   Rounding size to boundary between physical extents: 1.80 GiB
   Extending logical volume lv0 to 1.80 GiB
   Logical volume lv0 successfully resized
 [root@dream /]# resize2fs /dev/vg0/lv0 1.8G
 resize2fs 1.42.9 (28-Dec-2013)
 resize2fs: Invalid new size: 1.8G

Linux文件系统LVM建立_第10张图片

缩小到1G

 [root@dream /]# e2fsck -f /dev/vg0/lv0                                        ###扫描数据大小,预防造成数据损失
 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/vg0/lv0: 11/98304 files (9.1% non-contiguous), 15524/393216 blocks
 [root@dream /]# resize2fs /dev/vg0/lv0 1G                                    ###减小文件系统
 resize2fs 1.42.9 (28-Dec-2013)
 Resizing the filesystem on /dev/vg0/lv0 to 262144 (4k) blocks.
 The filesystem on /dev/vg0/lv0 is now 262144 blocks long.
 [root@dream /]# lvreduce -L 1G /dev/vg0/lv0                                  ###减小设备大小
   WARNING: Reducing active logical volume to 1.00 GiB
   THIS MAY DESTROY YOUR DATA (filesystem etc.)
 Do you really want to reduce lv0? [y/n]: y
   Reducing logical volume lv0 to 1.00 GiB
   Logical volume lv0 successfully resized

可以发现大小变为了1G

Linux文件系统LVM建立_第11张图片

正在使用的设备如何移出:

 pvmove  /dev/vdb1 /dev/vdb2                                             ###把/dev/vdb1数据拷贝到vdb2
 vgreduce westos  /dev/vdb1                                              ###把/dev/vdb1从westos中移出来
 pvremove  /dev/vdb1                                                     ###移出设备(未使用的直接这一步就好)

LVM快照

 [root@dream /]# mount /dev/vg0/lv0 /mnt
 [root@dream /]# cd /mnt
 [root@dream mnt]# ls
 lost+found
 [root@dream mnt]# touch file{1..10}
 [root@dream mnt]# ls
 file1   file2  file4  file6  file8  lost+found
 file10  file3  file5  file7  file9
 [root@dream /]# lvcreate  -L 10M -n lv0-backup -s /dev/vg0/lv0         ###创建快照
  Logical volume "lv0-backup" created
 [root@dream /]# mount /dev/vg0/lv0-backup /mnt
 [root@dream /]# ls /mnt
 file1   file2  file4  file6  file8  lost+found
 file10  file3  file5  file7  file9
 [root@dream /]# rm -rf /mnt/*
 [root@dream /]# umount /dev/vg0/lv0-backup
 [root@dream /]# lvcreate  -L 10M -n lv0-backup -s /dev/vg0/lv0
 mount /dev/vg0/lv0-backup /mnt/
 ls /mnt

lvm的删除

 lvremove /dev/vg0/lv0
 vgremove vg0
 pvremove /dev/vdb1
 fdisk /dev/vdb

lvm挂载着的设备直接在fdisk中删除出现问题:

 vgreduce --removemissing vg0

你可能感兴趣的:(运维)