Linux 2 unit5 LVM建立

weixin_34026276發表於2017-11-09

unit5  使用邏輯卷管理靈活儲存

###LVM建立###
[root@station ~]# mkdir /weixindata   ##建立目錄
[root@station ~]# pvcreate /dev/vdb1   ##建立pv
  Physical volume /dev/vdb1 notfound
  Can't open /dev/vdb1 exclusively.  Mounted filesystem?
[root@station ~]# vgcreate vg0 /dev/vdb1  ##建立vg
  Physical volume /dev/vdb1 notfound
  Physical volume /dev/vdb1 notfound
  Can't open /dev/vdb1 exclusively.  Mounted filesystem?
  Unable to add physical volume'/dev/vdb1' to volume group 'vg0'.

[root@station ~]# mkfs.xfs /dev/vg0
/dev/vg0: No such file or directory
Usage: mkfs.xfs
/* blocksize */     [-blog=n|size=num]
/* metadata */      [-m crc=[0|1]
/* data subvol */   [-dagcount=n,agsize=n,file,name=xxx,size=num,
               (sunit=value,swidth=value|su=num,sw=num|noalign),
                sectlog=n|sectsize=num
/* force overwrite */   [-f]
/* inode size */    [-ilog=n|perblock=n|size=num,maxpct=n,attr=0|1|2,
                projid32bit=0|1]
/* no discard */    [-K]
/* log subvol */    [-lagnum=n,internal,size=num,logdev=xxx,version=n
                sunit=value|su=num,sectlog=n|sectsize=num,
                lazy-count=0|1]
/* label */     [-L label (maximum 12characters)]
/* naming */        [-nlog=n|size=num,version=2|ci,ftype=0|1]
/* no-op info only */   [-N]
/* prototype file */    [-p fname]
/* quiet */     [-q]
/* realtime subvol */   [-r extsize=num,size=num,rtdev=xxx]
/* sectorsize */    [-slog=n|size=num]
/* version */       [-V]
            devicename
is required unless -d name=xxx is given.
is xxx (bytes), xxxs (sectors), xxxb (fs blocks), xxxk (xxxKiB),
      xxxm (xxx MiB), xxxg (xxx GiB), xxxt(xxx TiB) or xxxp (xxx PiB).
is xxx (512 byte blocks).
[root@station ~]# mdadm -D /dev/md0    ##檢視裝置資訊
mdadm: md device /dev/md0 does not appear to be active.
[root@station ~]# mdadm -S /dev/md0    ##停止此裝置
mdadm: stopped /dev/md0
[root@station ~]# mdadm -a /dev/md0  /dev/vdb2  ##新增一個新裝置
[root@station ~]# vgcreate vg0 /dev/vdb1

  Volume group "vg0"successfully created
[root@station ~]# lvcreate -L 300M -n lv0 vg0  ##建立300M的lv0在vg0下
  Logical volume "lv0"created
[root@station ~]# mkfs.xfs /dev/vg0/lv0 ##格式化
meta-data=/dev/vg0/lv0          isize=256    agcount=4,agsize=19200 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=76800, 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@station ~]# mount /dev/vg0/lv0 /weixindata/  ##掛載
[root@station ~]# ll /dev/mapper/vg0-lv0

lrwxrwxrwx. 1 root root 7 Apr 22 03:52 /dev/mapper/vg0-lv0 -> ../dm-0  ##下面測試結果出現/mapper實則和/vg0相同
[root@station ~]# ll /dev/vg0/lv0
lrwxrwxrwx. 1 root root 7 Apr 22 03:52 /dev/vg0/lv0 -> ../dm-0

測試結果

口令:watch -n 1 'echo ===pvmessage==;pvs;echo===vgmessage===;vgs;echo===lvm message===;lvs;echo ===;df/weixindata'
===pvmessage==

  PV         VG  Fmt  Attr PSize    PFree
  /dev/vdb1  vg0 lvm2 a--  1020.00m 720.00m
  VG  #PV #LV #SN Attr   VSize    VFree
  vg0   1   1   0 wz--n- 1020.00m 720.00m
  LV  VG   Attr       LSize  Pool Origin Data
%  Move Log Cpy%Sync Convert
  lv0 vg0  -wi-ao---- 300.00m

===
Filesystem          1K-blocks  Used Available U
se% Mounted on
/dev/mapper/vg0-lv0    303788 15520    288268 
 6% /weixindata

####裝置拉伸####
[root@station ~]# lvextend -L 900M /dev/vg0/lv0   ##將原裝置拉伸為900M
  Extending logical volume lv0 to 900.00MiB
  Logical volume lv0 successfullyresized
[root@station ~]# xfs_growfs /dev/vg0/lv0  ##檔案系統的擴充套件
meta-data=/dev/mapper/vg0-lv0   isize=256    agcount=4,agsize=19200 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=76800, 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 76800 to 230400
[root@station ~]# lvextend -L 1200M /dev/vg0/lv0  ##繼續拉伸為1200
  Extending logical volume lv0 to 1.17GiB
  Insufficient free space: 75 extentsneeded, but only 30 available ///提醒剩餘空間不足
[root@station ~]# pvcreate /dev/vdb2   ##建立另一個pv
  Physical volume "/dev/vdb2"successfully created
[root@station ~]# vgextend vg0 /dev/vdb2  ##將另一個pv加入vg中
  Volume group "vg0"successfully extended
[root@station ~]# lvextend -L 1200M /dev/vg0/lv0  ##繼續拉伸
  Extending logical volume lv0 to 1.17GiB
  Logical volume lv0 successfully resized//拉伸成功
[root@station ~]# xfs_growfs /dev/vg0/lv0 ##檔案系統的拉伸
meta-data=/dev/mapper/vg0-lv0   isize=256    agcount=12,agsize=19200 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=230400, 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 230400 to 307200


###系統的縮減###
1)縮減之前所做準備
[root@station ~]# umount /weixindata/  ##解除安裝
[root@station ~]# mkfs.ext4 /dev/vg0/lv0 ##更改檔案系統

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
76800 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=314572800
10 block groups
32768 blocks per group, 32768 fragments per group
7680 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376,294912

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

