linux AS4下LVM的使用手册

1:创建PV(物理分区)
假设我的磁盘有一部分没有划分的分区:

fdisk /dev/sda
查看现在的分区
Command (m for help): p

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          13      104391   83  Linux
/dev/sda2              14         143     1044225   82  Linux swap
/dev/sda3             144        2055    15358140   83  Linux

创建一个新分区:
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e(选择创建扩展分区)
Selected partition 4
First cylinder (2056-2610, default 2056):
Using default value 2056
Last cylinder or +size or +sizeM or +sizeK (2056-2610, default 2610):
Using default value 2610

前面的分区还没有用完磁盘,继续创建另一个分区:
Command (m for help): n
First cylinder (2056-2610, default 2056):
Using default value 2056
Last cylinder or +size or +sizeM or +sizeK (2056-2610, default 2610): 2300

Command (m for help): n
First cylinder (2301-2610, default 2301):
Using default value 2301
Last cylinder or +size or +sizeM or +sizeK (2301-2610, default 2610):
Using default value 2610

查看已经划分好的分区:
Command (m for help): p

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          13      104391   83  Linux
/dev/sda2              14         143     1044225   82  Linux swap
/dev/sda3             144        2055    15358140   83  Linux
/dev/sda4            2056        2610     4458037+   5  Extended
/dev/sda5            2056        2300     1967931   83  Linux
/dev/sda6            2301        2610     2490043+  83  Linux

w(存盘,退出)

从上面看,sda5和sda6是新创建的分区,ID号为8e(也就是 linux LVM)
现在将新创建的分区修改他们的系统ID

[root@oracle ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 2610.
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): t
Partition number (1-6): 5
Hex code (type L to list codes): L

 0  Empty           1e  Hidden W95 FAT1 75  PC/IX           be  Solaris boot  
 1  FAT12           24  NEC DOS         80  Old Minix       bf  Solaris       
 2  XENIX root      39  Plan 9          81  Minix / old Lin c1  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  82  Linux swap      c4  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     83  Linux           c6  DRDOS/sec (FAT-
 5  Extended        41  PPC PReP Boot   84  OS/2 hidden C:  c7  Syrinx        
 6  FAT16           42  SFS             85  Linux extended  da  Non-FS data   
 7  HPFS/NTFS       4d  QNX4.x          86  NTFS volume set db  CP/M / CTOS / .
 8  AIX             4e  QNX4.x 2nd part 87  NTFS volume set de  Dell Utility  
 9  AIX bootable    4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt        
 a  OS/2 Boot Manag 50  OnTrack DM      93  Amoeba          e1  DOS access    
 b  W95 FAT32       51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O       
 c  W95 FAT32 (LBA) 52  CP/M            9f  BSD/OS          e4  SpeedStor     
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs       
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  EFI GPT       
10  OPUS            55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor     
14  Hidden FAT16 <3 61  SpeedStor       a9  NetBSD          f4  SpeedStor     
16  Hidden FAT16    63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary 
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fd  Linux raid auto
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fe  LANstep       
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid ff  BBT           
1c  Hidden W95 FAT3
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-6): 6
Hex code (type L to list codes): 8e
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@oracle ~]# partprobe
[root@oracle ~]# reboot

创建PV(物理卷)
[root@oracle ~]# pvcreate  /dev/sda5 /dev/sda6
  Physical volume "/dev/sda5" successfully created
  Physical volume "/dev/sda6" successfully created

创建物理卷组rootvg,并把/dev/sda5 /dev/sda6加入其中:
[root@oracle ~]# vgcreate rootvg /dev/sda5 /dev/sda6
  Volume group "rootvg" successfully created

在物理卷组上创建一个100MB的逻辑卷lv1:
[root@oracle ~]# lvcreate -L 100M  -n lv1 rootvg
  Logical volume "lv1" created

格式化分区:
[root@oracle ~]# mkfs.ext3 /dev/rootvg/lv1
mke2fs 1.35 (28-Feb-2004)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
1976 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 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

将lv1挂载到/mnt/lvm下
[root@oracle ~]# mkdir /mnt/lvm
[root@oracle ~]# mount /dev/rootvg/lv1 /mnt/lvm/
[root@oracle ~]#

[root@oracle lvm]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              15G  2.4G   12G  18% /
/dev/sda1              99M  8.6M   86M  10% /boot
none                  125M     0  125M   0% /dev/shm
/dev/mapper/VolGroup00-LogVol02
                      9.1G   53M  8.6G   1% /home
/dev/mapper/VolGroup00-LogVol00
                      9.7G   55M  9.1G   1% /opt
/dev/mapper/VolGroup00-LogVol01
                     1008M   34M  924M   4% /tmp
