1. 程式人生 > >Linux: LVM 建立

Linux: LVM 建立

關於Linux 的邏輯卷知識,以前前接觸過一些,今天再次溫習下,主要是演練下 LVM
的建立過程及維護命令,LVM 理論部分和虛擬機器增加硬碟部分略。


--1 環境準備
虛擬機器:Red Hat Enterprise Linux Server release 5.5
增加兩塊 IDE 硬碟,一塊為 2GB,另一塊為 1GB。
 

--2 檢視新增硬碟情況
Disk /dev/hda: 19.3 GB, 19327352832 bytes
255 heads, 63 sectors/track, 2349 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14         144     1052257+  82  Linux swap / Solaris
/dev/hda3             145        2349    17711662+  83  Linux

Disk /dev/hdb: 2147 MB, 2147483648 bytes
16 heads, 63 sectors/track, 4161 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1        1985     1000408+  83  Linux
/dev/hdb2            1986        4161     1096704   83  Linux

Disk /dev/hdd: 1073 MB, 1073741824 bytes
16 heads, 63 sectors/track, 2080 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdd1               1        2080     1048288+  83  Linux

  備註: /dev/hdb, /dev/hdd 就是新增的盤, /dev/hdb 為 2147 MB,/dev/hdd
              為 1073 MB。 


--3  建立PV ( pvcreat )
[[email protected] ~]# pvcreate /dev/hdb1 /dev/hdd1
  /dev/cdrom: open failed: Read-only file system
  Attempt to close device '/dev/cdrom' which is not open.
  Physical volume "/dev/hdb1" successfully created
  /dev/cdrom: open failed: Read-only file system
  Attempt to close device '/dev/cdrom' which is not open.
  Physical volume "/dev/hdd1" successfully created

  備註:在建立 VG 之前,首先要將物理裝置標識成 LVM 可識別的物理裝置,另外
             pvcreate 命令會清除裝置上的所有資料。

--4  檢視 PV 資訊
[[email protected] ~]# pvscan
  PV /dev/hdb1                      lvm2 [976.96 MB]
  PV /dev/hdd1                      lvm2 [1023.72 MB]
  Total: 2 [1.95 GB] / in use: 0 [0   ] / in no VG: 2 [1.95 GB]
 
  備註:上面顯示 /dev/hdb1, /dev/hdd1 已經建立成 PV 了。
 

--5 建立VG ( vgcreate )
[[email protected] ~]# vgcreate vg01_pgdata /dev/hdb1 /dev/hdd1
  /dev/cdrom: open failed: Read-only file system
  /dev/cdrom: open failed: Read-only file system
  Attempt to close device '/dev/cdrom' which is not open.
  /dev/cdrom: open failed: Read-only file system
  Attempt to close device '/dev/cdrom' which is not open.
  /dev/cdrom: open failed: Read-only file system
  Attempt to close device '/dev/cdrom' which is not open.
  Volume group "vg01_pgdata" successfully created
 
 
--6 顯示 VG 資訊
[[email protected] ~]# vgdisplay
  --- Volume group ---
  VG Name               vg01_pgdata
  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               1.95 GB
  PE Size               4.00 MB
  Total PE              499
  Alloc PE / Size       0 / 0  
  Free  PE / Size       499 / 1.95 GB
  VG UUID               B5pg8R-2AGm-6DEp-K7HK-TI1I-HC3h-gWx32m
 
   備註: 邏輯卷 vg01_pgdata  建立成功,大小為 1.95 GB。
  
--7 建立LV
[[email protected] lvm]# lvcreate -L 1G -n lv_pgdata1  vg01_pgdata
  Logical volume "lv_pgdata1" created
 
 
--8 掃描 lv 邏輯單元
[[email protected] lvm]# lvscan
  ACTIVE            '/dev/vg01_pgdata/lv_pgdata1' [1.00 GB] inherit
 
 
--9 檢視LV資訊 
[[email protected] lvm]# lvdisplay  /dev/vg01_pgdata/lv_pgdata1
  --- Logical volume ---
  LV Name                /dev/vg01_pgdata/lv_pgdata1
  VG Name                vg01_pgdata
  LV UUID                pXsfbX-MAdM-6FQ5-cUz6-IKXC-hRJM-sNQ9yp
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                1.00 GB
  Current LE             256
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0 
 
  備註:LV建立成功,大小為 1 GB,接下來可以格式化並掛載 LV 了。
   
   
--10 格式化檔案系統
[[email protected] lvm]# mkfs -t ext3 -c /dev/vg01_pgdata/lv_pgdata1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
131072 inodes, 262144 blocks
13107 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
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

Checking for bad blocks (read-only test): done                               
Writing inode tables: done                           
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.


--11 目錄掛載   
[[email protected] lvm]# mkdir -p /database/pgdata1

[[email protected] lvm]# mount -t ext3 /dev/vg01_pgdata/lv_pgdata1 /database/pgdata1

[[email protected] lvm]# chown -R postgres:postgres /database/pgdata1

[[email protected] lvm]# df -hv
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda3              17G  9.8G  5.8G  64% /
/dev/hda1              99M   18M   76M  20% /boot
tmpfs                 217M     0  217M   0% /dev/shm
none                  217M  104K  217M   1% /var/lib/xenstored
/dev/mapper/vg01_pgdata-lv_pgdata1
                     1008M   34M  924M   4% /database/pgdata1
                    
   備註:到了這步, LVM 配置成功,目錄掛載成功。
                     
                    
--12 設定開機自動掛載
修改檔案 /etc/fstab,增加以下行
/dev/vg01_pgdata/lv_pgdata1 /database/pgdata1   ext3     defaults   0   0                    


--13 再次檢視 VG
[[email protected] lvm]# vgdisplay
  --- Volume group ---
  VG Name               vg01_pgdata
  System ID            
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               1.95 GB
  PE Size               4.00 MB
  Total PE              499
  Alloc PE / Size       256 / 1.00 GB
  Free  PE / Size       243 / 972.00 MB
  VG UUID               B5pg8R-2AGm-6DEp-K7HK-TI1I-HC3h-gWx32m
 
  備註:分配了(Alloc PE) 1GB,還剩餘 (Free  PE) 972 MB。  
 
 
--14 常見 LVM 命令

 

 

  功能

PV

VG

LV

 搜尋

pvscan

vgscan

lvscan

 建立

pvcreate

vgcreate

lvcreate

 列出

pvdisplay

vgdisplay

lvdisplay

 增加

 

vgextend

lvextend

 減少

 

vgreduce

       lvreduce

 刪除

 

vgremove

lvremove

 

   轉載http://francs3.blog.163.com/blog/static/40576727201212242741910/