[root@station ~]# mount /dev/vg0/lv0 /weixindata/  ##掛載
[root@station ~]# df  ##檢視掛載是否成功

Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/vda1            104739003241020   7232880  31% /
devtmpfs               927072       0   927072   0% /dev
tmpfs                  942660     472   942188   1% /dev/shm
tmpfs                  942660   17080   925580   2% /run
tmpfs                  942660       0   942660   0% /sys/fs/cgroup
/dev/mapper/vg0-lv0   1176704    3600  1095280   1% /weixindata

2)開始縮減
[root@station ~]# umount /weixindata/  ##解除安裝
[root@station ~]# 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/76800 files (0.0% non-contiguous), 13924/307200 blocks
[root@station ~]# resize2fs /dev/vg0/lv0 300M  ##縮減檔案系統為300M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 76800 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 76800 blocks long.

[root@station ~]# mount /dev/vg0/lv0 /weixindata/  ##掛載
[root@station ~]# lvreduce -L 300 /dev/vg0/lv0 ##縮減裝置

  WARNING: Reducing active and openlogical volume to 300.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystemetc.)
Do you really want to reduce lv0? [y/n]: y
  Reducing logical volume lv0 to 300.00MiB
  Logical volume lv0 successfullyresized
[root@station ~]# pvmove /dev/vdb1 /dev/vdb2 ##把裝置1資訊傳給裝置2
[root@station ~]# vgreduce vg0 /dev/vdb1 ##把vdb1從vg0移除

  Removed "/dev/vdb1" fromvolume group "vg0"
  /dev/vdb1: Moved: 1.3%
  /dev/vdb1: Moved: 100.0%
[root@station ~]# pvremove /dev/vdb1  把vdb1從pv移除
  Labels on physical volume"/dev/vdb1" successfully wiped


####快照####
[root@station ~]# touch /weixindata/file{1..10}
[root@station ~]# ls /weixindata/

file1   file2  file4 file6  file8  lost+found
file10  file3  file5 file7  file9
[root@station ~]# umount /weixindata/
[root@station ~]# cd /weixindata/
[root@station weixindata]# ls
[root@station weixindata]# cd 
[root@station ~]# lvcreate  -L 50M -nlv0backup -s /dev/vg0/lv0    ###建立快照

  Rounding up size to full physicalextent 52.00 MiB
  Logical volume "lv0backup"created
[root@station ~]# mount /dev/vg0/lv0backup /weixindata/
[root@station ~]# cd /weixindata/
[root@station weixindata]# ls

file1   file2  file4 file6  file8  lost+found
file10  file3  file5 file7  file9
[root@station weixindata]# rm -fr *
[root@station weixindata]# ls
[root@station weixindata]# cd 
[root@station ~]# umount /weixindata/
[root@station ~]# lvremove /dev/vg0/lvobackup ##刪除壞了的快照

  One or more specified logical volume(s)not found.
[root@station ~]# lvcreate  -L 50M -nlv0backup -s /dev/vg0/lv0   ##重新快照
  Rounding up size to full physicalextent 52.00 MiB
  Logical volume "lv0backup"already exists in volume group "vg0"
[root@station ~]# mount /dev/vg0/lv0backup /weixindata/
[root@station ~]# ls /weixindata/

file1   file2  file4 file6  file8  lost+found
file10  file3  file5 file7  file9


監護口令: 
watch -n 1 'echo ===pvmessage==; pvs;echo===vgmessage===;vgs;echo===lvmmessage===;lvs;echo ===;df /weixindata'

快照成功時監護介面:
===pvmessage==
  PV         VG  Fmt  Attr PSize    PFree
  /dev/vdb2  vg0 lvm2 a--  1020.00m 668.00m
  VG  #PV #LV #SN Attr   VSize    VFree
  vg0   1   2   1 wz--n- 1020.00m 668.00m
  LV        VG  Attr       LSize   Pool Origin
 Data% Move Log Cpy%Sync Convert
  lv0       vg0 owi-a-s--- 300.00m

  lv0backup vg0  swi-a-s--- 52.00m  lv0
   0.00
===
Filesystem     1K-blocks    Used Available Use%
 Mounted on
/dev/vda1       10473900 3240980   7232920 31%
 /

####LVM的刪除####
[root@station ~]# umount /weixindata/  ##解除安裝
[root@station ~]# lvremove /dev/vg0/lv0backup ##刪除快照

Do you really want to remove active logical volume lv0backup? [y/n]: y
  Logical volume "lv0backup"successfully removed
[root@station ~]# lvremove /dev/vg0/lv0 ##刪除lv0本身
Do you really want to remove active logical volume lv0? [y/n]: y
  Logical volume "lv0"successfully removed
[root@station ~]# vgremove vg0   刪除vg
  Volume group "vg0"successfully removed
[root@station ~]# pvremove /dev/vdb2  刪除pv
  Labels on physical volume"/dev/vdb2" successfully wiped

本文轉自AELY木部落格51CTO部落格,原文連結http://blog.51cto.com/12768057/1919200如需轉載請自行聯絡原作者


AELY木

相關文章