Linux 磁碟組劃分詳細介紹

Michael_DD發表於2015-09-23
Linux 磁碟組劃分詳細介紹


基本概念:
磁碟、分割槽、物理卷【物理部分】
卷組【中間部分】
邏輯卷、檔案系統【虛擬化後可控制部分】

1. pv vg lv  物理  卷組 邏輯
pvdisplay
vgdisplay
lvdisplay
pvs
vgs
lvs

2.檢視檔案系統及其掛載點
# df -h
檔案系統              容量  已用  可用 已用%% 掛載點
/dev/sda3              26G   16G  8.7G  64% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/sda1             504M   37M  443M   8% /boot
/dev/sdb1             109G   69G   35G  67% /var/lib/mysql

檔案系統可以是邏輯卷,也可以直接為物理分割槽。
/dev/mapper/VolGroup-lv_root   邏輯卷
/dev/sdb1   物理分割槽

3.磁碟資訊檢視
# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes
64 heads, 32 sectors/track, 30720 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006dbeb

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2         513      524288   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             514        4609     4194304   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3            4610       30720    26737664   83  Linux
Partition 3 does not end on cylinder boundary.

Disk /dev/sdb: 118.1 GB, 118111600640 bytes
255 heads, 63 sectors/track, 14359 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x276e626e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       14359   115338636   83  Linux

兩個物理硬碟:
Disk /dev/sda:
Disk /dev/sdb:
除了顯示硬碟分割槽,同時顯示邏輯卷資訊,邏輯卷以vg_開頭

fdisk /dev/sdb 對物理磁碟進行分割槽。


案例操作:
擴容;/ 目錄增加8G

機器原本/目錄有22G,屬於邏輯卷。因此可以直接增加,如果是直接的物理磁碟,則不能在該目錄擴容
[root@mysqlsource]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol01
                       22G   16G  4.7G  77% /
tmpfs                 3.8G  224K  3.8G   1% /dev/shm
/dev/sda1             485M   34M  427M   8% /boot

新增加硬碟(/dev/sdb   16G):
[root@mysqlsource]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes
64 heads, 32 sectors/track, 30720 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008c51d
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2         501      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             502       30720    30944256   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
Disk /dev/mapper/VolGroup-LogVol01: 23.1 GB, 23089643520 bytes
255 heads, 63 sectors/track, 2807 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/VolGroup-LogVol00: 8594 MB, 8594128896 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb: 17.2 GB, 17179869184 bytes
64 heads, 32 sectors/track, 16384 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

可以對/dev/sdb 進行分割槽
fdisk /dev/sdb
看到新建分割槽sdb1
fdisk -l

然後對sdb1建立成物理卷,也可以直接把/dev/sdb建立成物理卷