/dev/mapper/rootvg-lv1
                       97M  5.6M   87M   7% /mnt/lvm

动态增加逻辑卷的大小:
[root@oracle lvm]# lvextend -L+200M /dev/rootvg/lv1
  Extending logical volume lv1 to 300.00 MB
  Logical volume lv1 successfully resized
[root@oracle lvm]# ext2online /dev/rootvg/lv1
ext2online v1.1.18 - 2001/03/18 for EXT2FS 0.5b
[root@oracle lvm]# cd /mnt/lvm/
[root@oracle lvm]# ls
lost+found
[root@oracle lvm]# mkdir aa
[root@oracle lvm]# ls
aa  lost+found
[root@oracle lvm]#

减少逻辑卷的大小:
[root@oracle lvm]# umount /mnt/lvm/
[root@oracle /]# umount /mnt/lvm/
[root@oracle /]# cd /mnt/lvm/
[root@oracle lvm]# ls

[root@oracle lvm]# mke2fs -n /dev/rootvg/lv1
mke2fs 1.35 (28-Feb-2004)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
76912 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
38 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729, 204801, 221185

Block size=1024(bytes)
307200 blocks
lv1分区大小=1024×307200/1024/1024=300(MB)

307200个blocks组成了300M,如果我们要减小50M,应该减少多少blocks?
307200   x
------ =-----
300      50

x=307200X50/300=51200


[root@oracle lvm]# mke2fs -f /dev/rootvg/lv1
mke2fs: bad fragment size - /dev/rootvg/lv1
[root@oracle lvm]# resize2fs -f /dev/rootvg/lv1
resize2fs 1.35 (28-Feb-2004)
The filesystem is already 307200 blocks long.  Nothing to do!

调整lv分区大小,减小50M
[root@oracle lvm]# lvreduce -L-50M /dev/rootvg/lv1
  Rounding up size to full physical extent 48.00 MB
  WARNING: Reducing active logical volume to 252.00 MB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv1? [y/n]: y
  Reducing logical volume lv1 to 252.00 MB
  Logical volume lv1 successfully resized

重新挂载:
[root@oracle lvm]# mount /dev/rootvg/lv1 /mnt/lvm/

删除
[root@oracle lvm]# umount /mnt/lvm/
aa          lost+found/


动态加大VG:
这里我们新增一个PV并加入VG

1 fdisk增加一个(0x)8e标志的分区/dev/hda7
2 vgextend rootvg /dev/hda7

好了,vg增大了

缩小逻辑卷组VG:

1 搬移PV中的资料(只限于同一VG中)
#pvmove [-n ] []

#pvmove /dev/hda5 /dev/hda6
将VG中pv hda5的内容搬移到hda6中
pvmove /dev/hda5(也可以这样,lvm决定hda2的内容被复制到哪里)
2 vgreduce rootvg /dev/hda5
把vg hda5移除

5) 如何删除逻辑卷/逻辑卷组

删除VG流程:

1 umount /挂接点
2 lvremove /dev/rootvg/lv1
移除逻辑卷,这里是lv1
3 vgchange -a n rootvg(关闭rootvg)
4 vgremove rootvg
移除rootvg

删除分区:
fdis /dev/sda
Command (m for help): p

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          13      104391   83  Linux
/dev/sda2              14         143     1044225   82  Linux swap
/dev/sda3             144        2055    15358140   83  Linux
/dev/sda4            2056        2610     4458037+   5  Extended
/dev/sda5            2056        2300     1967931   8e  Linux LVM
/dev/sda6            2301        2610     2490043+  8e  Linux LVM

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

Command (m for help): p

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          13      104391   83  Linux
/dev/sda2              14         143     1044225   82  Linux swap
/dev/sda3             144        2055    15358140   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@oracle lvm]# partprobe
[root@oracle lvm]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              15G  2.4G   12G  18% /
/dev/sda1              99M  8.6M   86M  10% /boot
none                  125M     0  125M   0% /dev/shm
/dev/mapper/VolGroup00-LogVol02
                      9.1G   53M  8.6G   1% /home
/dev/mapper/VolGroup00-LogVol00
                      9.7G   55M  9.1G   1% /opt
/dev/mapper/VolGroup00-LogVol01
                     1008M   34M  924M   4% /tmp

6) lvm相关命令

这个表格,你可以在RHCE_Study_Guide.pdf上找到

    Physical Volume     Volume Group         Logical Volume
-------------------------------------------------------------------------
scan     pvscan             vgscan             lvscan
create     pvcreate         vgcreate         lvcreate
display pvdispaly         vgdisplay         lvdisplay
remove     pvremove         vgremove         lvremove
-------------------------------------------------------------------------

你可能感兴趣的:(linux AS4下LVM的使用手册)