LVM创建以及使用

           LVM(Logical VolumeManager 逻辑卷管理)

在磁盘分区和文件系统之间添加一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统,来提高磁盘分区管理的灵活性可实现零停机前提下自如对文件系统的大小进行调整,可以方便实现文件系统跨越不同磁盘和分区。

下图是我根据自己的理解画出的一张图。此图背景是:公司有一台戴尔R910服务器,现在接上6个SAS硬盘,做成raid5磁盘阵列,最后一个硬盘设为全局热备。然后在戴尔自带的磁盘阵列管理上把剩下的5块硬盘划分为4个VD(虚拟磁盘),把系统全部安装在第一个VD,然后剩下的3个VD利用LVM技术整合成一个大分区用于存储数据。


wKiom1RpzvXx3EOiAAFvpWRdLWE760.jpg


下面分享的我创建LV的整个过程,此操作系统为 Red hat enterprise linux 5.5 x86_64 : 


-------------------------------------  下面是对VD磁盘的分区管理  ----------------------------------------------------

 

[root@mail ~]# ls  /dev/sd* #查看当前系统有多少分区

/dev/sda /dev/sda1  /dev/sda2  /dev/sda3 /dev/sdb  /dev/sdc  /dev/sdd

 

[root@mail ~]# fdisk /dev/sdb  #对第二块硬盘进行分区

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    #更改分区的系统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 ]

 #创建主分区

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)

#创建主分区

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)

#创建主分区

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*  #查看当前系统有多少分区,可以看到sdbsdcsdd这三块硬盘都是分成一个分区

/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 volumepv就是物理卷的意思】

 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 �Ch #以易看懂的方式显示当前所有挂载点空余空间,dfdisk  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 / 的命令来重新挂载 / ,然后就可以修改文件啦,这个命令很有用,无论你是因为什么原因进到这样的一个维护模式,都可以用这样的命令,然后获取对磁盘的写入权限。

你可能感兴趣的:(LVM创建,系统分区)