LVM(Logical VolumeManager 邏輯卷管理)
node
在磁盤分區和文件系統之間添加一個邏輯層,來爲文件系統屏蔽下層磁盤分區佈局,提供一個抽象的盤卷,在盤捲上創建文件系統,來提升磁盤分區管理的靈活性可實現零停機前提下自如對文件系統的大小進行調整,能夠方便實現文件系統跨越不一樣磁盤和分區。linux
下圖是我根據本身的理解畫出的一張圖。此圖背景是:公司有一臺戴爾R910服務器,如今接上6個SAS硬盤,作成raid5磁盤陣列,最後一個硬盤設爲全局熱備。而後在戴爾自帶的磁盤陣列管理上把剩下的5塊硬盤劃分爲4個VD(虛擬磁盤),把系統所有安裝在第一個VD,而後剩下的3個VD利用LVM技術整合成一個大分區用於存儲數據。服務器
下面分享的我建立LV的整個過程,此操做系統爲 Red hat enterprise linux 5.5 x86_64 : app
------------------------------------- 下面是對VD磁盤的分區管理 ----------------------------------------------------ide
[root@mail ~]# ls /dev/sd* #查看當前系統有多少分區佈局
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb /dev/sdc /dev/sddui
[root@mail ~]# fdisk /dev/sdb #對第二塊硬盤進行分區this
Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabelspa
Building a new DOS disklabel. Changes willremain in memory only,操作系統
until you decide to write them. After that,of course, the previous
content won't be recoverable.
The number of cylinders for this disk isset to 248029.
There is nothing wrong with that, but thisis larger than 1024,
and could in certain setups cause problemswith:
1) software that runs at boot time (e.g.,old versions of LILO)
2) booting and partitioning software fromother OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partitiontable 4 will be corrected by w(rite)
Command (m for help): m #列出全部幫助信息
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibilityflag
d delete a partition #刪除分區
l list known partition types #列出已知的分區類型
m print this menu
n add a new partition #建立新分區
o create a new empty DOSpartition table
p print the partition table #在屏幕打印出全部的分區
q quit without saving changes #退出不保存
s create a new empty Sundisklabel
t change a partition's system id #更改分區的系統ID
u change display/entry units
v verify the partition table
w write table to disk and exit #把當前配置信息寫入硬盤並退出
x extra functionality (expertsonly)
Command (m for help): n #建立新分區
Command action
e extended #建立邏輯分區 [ 5,6,7…… ]
p primary partition (1-4) #建立主分區[ 1,2,3,4 ]
p #建立主分區
Partition number (1-4): 1 #建立第一塊主分區
First cylinder (1-248029, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK(1-248029, default 248029):
Using default value 248029
Command (m for help): l #列出已知的分區類型
0 Empty 1e Hidden W95 FAT1 80 Old Minix bf Solaris
1 FAT12 24 NEC DOS 81 Minix / old Lin c1 DRDOS/sec(FAT-
2 XENIXroot 39 Plan 9 82 Linux swap / So c4 DRDOS/sec(FAT-
3 XENIXusr 3c PartitionMagic 83 Linux c6 DRDOS/sec (FAT-
4 FAT16<32M 40 Venix 80286 84 OS/2 hidden C: c7 Syrinx
5 Extended 41 PPC PReP Boot 85 Linux extended da Non-FS data
6 FAT16 42 SFS 86 NTFS volume set db CP/M / CTOS /.
7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set de DellUtility
8 AIX 4e QNX4.x 2nd part 88 Linux plaintext df BootIt
9 AIXbootable 4f QNX4.x 3rd part 8e Linux LVM e1 DOS access
a OS/2Boot Manag 50 OnTrack DM 93 Amoeba e3 DOS R/O
b W95FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e4 SpeedStor
c W95FAT32 (LBA) 52 CP/M 9f BSD/OS eb BeOS fs
e W95FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi ee EFI GPT
f W95Ext'd (LBA) 54 OnTrackDM6 a5 FreeBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a6 OpenBSD f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f1 SpeedStor
12 Compaq diagnost 5c PriamEdisk a8 Darwin UFS f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot fb VMware VMFS
17 Hidden HPFS/NTF 64 NovellNetware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecureMult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
Command (m for help): t #更改分區的系統ID
Selected partition 1 #選擇第一個分區
Hex code (type L to list codes): 8e #默認是linux格式,更改成Linux LVM
Changed system type of partition 1 to 8e(Linux LVM)
Command (m for help): p #在屏幕打印出全部的分區
Disk /dev/sdb: 2040.1 GB, 2040109137920bytes
255 heads, 63 sectors/track, 248029cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 248029 1992292911 8e LinuxLVM
Command (m for help): w #把當前配置信息寫入硬盤並退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@mail ~]# fdisk /dev/sdc #對第三塊硬盤進行分區
Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes willremain in memory only,
until you decide to write them. After that,of course, the previous
content won't be recoverable.
The number of cylinders for this disk isset to 248029.
There is nothing wrong with that, but thisis larger than 1024,
and could in certain setups cause problemswith:
1) software that runs at boot time (e.g.,old versions of LILO)
2) booting and partitioning software fromother OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partitiontable 4 will be corrected by w(rite)
Command (m for help): m #列出全部幫助信息
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibilityflag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOSpartition table
p print the partition table
q quit without saving changes
s create a new empty Sundisklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (expertsonly)
Command (m for help): n #建立新分區
Command action
e extended
p primary partition (1-4)
p #建立主分區
Partition number (1-4): 1 #建立第一塊主分區
First cylinder (1-248029, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK(1-248029, default 248029):
Using default value 248029
Command (m for help): t #更改分區的系統ID
Selected partition 1 #選擇第一個分區
Hex code (type L to list codes): 8e #默認是linux格式,更改成Linux LVM
Changed system type of partition 1 to 8e(Linux LVM)
Command (m for help): w #把當前配置信息寫入硬盤並退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@mail ~]# fdisk /dev/sdd #對第四塊硬盤進行分區
Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes willremain in memory only,
until you decide to write them. After that,of course, the previous
content won't be recoverable.
The number of cylinders for this disk isset to 191145.
There is nothing wrong with that, but thisis larger than 1024,
and could in certain setups cause problemswith:
1) software that runs at boot time (e.g.,old versions of LILO)
2) booting and partitioning software fromother OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partitiontable 4 will be corrected by w(rite)
Command (m for help): n #建立分區
Command action
e extended
p primary partition (1-4)
p #建立主分區
Partition number (1-4): 1 #建立第一塊主分區
First cylinder (1-191145, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK(1-191145, default 191145):
Using default value 191145
Command (m for help): t #更改分區的系統ID
Selected partition 1 #選擇第一個分區
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e(Linux LVM)
Command (m for help): w #把當前配置信息寫入硬盤並退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
-------------------------------------------- 如下是建立LV部分 -------------------------------------------------
[root@mail ~]# ls /dev/sd* #查看當前系統有多少分區,能夠看到sdb,sdc,sdd這三塊硬盤都是分紅一個分區
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb /dev/sdb1 /dev/sdc /dev/sdc1 /dev/sdd /dev/sdd1
[root@mail ~]# pvcreate /dev/sdb1 #sdb1轉換成physical volume【pv就是物理卷的意思】
Physical volume "/dev/sdb1" successfully created
[root@mail ~]# pvcreate /dev/sdc1 #sdc1轉換成physical volume
Physical volume "/dev/sdc1" successfully created
[root@mail ~]# pvcreate /dev/sdd1 #sdd1轉換成physical volume
Physical volume "/dev/sdd1" successfully created
[root@mail ~]# pvs #顯示當前系統全部的物理卷physical volume
PV VG Fmt Attr PSize PFree
/dev/sda1 VolGroup_ID_20054 lvm2a- 9.53G 9.53G
/dev/sda3 VolGroup_ID_20054 lvm2a- 590.25G 54.50G
/dev/sdb1 lvm2 -- 1.86T 1.86T
/dev/sdc1 lvm2-- 1.86T 1.86T
/dev/sdd1 lvm2-- 1.43T 1.43T
[root@mail~]# vgcreate vg_de /dev/ sdb1 #建立卷組vg_de,並把sdb1加進來
Nophysical volume label read from /dev/sdb1
Physical volume "/dev/sdb1" successfully created
Volume group "vg_de" successfully created
[root@mail ~]# vgextend vg_de /dev/sdc1 #擴展卷組,把sdc1加進來
Nophysical volume label read from /dev/sdc1
Physical volume "/dev/sdc1" successfully created
Volume group "vg_de" successfully extended
[root@mail ~]# vgextend vg_de /dev/sdd1 #擴展卷組,把sdd1加進來
Nophysical volume label read from /dev/sdd1
Physical volume "/dev/sdd1" successfully created
Volume group "vg_de" successfully extended
[root@mail ~]# vgs #顯示系統全部卷組
VG #PV #LV #SNAttr VSize VFree
VolGroup_ID_20054 2 6 0wz--n- 599.78G 64.03G
vg_de 3 0 0wz--n- 5.14T 5.14T
[root@mail ~]# lvcreate -L 5.14T -n lv_devg_de #建立邏輯卷lv_de,並把卷組vg _de的空間資源所有分給其使用
Rounding up size to full physical extent 5.14 TB
Logical volume "lv_de" created
[root@mail ~]# lvs #顯示系統全部邏輯卷
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
LogVol0 VolGroup_ID_20054-wi-ao 4.00G
LogVol1 VolGroup_ID_20054-wi-ao 55.94G
LogVol2 VolGroup_ID_20054-wi-ao 9.88G
LogVol4 VolGroup_ID_20054-wi-ao 74.38G
LogVol5 VolGroup_ID_20054-wi-ao 268.00G
LogVolHome VolGroup_ID_20054 -wi-ao 123.56G
lv_de vg_de -wi-a- 5.14T
[root@mail ~]# mkfs -t ext3 /dev/vg_de/lv_de #把lv_de格式化爲ext3格式
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
689881088 inodes, 1379759104 blocks
68987955 blocks (5.00%) reserved for thesuper user
First data block=0
Maximum filesystem blocks=4294967296
42107 block groups
32768 blocks per group, 32768 fragments pergroup
16384 inodes per group
Superblock backups stored on blocks:
32768,98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000,7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000,214990848, 512000000, 550731776, 644972544
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystemaccounting information: done
This filesystem will be automaticallychecked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@mail ~]# mount /dev/vg_de/lv_de /local/ #把lv_de邏輯卷掛載到 /local/ 使用
[root@mail ~]# df –h #以易看懂的方式顯示當前全部掛載點空餘空間,df是disk free的簡稱
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/VolGroup_ID_20054-LogVol1
55G 465M 51G 1% /
/dev/mapper/VolGroup_ID_20054-LogVol2
9.6G 151M 9.0G 2% /tmp
/dev/mapper/VolGroup_ID_20054-LogVol4
73G 243M 69G 1% /var
/dev/mapper/VolGroup_ID_20054-LogVol5
260G 2.1G 245G 1% /usr
/dev/sda2 190M 16M 165M 9% /boot
tmpfs 7.9G 0 7.9G 0% /dev/shm
/dev/mapper/VolGroup_ID_20054-LogVolHome
122G 219M 116G 1% /home
/dev/mapper/vg_de-lv_de
5.1T 188M 4.9T 1% /local
[root@mail ~]# vi /etc/fstab #編輯磁盤開機掛載文件,使lv_de邏輯卷掛載到 /local/ 這個效果永久生效。對於Linux而言,一切沒有寫入文件的操做,關機重啓則無效
/dev/VolGroup_ID_20054/LogVol1 / ext3 defaults 1 1
/dev/VolGroup_ID_20054/LogVol2 /tmp ext3 defaults 1 2
/dev/VolGroup_ID_20054/LogVol4 /var ext3 defaults 1 2
/dev/VolGroup_ID_20054/LogVol5 /usr ext3 defaults 1 2
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/VolGroup_ID_20054/LogVol0 swap swap defaults 0 0
/dev/VolGroup_ID_20054/LogVolHome /home ext3 defaults 1 2
/dev/vg_de/lv_de /local ext3 defaults 0 2
[root@mail ~]# reboot
-----------
題外話:做者本人當時編輯fstab文件的時候,加載LV磁盤的有些地方沒注意寫錯了,致使系統一重啓就找不到LV磁盤,進入到了單用戶維護模式,而此時 / 是隻讀的,根本沒辦法把有錯的地方改過來。後來發現能夠用 mount -n -o remount,rw / 的命令來從新掛載 / ,而後就能夠修改文件啦,這個命令頗有用,不管你是由於什麼緣由進到這樣的一個維護模式,均可以用這樣的命令,而後獲取對磁盤的寫入權限。