RHEL LVM详解

来源:互联网 发布:ubuntu 系统log 编辑:程序博客网 时间:2024/04/30 10:03
RHEL LVM详解
2009-04-17 17:04
今天学习了LVM,现在做一下笔记:

LVM: Logical Volume Manager逻辑卷管理,它是一种磁盘的管理方式,是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区。

下面我们来逐步学习LVM的创建,大小调整 ,增容和删除:

一. LVM的创建:

    在创建LVM之前我们先查看一下系统中是否安装了LVM工具:

  

如果没有安装要先安装此工具,不然后继工作无法进行.

创建物理分区并格式化:
和创建普通分区的步骤差不多,只是多了一步要设定分区的ID,LVM的系统ID是 8e.
[root@localhost /]# fdisk -l      --------------------------------->查看分区信息

Disk /dev/sda: 146.8 GB, 146814976000 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id System
/dev/sda1   *           1        3824    30716248+ 83 Linux
/dev/sda2            3825        7648    30716280   83 Linux
/dev/sda3            7649        8170     4192965   82 Linux swap / Solaris
/dev/sda4            8171       17849    77746567+   5 Extended
/dev/sda5            8171       10603    19543041   83 Linux
[root@localhost /]# fdisk /dev/sda       --------------------->对/dev/sda进行分区

The number of cylinders for this disk is set to 17849.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n        -------------------->新建一个分区
First cylinder (10604-17849, default 10604):
Using default value 10604
Last cylinder or +size or +sizeM or +sizeK (10604-17849, default 17849): +500M     ------>创建一个500M的分区

Command (m for help): help         ----------------------->查看fdisk帮助
h: unknown command
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): t                 --------------------------->改变一个分区的ID
Partition number (1-6): 6                 --------------------------->选择第六个分区
Hex code (type L to list codes): 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 XENIX root      39 Plan 9          82 Linux swap / So c4 DRDOS/sec (FAT-
3 XENIX usr       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 Dell Utility  
8 AIX             4e QNX4.x 2nd part 88 Linux plaintext df BootIt        
9 AIX bootable    4f QNX4.x 3rd part 8e Linux LVM       e1 DOS access    
a OS/2 Boot Manag 50 OnTrack DM      93 Amoeba          e3 DOS R/O       
b W95 FAT32       51 OnTrack DM6 Aux 94 Amoeba BBT      e4 SpeedStor     
c W95 FAT32 (LBA) 52 CP/M            9f BSD/OS          eb BeOS fs       
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi ee EFI GPT       
f W95 Ext'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 Priam Edisk     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 Novell Netware b7 BSDI fs         fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap       fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep       
1c Hidden W95 FAT3 75 PC/IX           be Solaris boot    ff BBT
          
Hex code (type L to list codes): 8e         --------------------------->选择linux LVM
Changed system type of partition 6 to 8e (Linux LVM)

Command (m for help): w                       --------------------------->保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@localhost /]# partprobe                 --------------------------->让分区立退生效
[root@localhost /]# fdisk -l

Disk /dev/sda: 146.8 GB, 146814976000 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id System
/dev/sda1   *           1        3824    30716248+ 83 Linux
/dev/sda2            3825        7648    30716280   83 Linux
/dev/sda3            7649        8170     4192965   82 Linux swap / Solaris
/dev/sda4            8171       17849    77746567+   5 Extended
/dev/sda5            8171       10603    19543041   83 Linux
/dev/sda6           10604       10665      497983+ 8e Linux LVM     ---------->刚才新建的LVM分区
[root@localhost /]# mke2fs -j /dev/sda6                                        ----------->格式化此分区
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
124928 inodes, 497980 blocks
24899 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
61 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, 401409

Writing inode tables: done                           
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

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

创建物理卷:

#pvcreate /dev/sda6          ----------------->将sda6创建物理卷
#pvdisplay                          ----------------->查看创建的物理卷

创建卷组:
#vgcreate cxk /dev/sda6           ----------------->cxk为卷组名
#vgdisplay                                 ----------------->查看创建的卷组

创建逻辑卷:
#lvcreate -L 200M cxk -n cxktest           ----------------->创建一个200M的逻辑卷,cxk为刚才建立的卷组,cxktest为逻辑卷名称
#lvdisplay                                             ----------------->查看创建的逻辑卷
格式化和挂载逻辑卷:
[root@localhost /]# mkfs.ext3 /dev/cxk/cxktest         -------------------->格式化逻辑卷
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729

Writing inode tables: done                           
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost /]# mkdir /mnt/cxktest
[root@localhost /]# mount /dev/cxk/cxktest /mnt/cxktest/       -------------->挂载/dev/cxk/cxktest到/mnt/cxktest