建立物理卷:
[root@mysqlsource ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created

檢視卷組 VolGroup  
[root@mysqlsource ~]# vgs 
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   1   2   0 wz--n- 29.51g    0
  
新建的物理分割槽,分配到卷組VolGroup
[root@mysqlsource ~]# vgextend VolGroup /dev/sdb 
  Volume group "VolGroup" successfully extended

檢視需要新增的lv卷
[root@mysqlsource ~]# lvs
  LV       VG       Attr      LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  LogVol00 VolGroup -wi-ao---  8.00g                                             
  LogVol01 VolGroup -wi-ao--- 21.50g                                             

增加lv卷lvextend -L +[??]g /dev/[vg]/[lv] (加8G)
[root@mysqlsource ~]# lvextend -L +8G /dev/VolGroup/LogVol01
  Extending logical volume LogVol01 to 29.50 GiB
  Logical volume LogVol01 successfully resized

線上擴容 resize2fs /dev/[vg]/[lv] 
[root@mysqlsource ~]# resize2fs /dev/VolGroup/LogVol01
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/LogVol01 is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/VolGroup/LogVol01 to 7734272 (4k) blocks.
The filesystem on /dev/VolGroup/LogVol01 is now 7734272 blocks long.

空間已擴充套件為30G
[root@mysqlsource ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol01
                       30G   16G   13G  56% /
tmpfs                 3.8G  224K  3.8G   1% /dev/shm
/dev/sda1             485M   34M  427M   8% /boot

vgs可以看到VolGroup擴充套件了容量,VFreee是剩下可用容量,如果顯示8.00G,
[root@mysqlsource ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   2   2   0 wz--n- 45.50g 8.00g
[root@mysqlsource ~]# 

把剩餘8G加上
[root@mysqlsource ~]# lvextend -L +8G /dev/VolGroup/LogVol01
  Extending logical volume LogVol01 to 37.50 GiB
  Insufficient free space: 2048 extents needed, but only 2047 available
[root@mysqlsource ~]# 
[root@mysqlsource ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol01
                       30G   16G   13G  56% /
tmpfs                 3.8G  224K  3.8G   1% /dev/shm
/dev/sda1             485M   34M  427M   8% /boot
[root@mysqlsource ~]# resize2fs /dev/VolGroup/LogVol01
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 7734272 blocks long.  Nothing to do!


[root@mysqlsource ~]# lvextend -L +7G /dev/VolGroup/LogVol01
  Extending logical volume LogVol01 to 36.50 GiB
  Logical volume LogVol01 successfully resized
[root@mysqlsource ~]# resize2fs /dev/VolGroup/LogVol01
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/LogVol01 is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/VolGroup/LogVol01 to 9569280 (4k) blocks.
The filesystem on /dev/VolGroup/LogVol01 is now 9569280 blocks long.

[root@mysqlsource ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree   
  VolGroup   2   2   0 wz--n- 45.50g 1020.00m
[root@mysqlsource ~]# 


新建邏輯卷並且掛載磁碟

fdisk /dev/sdc 進行分割槽,分成一個/dev/sdbc1物理磁碟

pvcreate:
[root@mysqlsource ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created


pvremove:
[root@mysqlsource ~]# pvremove /dev/sdc1
  Labels on physical volume "/dev/sdc1" successfully wiped


vgcreate:
[root@mysqlsource ~]# vgcreate VolGroup1 /dev/sdc1
  Volume group "VolGroup1" successfully created
[root@mysqlsource ~]# vgs
  VG        #PV #LV #SN Attr   VSize    VFree   
  VolGroup    2   2   0 wz--n-   45.50g 1020.00m
  VolGroup1   1   0   0 wz--n- 1020.00m 1020.00m
[root@mysqlsource ~]# 

vgremove:
[root@mysqlsource ~]# vgremove VolGroup1
  Volume group "VolGroup1" successfully removed
[root@mysqlsource ~]# 

lvcreate:
[root@mysqlsource ~]# lvcreate -n LogVol02 -L 1020m VolGroup1
  Logical volume "LogVol02" created
[root@mysqlsource ~]# lvs
  LV       VG        Attr      LSize    Pool Origin Data%  Move Log Cpy%Sync Convert
  LogVol00 VolGroup  -wi-ao---    8.00g                                             
  LogVol01 VolGroup  -wi-ao---   36.50g                                             
  LogVol02 VolGroup1 -wi-a---- 1020.00m                                             
[root@mysqlsource ~]# 

lvremove:
[root@mysqlsource ~]# lvremove /dev/VolGroup1/LogVol02
Do you really want to remove active logical volume LogVol02? [y/n]: y
  Logical volume "LogVol02" successfully removed
[root@mysqlsource ~]#

[root@mysqlsource ~]# lvs
  LV       VG        Attr      LSize    Pool Origin Data%  Move Log Cpy%Sync Convert
  LogVol00 VolGroup  -wi-ao---    8.00g                                             
  LogVol01 VolGroup  -wi-ao---   36.50g                                             
  LogVol02 VolGroup1 -wi-a---- 1020.00m                                             

格式化檔案系統
[root@mysqlsource ~]# mkfs.ext4 /dev/VolGroup1/LogVol02
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
65280 inodes, 261120 blocks
13056 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

Writing inode tables: done                            
Creating journal (4096 blocks): 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.
[root@mysqlsource ~]# 
[root@mysqlsource ~]# 
[root@mysqlsource ~]# mkdir /test

掛載檔案系統
[root@mysqlsource ~]# mount /dev/VolGroup1/LogVol02 /test

檢視
[root@mysqlsource ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol01
                       36G   16G   19G  46% /
tmpfs                 3.8G  224K  3.8G   1% /dev/shm
/dev/sda1             485M   34M  427M   8% /boot
/dev/mapper/VolGroup1-LogVol02
                     1004M   18M  936M   2% /test
[root@mysqlsource ~]# 


/home的掛載裝置對應的邏輯卷非常大,但是/掛載的邏輯卷空間較小,需要減小/home邏輯卷,加大/對應的邏輯卷。

 umount /home【首先必須卸下掛載點,umount時相關裝置必須是空閒的,但是像/及/root有些系統服務無法被 終止,也就無法umount,因此像/ /root這些掛載點最好安裝的時候最好就有所規劃,但是我們可以讓他擴大還是沒有問題】

   在umount過程中如果有裝置佔用情況可使用

   fuser -m -k /home終止相關程式,再次umount /home成功

   df 看不到 /home掛載點

   必須先檢測裝置對映

   e2fsck -f /dev/mapper/vg_dbcentos6-lv_home 

   減小/home大小,原有4G,減小到1G

   resize2fs /dev/mapper/vg_dbcentos6-lv_home 1G

   重新掛載

   mount /dev/mapper/vg_dbcentos6-lv_home /home

   df -h 看到/home只剩1G了

   以上只是減少了檔案裝置的大小,還要進一步減小對應邏輯卷大小

   lvresize -L 1G /dev/mapper/vg_dbcentos6-lv_home【此過程會提示可能丟資料,如果有必要,請先備份

   /home下資料在做操作】

   lvresize -L +3GM /dev/mapper/vg_dbcentos6-lv_root【lv_root邏輯卷新增3G】

   resize2fs /dev/mapper/vg_dbcentos6-lv_root 【這個命令不帶引數,表示把多出來的容量都加到當前檔案系統】

   df -h【看到容量增加了】

   總結一下案例2的過程,先umount、接下來efsck -f、接下來resize2fs要減小的檔案系統、mount檔案系統、

   lvresize要減小的邏輯卷、lvresize要增大的邏輯卷、resize2fs要增大的檔案系統。






來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29500582/viewspace-1806993/,如需轉載,請註明出處,否則將追究法律責任。

相關文章