歡迎您光臨本站 註冊首頁

CentOS 5 LVM邏輯卷管理

←手機掃碼閱讀     火星人 @ 2014-03-08 , reply:0
相關知識點 LVM: Logical Volume Manager邏輯卷管理,它是一種磁碟的管理方式,是Linux環境下對磁碟分區進行管理的一種機制,LVM是建立在硬碟和分區之上的一個邏輯層,來提高磁碟分區管理的靈活性.通過LVM系統管理員可以輕鬆管理磁碟分區. 卷組volume group (VG) 卷組是LVM中最高抽象層,是由一個或多個物理卷所組成的存儲器池. 物理卷physical volume (PV) 典型的物理卷是硬碟分區,但也可以是整個硬碟或已創建的Software RAID 卷. 邏輯卷logical volume (LV) 邏輯卷相當於非LVM系統中的分區,它在卷組上建立,是一個標準的塊設備,可以在其上建立文件系統. 物理塊physical extent (PE) 物理卷按大小相等的“塊”為單位存儲,塊的大小與卷組中邏輯卷塊的大小相同. 邏輯塊logical extent (LE) 邏輯卷按“塊”為單位存儲,在一卷組中的所有邏輯卷的塊大小是相同的. 作業環境 註:以下的操作是在VirtualBox4.1虛擬機環境下進行的. 系統版本:CentOS 5.8 硬碟大小(虛擬硬碟):20G 一、先查看系統空間使用情況. 根據關注點的不同,使用不同的使命查看系統空間的使用情況. 查看磁碟的分區信息,可以看到,系統除了引導分區(100M)外,其他所有的空間都在LVM里. [root@sqing ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 2610 20860402 8e Linux LVM 查看節點的使用情況 [root@sqing ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-lv_root 9.7G 926M 8.3G 10% / /dev/sda1 99M 13M 82M 13% /boot tmpfs 506M 0 506M 0% /dev/shm 查看邏輯卷組信息,可以看到,vg中有7.88G的剩餘空間. [root@sqing ~]# vgscan Reading all physical volumes. This may take a while... Found volume group "VolGroup00" using metadata type lvm2 [root@sqing ~]# vgdisplay --- Volume group --- VG Name VolGroup00 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 4 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.88 GB PE Size 32.00 MB Total PE 636 Alloc PE / Size 448 / 12.00 GB Free PE / Size 188 / 7.88 GB VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH 查看邏輯卷信息,可以看到,根分區大小為10G,交換分區(swap)大小為2G. [root@sqing ~]# lvscan ACTIVE '/dev/VolGroup00/lv_root' [10.00 GB] inherit ACTIVE '/dev/VolGroup00/lv_swap' [2.00 GB] inherit 二、裝機時,swap分區大小設置為2G,現在發現不夠用,需要將其擴大到4G. 擴大swap邏輯卷 步驟:首先取消激活swap空間,然後修改swap分區大小,接著將swap分區設置為交換區,激活swap空間. [root@sqing ~]# swapoff usage: swapoff [-hV] swapoff -a [-v] swapoff [-v] special ... [root@sqing ~]# lvresize -L 4G /dev/VolGroup00/lv_swap #指定大小增加到4G Extending logical volume lv_swap to 4.00 GB Logical volume lv_swap successfully resized [root@sqing ~]# mkswap -f /dev/VolGroup00/lv_swap Setting up swapspace version 1, size = 4294963 kB [root@sqing ~]# swapon usage: swapon [-hV] swapon -a [-e] [-v] swapon [-v] [-p priority] special|LABEL=volume_name ... swapon [-s] (註:如果縮小swap邏輯卷,其操作步驟與上述步驟相同.) 操作完成後,查看當前的lv、vg信息,可以看到lv_swap現在已經是4G了,而邏輯卷的空閑大小由先前的7.88G,變為5.88G. [root@sqing ~]# lvscan ACTIVE '/dev/VolGroup00/lv_root' [10.00 GB] inherit ACTIVE '/dev/VolGroup00/lv_swap' [4.00 GB] inherit [root@sqing ~]# vgdisplay --- Volume group --- VG Name VolGroup00 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 6 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.88 GB PE Size 32.00 MB Total PE 636 Alloc PE / Size 448 / 14.00 GB Free PE / Size 188 / 5.88 GB VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH 三、現在要將磁碟剩餘空間利用起來. 在/dev/VolGroup00上新增一個邏輯分區,命名為lv_data,大小為邏輯卷組VolGroup00中剩餘的空間(5.88G). 新建邏輯卷lv_data 步驟:創建新分區;接著格式化新分區;然後掛載新分區(Linux上不能直接使用分區,使用分區時是通過目錄存儲的,故需將lv_data掛載到一個目錄上) (註:以下操作,本想一下子使用剩餘的所有空間5.88G,但由於系統數據與顯示數據(G級位元組)上的出入,輸入的數字5.88G過大,后改為5.86G) [root@sqing ~]# lvcreate -L 5.88G -n lv_data /dev/VolGroup00 Rounding up size to full physical extent 5.91 GB Volume group "VolGroup00" has insufficient free space (188 extents): 189 required. [root@sqing ~]# lvcreate -L 5.86G -n lv_data /dev/VolGroup00 Rounding up size to full physical extent 5.88 GB Logical volume "lv_data" created [root@sqing ~]# mkfs.ext3 /dev/VolGroup00/lv_data mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 770048 inodes, 1540096 blocks 77004 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1577058304 47 block groups 32768 blocks per group, 32768 fragments per group 16384 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 31 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. 查看此時的vg信息,發現剩餘空間剛好用完. [root@sqing ~]# vgdisplay --- Volume group --- VG Name VolGroup00 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 7 VG Access read/write VG Status resizable MAX LV 0 Cur LV 3 Open LV 3 Max PV 0 Cur PV 1 Act PV 1 VG Size 19.88 GB PE Size 32.00 MB Total PE 636 Alloc PE / Size 636 / 19.88 GB Free PE / Size 0 / 0 VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH [root@sqing ~]# mkdir /home/data [root@sqing ~]# mount /dev/VolGroup00/lv_data /home/data [root@sqing ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-lv_root 9.7G 926M 8.3G 10% / /dev/sda1 99M 13M 82M 13% /boot tmpfs 506M 0 506M 0% /dev/shm /dev/mapper/VolGroup00-lv_data 5.8G 140M 5.4G 3% /home/data 四、假設,為了方便管理,要在lv_data邏輯卷上擠出一部分空間(只留3G),用來新建一個邏輯卷lv_develop.那麼,需要先縮小lv_data,然後再建lv_develop.出於數據安全的考慮,宜將邏輯卷從目錄中卸載下來,再進行大小調整. 這裡為了驗證測試,在卸載之前,先在/home/data目錄下創建一些文件. [root@sqing ~]# cd /home/data/ [root@sqing data]# touch 001.txt 002.txt 003.txt [root@sqing data]# ls #卸載lv_data后,/home/data目錄下有數據 001.txt 002.txt 003.txt lost found [root@sqing data]# cd [root@sqing ~]# umount /home/data/ [root@sqing ~]# ls /home/data/ #卸載lv_data后,/home/data目錄為空 [root@sqing ~]# 縮小邏輯卷lv_data 步驟:用e2fsck命令檢查分區,再更改文件系統大小,縮小邏輯卷大小 [root@sqing ~]# e2fsck -f /dev/VolGroup00/lv_data e2fsck 1.39 (29-May-2006) 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/VolGroup00/lv_data: 14/770048 files (7.1% non-contiguous), 59982/1540096 blocks [root@sqing ~]# resize2fs -p /dev/VolGroup00/lv_data resize2fs 1.39 (29-May-2006) The filesystem is already 1540096 blocks long. Nothing to do! [root@sqing ~]# lvresize -L 3G /dev/VolGroup00/lv_data WARNING: Reducing active logical volume to 3.00 GB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv_data? [y/n]: y Reducing logical volume lv_data to 3.00 GB Logical volume lv_data successfully resized 查看此時的vg信息,發現已經騰出了2.88G的空間. [root@sqing ~]# vgdisplay --- Volume group --- VG Name VolGroup00 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 8 VG Access read/write VG Status resizable MAX LV 0 Cur LV 3 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 19.88 GB PE Size 32.00 MB Total PE 636 Alloc PE / Size 544 / 17.00 GB Free PE / Size 92 / 2.88 GB VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH 此時的lv_data邏輯卷大小為3G. [root@sqing ~]# lvscan ACTIVE '/dev/VolGroup00/lv_root' [10.00 GB] inherit ACTIVE '/dev/VolGroup00/lv_swap' [4.00 GB] inherit ACTIVE '/dev/VolGroup00/lv_data' [3.00 GB] inherit 新建邏輯卷lv_develop 步驟:創建新分區;接著格式化新分區. (註:吸收上面的教訓,邏輯卷的大小不要一下子設為2.88G,而是要小一些,這裡設為2.8G) [root@sqing ~]# lvcreate -L 2.8G -n lv_develop /dev/VolGroup00 Rounding up size to full physical extent 2.81 GB Logical volume "lv_develop" created [root@sqing ~]# mkfs.ext3 /dev/VolGroup00/lv_develop mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 368736 inodes, 737280 blocks 36864 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=754974720 23 block groups 32768 blocks per group, 32768 fragments per group 16032 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 33 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. Lv_develop創建好了,大小了2.81G. [root@sqing ~]# lvscan ACTIVE '/dev/VolGroup00/lv_root' [10.00 GB] inherit ACTIVE '/dev/VolGroup00/lv_swap' [4.00 GB] inherit ACTIVE '/dev/VolGroup00/lv_data' [3.00 GB] inherit ACTIVE '/dev/VolGroup00/lv_develop' [2.81 GB] inherit 查看此時的vg信息,發現還有64M的剩餘空間. [root@sqing ~]# vgdisplay --- Volume group --- VG Name VolGroup00 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 9 VG Access read/write VG Status resizable MAX LV 0 Cur LV 4 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 19.88 GB PE Size 32.00 MB Total PE 636 Alloc PE / Size 634 / 19.81 GB Free PE / Size 2 / 64.00 MB VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH 為了充分利用空間資源,將64M空閑空間加入到lv_develop中, 擴大邏輯卷lv_develop 步驟:先擴大邏輯分區大小,再更改文件系統以使其擴容 [root@sqing ~]# lvresize -L 64M /dev/VolGroup00/lv_d #增加64M,注意前面有個加號 lv_data lv_develop [root@sqing ~]# lvresize -L 64M /dev/VolGroup00/lv_develop Extending logical volume lv_develop to 2.88 GB Logical volume lv_develop successfully resized [root@sqing ~]# resize2fs -p /dev/VolGroup00/lv_develop resize2fs 1.39 (29-May-2006) Resizing the filesystem on /dev/VolGroup00/lv_develop to 753664 (4k) blocks. The filesystem on /dev/VolGroup00/lv_develop is now 753664 blocks long. [root@sqing ~]# lvscan ACTIVE '/dev/VolGroup00/lv_root' [10.00 GB] inherit ACTIVE '/dev/VolGroup00/lv_swap' [4.00 GB] inherit ACTIVE '/dev/VolGroup00/lv_data' [3.00 GB] inherit ACTIVE '/dev/VolGroup00/lv_develop' [2.88 GB] inherit 查看此時的vg信息,發現剩餘空間全部用光了,很好. [root@sqing ~]# vgdisplay --- Volume group --- VG Name VolGroup00 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 10 VG Access read/write VG Status resizable MAX LV 0 Cur LV 4 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 19.88 GB PE Size 32.00 MB Total PE 636 Alloc PE / Size 636 / 19.88 GB Free PE / Size 0 / 0 VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH 將兩個邏輯卷掛載到指定目錄中. [root@sqing ~]# mkdir /home/develop [root@sqing ~]# mount /dev/VolGroup00/lv_data /home/data/ [root@sqing ~]# mount /dev/VolGroup00/lv_develop /home/develop/ 檢查一下lv_data中數據 (註:lv_data縮小后的空間大小,一定要大於縮小之前已用空間的大小) [root@sqing ~]# ls /home/data/ #/home/data目錄下的數據全都在 001.txt 002.txt 003.txt lost found [root@sqing ~]# ls /home/develop/ #/home/develop目錄為空是當然的 lost found 至此,LVM相關操作完畢. 為了使系統在重啟后自動掛載邏輯卷,需要掛載lv_data、lv_develop的信息寫入到/etc/fstab中,/etc/fstab修改後的內容如下: [root@sqing ~]# cat /etc/fstab /dev/VolGroup00/lv_root / ext3 defaults 1 1 LABEL=/boot /boot ext3 defaults 1 2 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/VolGroup00/lv_swap swap swap defaults 0 0 /dev/VolGroup00/lv_data /home/data ext3 defaults 0 0 /dev/VolGroup00/lv_develop /home/develop ext3 defaults 0 0 以上操作,一年前就已經學會了的,不過不常玩,便生疏了,如今重溫,撰寫此筆記. 實際工作中,會遇加硬碟的情況,要先創建分區(主分區、擴展分區)、格化、掛載,然後,才能使用以上命令進行LVM管理.

本文出自 「一樹清勁」 博客,請務必保留此出處http://sunshyfangtian.blog.51cto.com/1405751/860018


[火星人 ] CentOS 5 LVM邏輯卷管理已經有923次圍觀

http://coctec.com/docs/linux/show-post-45885.html