二,逻辑卷的大小调整:

调大:
[root@localhost /]# lvresize -L 300M /dev/cxk/cxktest
Extending logical volume cxktest to 300.00 MB
Logical volume cxktest successfully resized
[root@localhost /]# lvdisplay
--- Logical volume ---
LV Name                /dev/cxk/cxktest
VG Name                cxk
LV UUID                zJoxAt-VQ7c-HHm3-lOTM-1Bqd-WQy2-MzOkwB
LV Write Access        read/write
LV Status              available
# open                 1
LV Size                300.00 MB
Current LE             75
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:0

调小:
调小的时间要先缩小文件系统,不然可能会造成资料丢失:
[root@localhost /]# resize2fs /dev/cxk/cxktest 250M     ---------->调整文件系统的大小
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/cxk/cxktest is mounted on /mnt/cxktest; on-line resizing required
Performing an on-line resize of /dev/cxk/cxktest to 256000 (1k) blocks.
The filesystem on /dev/cxk/cxktest is now 256000 blocks long.

[root@localhost /]# lvresize -L 250M /dev/cxk/cxktest  ---------->调整逻辑卷的大小
Rounding up size to full physical extent 252.00 MB
WARNING: Reducing active and open logical volume to 252.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce cxktest? [y/n]: y
Reducing logical volume cxktest to 252.00 MB
Logical volume cxktest successfully resized
[root@localhost /]# resize2fs /dev/cxk/cxktest 250M
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/cxk/cxktest is mounted on /mnt/cxktest; on-line resizing required
Performing an on-line resize of /dev/cxk/cxktest to 256000 (1k) blocks.
The filesystem on /dev/cxk/cxktest is now 256000 blocks long.

[root@localhost /]# lvresize -L 250M /dev/cxk/cxktest
Rounding up size to full physical extent 252.00 MB
WARNING: Reducing active and open logical volume to 252.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce cxktest? [y/n]: y
Reducing logical volume cxktest to 252.00 MB
Logical volume cxktest successfully resized

逻辑卷的扩容:
我们先再新建一个物理卷,和上面同样的方法:
[root@localhost /]# fdisk /dev/sda

The number of cylinders for this disk is set to 17849.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
First cylinder (10666-17849, default 10666):
Using default value 10666
Last cylinder or +size or +sizeM or +sizeK (10666-17849, default 17849): +300M

Command (m for help): t
Partition number (1-7): 7
Hex code (type L to list codes): 8e
Changed system type of partition 7 to 8e (Linux LVM)

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@localhost /]# partprobe

[root@localhost /]# pvcreate /dev/sda7
Physical volume "/dev/sda7" successfully created

[root@localhost /]# vgextend cxk /dev/sda7          ---------->将/dev/sda7增加到逻辑卷cxk中
Volume group "cxk" successfully extended
[root@localhost /]# vgdisplay
--- Volume group ---
VG Name               cxk
System ID            
Format                lvm2
Metadata Areas        2
Metadata Sequence No 5
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               772.00 MB
PE Size               4.00 MB
Total PE              193
Alloc PE / Size       63 / 252.00 MB
Free PE / Size       130 / 520.00 MB
VG UUID               2981Sv-yyqh-AFjB-mBeK-UKPd-SROT-Ivm5RE
  
逻辑卷的删除:

[root@localhost /]# umount /mnt/cxktest/                   -------------------->先umount分区
[root@localhost /]# lvremove /dev/cxk/cxktest          -------------------->移除逻辑卷
Do you really want to remove active logical volume "cxktest"? [y/n]: y
Logical volume "cxktest" successfully removed
[root@localhost /]# vgremove cxk                             -------------------->移除卷组
Volume group "cxk" successfully removed
[root@localhost /]# pvremove /dev/sda6                  --------------------->移除物理卷
Labels on physical volume "/dev/sda6" successfully wiped
[root@localhost /]# pvremove /dev/sda7
Labels on physical volume "/dev/sda7" successfully wiped
[root@localhost /]# fdisk /dev/sda                            --------------------->删除物理分区

The number of cylinders for this disk is set to 17849.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): d
Partition number (1-7): 7

Command (m for help): d
Partition number (1-6): 6

Command (m for help): p

Disk /dev/sda: 146.8 GB, 146814976000 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id System
/dev/sda1   *           1        3824    30716248+ 83 Linux
/dev/sda2            3825        7648    30716280   83 Linux
/dev/sda3            7649        8170     4192965   82 Linux swap / Solaris
/dev/sda4            8171       17849    77746567+   5 Extended
/dev/sda5            8171       10603    19543041   83 Linux

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@localhost /]#

原创粉丝点击