Linux的邏輯卷管理

BTxigua發表於2008-03-18

在Linux的邏輯卷管理,下面的步驟包括新建一個邏輯卷以及怎樣增加邏輯卷的大小。
1、新掛載一個硬碟裝置
使用fdisk -l檢視裝置名稱
在這裡,我們使用的是 /dev/sdc

2、使用fdisk建立LVM分割槽
[root@localhost etc]# fdisk /dev/sdc

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
p
Partition number (1-4): 1
Partition 1 is already defined. Delete it before re-adding it.


Command (m for help): d
Selected partition 1

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
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 (experts only)

Command (m for help): d /dev/sdc1 --因為這裡原來就使用了sdc裝置,所以需要先刪除
No partition is defined yet!

Command (m for help): p

Disk /dev/sdc: 1073 MB, 1073741824 bytes
64 heads, 32 sectors/track, 1024 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Device Boot Start End Blocks Id System

Command (m for help): n
Command action
e extended
p primary partition (1-4) --在這裡選擇p,選擇擴充套件將無法轉換lvm格式
p
Partition number (1-4): 1
First cylinder (1-1024, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1024, default 1024):
Using default value 1024

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e --8e即表示為lvm
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdc: 1073 MB, 1073741824 bytes
64 heads, 32 sectors/track, 1024 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 1024 1048560 8e Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


3、建立物理卷pv
[root@localhost etc]# 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 509 4088511 83 Linux
/dev/sda2 637 2610 15856155 83 Linux
/dev/sda3 510 636 1020127+ 82 Linux swap

Partition table entries are not in disk order

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 391 3140676 8e Linux LVM

Disk /dev/sdc: 1073 MB, 1073741824 bytes
64 heads, 32 sectors/track, 1024 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 1024 1048560 8e Linux LVM
[root@localhost etc]# pvcreate /dev/sdc1
pvcreate -- ERROR: "/etc/lvmtab" doesn't exist; please run vgscan

[root@localhost etc]# vgscan --需要先執行vgscan,建立/etc/lvmtab和/etc/lvmtab.d
vgscan -- reading all physical volumes (this may take a while...)
vgscan -- "/etc/lvmtab" and "/etc/lvmtab.d" successfully created
vgscan -- WARNING: This program does not do a VGDA backup of your volume group

[root@localhost etc]# pvcreate /dev/sdc1
pvcreate -- physical volume "/dev/sdc1" successfully created

[root@localhost etc]# vgcreate testfg /dev/sdc1
vgcreate -- INFO: using default physical extent size 4 MB
vgcreate -- INFO: maximum logical volume size is 255.99 Gigabyte
vgcreate -- doing automatic backup of volume group "testfg"
vgcreate -- volume group "testfg" successfully created and activated

4、建立卷組
[root@localhost etc]# vgcreate testfg /dev/sdc1
vgcreate -- INFO: using default physical extent size 4 MB
vgcreate -- INFO: maximum logical volume size is 255.99 Gigabyte
vgcreate -- doing automatic backup of volume group "testfg"
vgcreate -- volume group "testfg" successfully created and activated
--這裡預設的PE大小為4M,PE的大小也決定邏輯卷的最大容量。這裡是255.99 Gigabyte,如果希望獲得更大的邏輯卷容量,可以更改PE的大小。可以使用-s引數指定PE的大小,PE的取值範圍為8 KB 到 512 MB。

5、啟用卷組
[root@localhost etc]# vgchange -a y testvg
vgchange -- volume group "testvg" does not exist

[root@localhost etc]# vgchange -a y testfg
vgchange -- volume group "testfg" already active


6、建立邏輯卷
[root@localhost etc]# vgdisplay testfg
--- Volume group ---
VG Name testfg
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 0
Open LV 0
MAX LV Size 255.99 GB
Max PV 256
Cur PV 1
Act PV 1
VG Size 1016 MB
PE Size 4 MB
Total PE 254
Alloc PE / Size 0 / 0
Free PE / Size 254 / 1016 MB
VG UUID EYvilo-4nl3-CC5r-NkEh-7AGJ-oQn4-wfZeC0


[root@localhost etc]# lvcreate -L254 -nlvm1 testfg --L引數指定的是PE的數量
lvcreate -- rounding size up to physical extent boundary
lvcreate -- doing automatic backup of "testfg"
lvcreate -- logical volume "/dev/testfg/lvm1" successfully created


7、建立檔案系統
[root@localhost etc]# mkfs -t ext2 /dev/testfg/lvm1
mke2fs 1.32 (09-Nov-2002)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=1
32 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

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

