2021-05-12 14:32:11
LVM相關的命令及邏輯卷實現
1,準備工作虛擬機器vmware 和RedHat6 server,模擬給redhat6新增兩塊SCSI硬碟大小為5G和7G 也可以自定大小,新增完成->開機,(如果開機狀態新增的)# :reboot
2,檢視磁碟的磁碟名 fdisk -l (確定/dev/sdb和/dev/sdc 兩塊磁碟)
[root@localhost ~]# ll /dev/sd*
brw-rw----. 1 root disk 8, 0 Mar 24 06:55 /dev/sda
brw-rw----. 1 root disk 8, 1 Mar 24 06:55 /dev/sda1
brw-rw----. 1 root disk 8, 2 Mar 24 06:55 /dev/sda2
brw-rw----. 1 root disk 8, 16 Mar 24 06:55 /dev/sdb
brw-rw----. 1 root disk 8, 32 Mar 24 06:55 /dev/sdc
3對/dev/sdb 和/dev/sdc 分割區
[root@localhost ~]# fdisk /dev/sdb
Command (m for help): n --->命令n 建立新分割區
Command action
e extended ---->e為擴充套件分割區
p primary partition (1-4)----->p為主分割區
p ----->我們這裡選擇主分割區
Partition number (1-4): 1 ----->主分割區編號,選擇預設
First cylinder (1-652, default 1): ----->起始柱面號碼,選擇預設
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): ----->結束柱面編號,根據自己的磁碟劃分,可以用+#G來確定
Using default value 652
Command (m for help): t----->命令t 改變分割區型別
Selected partition 1
Hex code (type L to list codes): 8e----->型別為8e LVM 如果有不知道型別的可以L檢視
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w----->命令w 儲存退出
以同樣方法建立/dev/sdc
4執行命令:partprobe ----->重新讀取分割區表
cat /proc/partitons ----->檢視分割區表是否載入創的新分割區
5pv 物理卷
1:pv 建立
[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdc1-----> pvcreate 跟要加入pv的裝置
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdc1" successfully created
2:pv :檢視pv資訊
[root@localhost ~]# pvs -----> 檢視pv資訊
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup lvm2 a-- 19.51g 0
/dev/sdb1 lvm2 a-- 4.99g 4.99g
/dev/sdc1 lvm2 a-- 6.99g 6.99g
---------------------------------------------------------------
[root@localhost ~]# pvdisplay -----> 檢視pv的詳細資訊
--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroup
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID NtZgu3-kqfB-HYtw-YMzu-oM6r-Fy9c-0kmeqf
"/dev/sdb1" is a new physical volume of "4.99 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 4.99 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID CCEdHk-dQJi-YfZi-SGnO-cnoc-mLgV-AqwokJ
"/dev/sdc1" is a new physical volume of "6.99 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 6.99 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID tbAPyW-Mxzp-spvY-6qtA-k08s-pj40-7gWZHo
----------------------------------------------------------------------------
3:pv 的移除
[root@localhost ~]# pvremove /dev/sdb1------>跟要移除的分割區
Labels on physical volume "/dev/sdb1" successfully wiped
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup lvm2 a-- 19.51g 0
/dev/sdc1 lvm2 a-- 6.99g 6.99g
-----------------------------------------------------
4:pv 後設資料的移動
pvmove :加入vg以後,及生成PE ;使用pvremove 之前,要pvmove 轉移原資料
----------------------------------------------------
5;pvscan 掃描系統上pv
[root@localhost ~]# pvscan
PV /dev/sda2 VG VolGroup lvm2 [19.51 GiB / 0 free]
PV /dev/sdb1 lvm2 [4.99 GiB]
PV /dev/sdc1 lvm2 [6.99 GiB]
Total: 3 [31.50 GiB] / in use: 1 [19.51 GiB] / in no VG: 2 [11.99 GiB
=============================================
6 vg 卷組
1;建立vg vgcreate
[root@localhost ~]# vgcreate myvg /dev/sdb1 /dev/sdc1---->vgcreate vg_name /part/to/sd :vgcreate 捲組名 跟要建立vg的裝置
Volume group "myvg" successfully created
2;顯示vg vgs/vgdisplay
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 1 2 0 wz--n- 19.51g 0
myvg 2 0 0 wz--n- 11.98g 11.98g
-----------------------------------------------
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 11.98 GiB
PE Size 4.00 MiB
Total PE 3068
Alloc PE / Size 0 / 0
Free PE / Size 3068 / 11.98 GiB
VG UUID 7qz29d-SrAL-wgpf-0sQZ-XgjJ-mtVf-1eQpEG
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
---------------------------------------------
3 刪除vg vgremove
[root@localhost ~]# vgremove myvg ---->vgremove 捲組名
Volume group "myvg" successfully removed
4縮減vg(從vg中縮減pv) vgreduce
[root@localhost ~]# vgreduce myvg /dev/sdc1----->vgreduce 捲組名 卷組中的裝置
Removed "/dev/sdc1" from volume group "myvg"
5擴充套件vg ;vgextend
[root@localhost ~]# vgextend myvg /dev/sdc1--->vgextend 要新增卷組名 要新增裝置
Volume group "myvg" successfully extended
6掃描vg vgscan
[root@localhost ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "myvg" using metadata type lvm2
Found volume group "VolGroup" using metadata type lvm2
----------------------------------------------------------
7 lv 邏輯卷
1邏輯卷建立:lvcreate -L #G -n lv_name vg_name
-L :指定建立lv大小
-n :lv的名字
[root@localhost ~]# lvcreate -L 6G -n testlv myvg--->
Logical volume "testlv" created
2 lv 的檢視 :lvs :檢視資訊 / lvdisplay:檢視詳細資訊
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao---- 17.51g
lv_swap VolGroup -wi-ao---- 2.00g
testlv myvg -wi-a----- 6.00g
3 掃描lv :lvscan
[root@localhost ~]# lvscan
ACTIVE '/dev/myvg/testlv' [6.00 GiB] inherit
ACTIVE '/dev/VolGroup/lv_root' [17.51 GiB] inherit
ACTIVE '/dev/VolGroup/lv_swap' [2.00 GiB] inherit
4 格式化 mkfs /mke2fs
mke2fs -j /dev/myvg/testlv
[root@localhost ~]# mkfs /dev/myvg/testlv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
393216 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
5掛載lv mount
mkdir -p /mnt/lv 在mnt目錄下建立lv目錄,留掛載用
mount /dev/myvg/testlv /mnt/lv
[root@localhost lv]# ll /mnt/lv ----->檢視掛載點,看到lost+found 說明成功掛載
total 16
drwx------. 2 root root 16384 Mar 24 19:07 lost+found
6 檢視掛載後的lv大小 df -lh
[root@localhost lv]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 1.9G 15G 12% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 485M 39M 421M 9% /boot
/dev/mapper/myvg-testlv 6.0G 12M 5.6G 1% /mnt/lv
8:lv的擴充套件
*1 擴充套件lv物理邊界 ,先檢視一下vg中是否有足夠的可用空間
lvextend -L [+]#G /part/to/lv_name
註: 帶+擴充套件#G 不帶+表達擴充套件到#G
[root@localhost ~]# lvextend -L 10G /dev/myvg/testlv
Extending logical volume testlv to 10.00 GiB
Logical volume testlv successfully resized
(2)檢視lv大小 lvs
Logical volume testlv successfully resized
[root@localhost lv]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao---- 17.51g
lv_swap VolGroup -wi-ao---- 2.00g
testlv myvg -wi-ao---- 10.0 這是lv已經擴充套件到10G了。我們現在來看一下df -lh檔案(3)系統中的lv大小
[root@localhost lv]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 1.9G 15G 12% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 485M 39M 421M 9% /boot
/dev/mapper/myvg-testlv 6.0G 12M 5.6G 1% /mnt/lv 這是檔案系統中的lv還是6G沒有變化,所有我們還要擴充套件lv的檔案系統邊界
*2 擴充套件lv的檔案系統邊界 (在redhat5上支援線上擴充套件,redhat6不支援。需要解除安裝掛載)
lvsize2fs /part/to/lv_name #G
-p 擴充套件到和物理邊界一樣大
先解除安裝掛載umount /dev/myvg/testlv
[root@localhost lv]# umount /dev/myvg/testlv
umount: /mnt/lv: device is busy.---->磁碟繁忙,不讓我們解除安裝。
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@localhost lv]# fuser -mk /dev/myvg/testlv---->把掛載點上的進程結束,可能會logout 重登
//dev/myvg/testlv: 2164c
再強行磁碟檢查 e2fsck -f /dev/myvg/testlv
[root@localhost ~]# e2fsck -f /dev/myvg/testlv
e2fsck 1.41.12 (17-May-2010)
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
最後擴充套件lv的檔案系統邊界 resize2fs /dev/myvg/testlv 10G
[root@localhost ~]# resize2fs /dev/myvg/testlv 10G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/testlv to 2621440 (4k) blocks.
The filesystem on /dev/myvg/testlv is now 2621440 blocks long.
重新掛載啟動,mount /dve/myvg/testlv /mnt/lv
設定開機自動掛載 vi /etc/fstab 在裡面新增一行
/dev/myvg/testlv /mnt/lv ext3 defaults 0 0
~
9 :lv的縮減
1,要先檢查檔案系統,確定縮減後的資料能夠放的下
2,不能線上縮減,必須先解除安裝
3,縮減之前,必須先磁碟檢查e2fsck ,再縮減lv檔案系統大小resize2fs /part/to/lv_name
最後在縮減lv物理邊界的大lvreduce -L #G /part/to/lv_name。
4,重新掛載啟用
10 快照捲
lvcreate -s -L #G -n lv_name vg
本文永久更新連結地址:http://www.linuxidc.com/Linux/2017-03/142218.htm
相關文章