LVM是 Logical Volume Manager(邏輯卷管理)的簡寫,它是Linux環境下對磁盤分區進行管理的一種機制,它由Heinz Mauelshagen在Linux 2.4內核上實現。node
物理存儲介質(PhysicalStorageMedia)app
指系統的物理存儲設備:磁盤,如:/dev/hda、/dev/sda等,是存儲系統最底層的存儲單元。dom
物理卷(Physical Volume,PV)ide
指磁盤分區或從邏輯上與磁盤分區具備一樣功能的設備(如RAID),是LVM的基本存儲邏輯塊,但和基本的物理存儲介質(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數。ui
卷組(Volume Group,VG)spa
相似於非LVM系統中的物理磁盤,其由一個或多個物理卷PV組成。能夠在卷組上建立一個或多個LV(邏輯卷)。code
邏輯卷(Logical Volume,LV)orm
相似於非LVM系統中的磁盤分區,邏輯卷創建在卷組VG之上。在邏輯卷LV之上能夠創建文件系統(好比/home或者/usr等)。blog
pvdisplay #查看PV信息ip
[root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sda3 VG Name VolGroup PV Size 18.51 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 4738 Free PE 0 Allocated PE 4738 PV UUID ARNJxr-gfcH-rW5H-X6I1-c2da-QhM1-BJ7ljo
vgdisplay #查看VG信息
[root@localhost ~]# vgdisplay --- 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 18.51 GiB PE Size 4.00 MiB Total PE 4738 Alloc PE / Size 4738 / 18.51 GiB Free PE / Size 0 / 0 VG UUID gFkWoE-8AVZ-kbd5-Clge-6Tq3-gcFh-4iVr1x
lvdisplay #查看LV信息
[root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/VolGroup/LogVol01 LV Name LogVol01 VG Name VolGroup LV UUID UeAMMJ-G6Ou-yMlJ-9Igz-pVBU-1HSy-jt6Trt LV Write Access read/write LV Creation host, time localhost.localdomain, 2015-07-23 03:42:14 -0400 LV Status available # open 1 LV Size 4.05 GiB Current LE 1036 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/VolGroup/LogVol00 LV Name LogVol00 VG Name VolGroup LV UUID OF62jU-eVGO-4Rtd-TycU-aoGC-0Rs0-nDRRhE LV Write Access read/write LV Creation host, time localhost.localdomain, 2015-07-23 03:42:15 -0400 LV Status available # open 1 LV Size 14.46 GiB Current LE 3702 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
減小swap分區空間前要取消掛載,使用 swapoff -a
[root@localhost ~]# free -m total used free shared buffers cached Mem: 1877 140 1737 0 6 39 -/+ buffers/cache: 93 1783 Swap: 4143 0 4143 [root@localhost ~]# swapoff -a [root@localhost ~]# free -m total used free shared buffers cached Mem: 1877 136 1740 0 6 39 -/+ buffers/cache: 91 1786 Swap: 0 0 0
使用 lvreduce -L -xxx filename 或 lvreduce -L xxx filename 來減小swap分區空間,「-」表明減多少,不加「-」表明減剩多少。下圖表示給swap分區留512M。
使用 mke2fs filename 格式化分區
使用 resize2fs filename 重寫分區空間
[root@localhost ~]# lvreduce -L 512M /dev/VolGroup/LogVol01 WARNING: Reducing active and open logical volume to 512.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce LogVol01? [y/n]: y Reducing logical volume LogVol01 to 512.00 MiB Logical volume LogVol01 successfully resized [root@localhost ~]# mke2fs /dev/VolGroup/LogVol01 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 32768 inodes, 131072 blocks 6553 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=134217728 4 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304 Writing inode tables: done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 29 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# resize2fs /dev/VolGroup/LogVol01 resize2fs 1.41.12 (17-May-2010) The filesystem is already 131072 blocks long. Nothing to do!
使用 lvextend -L +1G filename 爲根分區增長空閒空間,用法同上。空閒空間能夠使用vgdisplay查看,增長空間不須要格式化。
[root@localhost ~]# lvextend -L +3.54G /dev/VolGroup/LogVol00 Rounding size to boundary between physical extents: 3.54 GiB Extending logical volume LogVol00 to 18.00 GiB Logical volume LogVol00 successfully resized [root@localhost ~]# resize2fs /dev/VolGroup/LogVol00 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/VolGroup/LogVol00 is mounted on /; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/VolGroup/LogVol00 to 4719616 (4k) blocks. The filesystem on /dev/VolGroup/LogVol00 is now 4719616 blocks long.
新建2G的swap文件,掛載到swap,添加到開機啓動項。
[root@localhost ~]# dd if=/dev/zero of=/www/swap bs=1024 count=2048000 2048000+0 records in 2048000+0 records out 2097152000 bytes (2.1 GB) copied, 12.0018 s, 175 MB/s [root@localhost ~]# mkswap /www/swap mkswap: /www/swap: warning: don't erase bootbits sectors on whole disk. Use -f to force. Setting up swapspace version 1, size = 2047996 KiB no label, UUID=92ab4ed5-f301-4ba4-970f-9e5600610522 [root@localhost ~]# swapon /www/swap [root@localhost ~]# free -m total used free shared buffers cached Mem: 1877 1796 80 0 7 1647 -/+ buffers/cache: 142 1735 Swap: 1999 0 1999 [root@localhost ~]# swapon -s Filename Type Size Used Priority /www/swap file 2047992 0 -1
修改啓動項
編輯 /etc/fstab文件,去掉以前的swap分區掛載,在最末行增長swap文件的掛載
[root@localhost ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Thu Jul 23 03:45:08 2015 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/VolGroup-LogVol00 / ext4 defaults 1 1 UUID=4221a802-6ce2-4638-a3a5-7c17b961f291 /boot ext4 defaults 1 2 UUID=b720b68f-37b7-45ce-b4a2-40deeb9fa90f /www 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 /www/swap swap swap defaults 0 0
重啓後能夠看到,根分區空間有增長, swap使用的是swap文件
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-LogVol00 18G 716M 17G 5% / tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 485M 32M 429M 7% /boot /dev/sda2 61G 2.2G 55G 4% /www [root@localhost ~]# free -m total used free shared buffers cached Mem: 1877 135 1741 0 7 36 -/+ buffers/cache: 91 1786 Swap: 1999 0 1999 [root@localhost ~]# swapon -s Filename Type Size Used Priority /www/swap file 2047992 0 -1
注:以前有實驗過直接刪掉swap的LV,用文件代替,可是,刪除swap的LV後,重啓報錯如圖
按網上的解決辦法,開機按「e」加「enforcing=0」神馬的無論用。這個問題暫時木有解決,有解決過的老師,請告訴下學生。