8、掛載檔案系統
[root@localhost home]# mkdir ibm
[root@localhost home]# mount /dev/testfg/lvm1 /home/ibm
[root@localhost home]# df -k
檔案系統 1K-塊 已用 可用 已用% 掛載點
/dev/sda1 4024188 2293400 1526364 61% /
/dev/sda2 15607296 3299848 11514644 23% /home
none 115352 0 115352 0% /dev/shm
/dev/testfg/lvm1 253871 13 240751 1% /home/ibm


9、新增一個新的磁碟
比如我們在這裡新新增了一個磁碟/dev/sdb。需要將該磁碟增加到卷組/dev/testfg/lvm1中。
按照前面的步驟,將該磁碟建立稱謂一個物理卷 /dev/sdb1,然後將該物理卷新增到卷組中:

[root@localhost home]# fdisk /dev/sdb --格式化為lvm裝置
[root@localhost home]# pvcreate /dev/sdb1
pvcreate -- physical volume "/dev/sdb1" successfully created

[root@localhost home]# vgextend testfg /dev/sdb1
vgextend -- INFO: maximum logical volume size is 255.99 Gigabyte
vgextend -- doing automatic backup of volume group "testfg"
vgextend -- volume group "testfg" successfully extended

10、刪除一個物理卷
[root@localhost home]# pvdisplay /dev/sdb1
--- Physical volume ---
PV Name /dev/sdb1
VG Name testfg
PV Size 3 GB [6281352 secs] / NOT usable 4.19 MB [LVM: 130 KB]
PV# 2
PV Status NOT available
Allocatable yes
Cur LV 0
PE Size (KByte) 4096
Total PE 765
Free PE 765
Allocated PE 0
PV UUID LsTOGX-bnhO-uWqj-ivsc-fY98-3CrN-0i7vbK
--在這裡,需要確認該pv沒有正在被使用

[root@localhost home]# vgreduce /dev/testfg/lvm1 /dev/sdb1
vgreduce -- ERROR: invalid volume group name "testfg/lvm1"

[root@localhost home]# vgreduce testfg /dev/sdb1
vgreduce -- doing automatic backup of volume group "testfg"
vgreduce -- volume group "testfg" successfully reduced by physical volume:
vgreduce -- /dev/sdb1

在pv /dev/sdb1被刪除之前,我們看到testfg的情況是:
[root@localhost home]# vgdisplay testfg
--- Volume group ---
VG Name testfg
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 1
Open LV 1
MAX LV Size 255.99 GB
Max PV 256
Cur PV 2
Act PV 2
VG Size 3.98 GB
PE Size 4 MB
Total PE 1019
Alloc PE / Size 64 / 256 MB
Free PE / Size 955 / 3.73 GB
VG UUID EYvilo-4nl3-CC5r-NkEh-7AGJ-oQn4-wfZeC0
在pv /dev/sdb1被刪除之後,我們看到testfg的容量變化
[root@localhost home]# vgdisplay testfg
--- Volume group ---
VG Name testfg
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 1
Open LV 1
MAX LV Size 255.99 GB
Max PV 256
Cur PV 1
Act PV 1
VG Size 1016 MB
PE Size 4 MB
Total PE 254
Alloc PE / Size 64 / 256 MB
Free PE / Size 190 / 760 MB
VG UUID EYvilo-4nl3-CC5r-NkEh-7AGJ-oQn4-wfZeC0:


11、擴充套件邏輯卷的大小
在10中,我們看到是通過新增一個pv或者刪除一個pv來增加或這刪除lv的大小。現在我們使用lvextend和lvreduce來增加和減小lv的大小。
[root@localhost home]# lvextend -L+4m /dev/testfg/lvm1
lvextend -- extending logical volume "/dev/testfg/lvm1" to 260 MB
lvextend -- doing automatic backup of volume group "testfg"
lvextend -- logical volume "/dev/testfg/lvm1" successfully extended
--如果在這裡你增加的不是PE的倍數,系統會自動round到一個PE的倍數大小。

[root@localhost home]# lvreduce -L-4m /dev/testfg/lvm1
lvreduce -- WARNING: reducing active and open logical volume to 256 MB
lvreduce -- THIS MAY DESTROY YOUR DATA (filesystem etc.)
lvreduce -- do you really want to reduce "/dev/testfg/lvm1"? [y/n]: y
lvreduce -- doing automatic backup of volume group "testfg"
lvreduce -- logical volume "/dev/testfg/lvm1" successfully reduced
在這裡,我們擴充套件或者刪除了邏輯卷的容量,但是在檔案系統上還是還是沒有識別出這部分空間的變化。可以使用resize2fs命令是更改被作業系統所識別.

如下所示,在lvextend增加了40m的邏輯卷空間。但是在作業系統上沒有識別出來。

[root@localhost root]# df -k
檔案系統 1K-塊 已用 可用 已用% 掛載點
/dev/sda1 4024188 2294624 1525140 61% /
/dev/sda2 15607296 3299848 11514644 23% /home
none 115352 0 115352 0% /dev/shm
/dev/testfg/lvm1 253871 13 240751 1% /home/bak
[root@localhost root]# lvextend -L+40m /dev/testfg/lvm1
lvextend -- extending logical volume "/dev/testfg/lvm1" to 340 MB--增加到340m空間了。
lvextend -- doing automatic backup of volume group "testfg"
lvextend -- logical volume "/dev/testfg/lvm1" successfully extended

[root@localhost root]# vgdisplay testfg
--- Volume group ---
VG Name testfg
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 1
Open LV 1
MAX LV Size 255.99 GB
Max PV 256
Cur PV 1
Act PV 1
VG Size 1016 MB
PE Size 4 MB
Total PE 254
Alloc PE / Size 85 / 340 MB --從vg上也可以看到分配了340m的空間。
Free PE / Size 169 / 676 MB
VG UUID EYvilo-4nl3-CC5r-NkEh-7AGJ-oQn4-wfZeC0


[root@localhost root]# df -k
檔案系統 1K-塊 已用 可用 已用% 掛載點
/dev/sda1 4024188 2294624 1525140 61% /
/dev/sda2 15607296 3299848 11514644 23% /home
none 115352 0 115352 0% /dev/shm
/dev/testfg/lvm1 253871 13 240751 1% /home/bak --但在作業系統上沒有識別出空間已經增長。
[root@localhost root]# resize2fs /dev/testfg/lvm1
resize2fs 1.32 (09-Nov-2002)
/dev/testfg/lvm1 is mounted; can't resize a mounted filesystem!

[root@localhost root]# umount /dev/testfg/lvm1
[root@localhost root]# resize2fs /dev/testfg/lvm1
resize2fs 1.32 (09-Nov-2002)
Please run 'e2fsck -f /dev/testfg/lvm1' first.

[root@localhost root]# e2fsck -f /dev/testfg/lvm1
e2fsck 1.32 (09-Nov-2002)
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/testfg/lvm1: 11/65536 files (0.0% non-contiguous), 8286/262144 blocks
[root@localhost root]# df -k
檔案系統 1K-塊 已用 可用 已用% 掛載點
/dev/sda1 4024188 2294624 1525140 61% /
/dev/sda2 15607296 3299848 11514644 23% /home
none 115352 0 115352 0% /dev/shm
[root@localhost root]# resize2fs /dev/testfg/lvm1
resize2fs 1.32 (09-Nov-2002)
The filesystem on /dev/testfg/lvm1 is now 348160 blocks long.

[root@localhost root]# mount /dev/testfg/lvm1 /home/bak
[root@localhost root]# df -k
檔案系統 1K-塊 已用 可用 已用% 掛載點
/dev/sda1 4024188 2294624 1525140 61% /
/dev/sda2 15607296 3299848 11514644 23% /home
none 115352 0 115352 0% /dev/shm
/dev/testfg/lvm1 337041 61 323054 1% /home/bak --已經識別出增長後的空間了。


12、掃尾工作
在我們新增了新的檔案系統之後,一般會需要在開機之後自動掛載。因此我們可以把掛載的檔案系統以及掛載目錄新增到/etc/fstab中。
在/etc/fstab增加一個條目
/dev/testfg/lvm1 /home/ibm ext2 default 0 0

再補充一下關於fstab的格式說明:

etc/fstab 使用一種特殊語法格式。每行都包含六個欄位。這些欄位之間由空白鍵(空格鍵,tab鍵,或者兩者混合使用)分隔。每個欄位都有自己的含意:
* 第一個欄位是對分割槽的描述,也就是裝置檔案的路徑
* 第二個欄位是分割槽掛載點,也就是分割槽應該掛載到的地方
* 第三個欄位給出分割槽所用的檔案系統
* 第四個欄位給出的是掛載分割槽時mount命令所用的掛載選項。由於每個 檔案系統都有自己的掛載選項,我們建議你閱讀mount手冊(man mount)以獲得 所有掛載選項的列表。多個掛載選項之間是用逗號分隔的。
* 第五個欄位是給dump使用的,用以決定這個分割槽是否需要dump。 一般情況下,你可以把該欄位設為0(零)。
* 第六個欄位是給fsck使用的,用以決定系統非正常關機之後檔案系統的檢查 順序。根檔案系統應該為1,而其它的應該為2(如果不需要 檔案系統自檢的話可以設為0)。

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

相關文章