首页 > 代码库 > Linux系统分区及LVM使用(一)

Linux系统分区及LVM使用(一)

一、MBR分区

使用fdisk等分区工具,使用MBR(Master Boot Record)管理单个磁盘,这就决定了其分区表最多支持4个分区,分区类型包括主分区和扩展分区,逻辑分区可以被再次分割成多个逻辑分区。


标准 MBR 结构
地址 描述 长度            
(字节)
Hex Oct Dec
0000 0000 0 代码区 440            
(最大 446)
01B8 0670 440 选用磁盘标志 4
01BC 0674 444 一般为空值; 0x0000 2
01BE 0676 446 标准 MBR 分区表规划            
(四个16 byte的主分区表入口)
64
01FE 0776 510 55h MBR 有效标志:            
0x55AA
2
01FF 0777 511 AAh
MBR, 总大小: 446 + 64 + 2 = 512


上图是MBR结构,64个字节用来做分区表,分为4组,所以该硬盘最多支持4个主分区或者扩展分区。

fdisk等工具分区的使用简单,文章很多,也很详尽,这里不再赘述。

二、RAID

    机械磁盘的安全性是很低的,生产环境中即使是使用可靠性较高的工业级磁盘,也很难保证磁盘不出故障,从而影响到数据的完整性。

    1987年加州大学伯克利分校提出了RAID(Redundant Array of Inexpensive Disks)廉价磁盘冗余阵列,其设计目的就是把多个磁盘组成一个阵列,使其性能达到甚至超过价格昂贵且容量巨大的硬盘。但是发展到今天,机械式硬盘的价格不断下降,而且容量在不断上升,人们在使用RAID的时候,更看重其冗余带来可靠性和读写性能提升,所以,RAID被重新称为 Redundant Array of Inexpensive Disks 独立硬盘冗余阵列。

    总体来说,RAID是把多块磁盘组合起来当做一块硬盘提供给操作系统使用。

    RAID分为不同的级别,或提高读写性能,或提高冗余度已达到可靠性提升。生成环境中常用RAID0、1、5、10,尤其RAID5和10应用最为广泛。关于不同冗余级别的定义和数据块以及校验块的分布示意图可以参考资料“Common RAID Disk Data Format Specification ”。

     RAID分为硬件RAID和软件RAID。硬件RAID一般使用在商用服务器之中,使用集成控制器或者板卡适配器来提供RAID功能(一些民用级主板也板载了RAID控制器);在有些考虑成本的地方或非关键场合,可以考虑使用软件RAID,但请在多块物理磁盘上实现。

    在Linux系统中目前以MD (Multiple Devices)虚拟块设备的方式实现软件RAID,利用多个底层的块设备虚拟出一个新的虚拟块设备,并且利用条带化(stripping)技术将数据块均匀分布到多个磁盘上来提高虚拟设备的读写性能,利用不同的数据冗余算法来保护用户的数据,而不会因为某个块设备的故障而完全丢失,而且还能在设备被替换后将丢失的数据恢复到新的设备上。目前MD支持linear, multipath, raid0 (stripping), raid1 (mirror), raid4, raid5, raid6, raid10等不同的冗余级别和组成方式,也能支持多个RAID阵列的层叠组成raid1+0, raid5+1等类型的阵列。更多内容请参考资料“Software RAID HOWTO”。

 

下面就在虚拟机中以Linux的软RAID来举例实现RAID10

image

创建四块2G磁盘备用,分别两两先做成RAID1。

[root@localhost ~]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf4ac53f4.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won‘t be recoverable.
 
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
         switch off the mode (command ‘c‘) and change display units to
         sectors (command ‘u‘).
 
Command (m for help): p
 
Disk /dev/sdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf4ac53f4
 
   Device Boot      Start         End      Blocks   Id  System
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): 
Using default value 261
 
Command (m for help): l
 
 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext‘d (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1
 
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.

如上,将4块磁盘设备分区,并调整类型为fd(Linux raid auto),如下

Disk /dev/sdb: 261 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
 
   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sdb1          0+    260     261-   2096451   fd  Linux raid autodetect
 
Disk /dev/sde: 261 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
 
   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sde1          0+    260     261-   2096451   fd  Linux raid autodetect
 
Disk /dev/sdd: 261 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
 
   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sdd1          0+    260     261-   2096451   fd  Linux raid autodetect
 
Disk /dev/sdc: 261 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
 
   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sdc1          0+    260     261-   2096451   fd  Linux raid autodetect

先组建RAID1,然后再组建RAID0

[root@localhost ~]# mdadm -C /dev/md0 -l 1 -n 2 -a yes /dev/sd{b,c}1
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store ‘/boot‘ on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=http://www.mamicode.com/0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@localhost ~]# mdadm -C /dev/md1 -l 1 -n 2 -a yes /dev/sd{d,e}1
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store ‘/boot‘ on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=http://www.mamicode.com/0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] 
md1 : active raid1 sde1[1] sdd1[0]
      2095360 blocks super 1.2 [2/2] [UU]
      
md0 : active raid1 sdc1[1] sdb1[0]
      2095360 blocks super 1.2 [2/2] [UU]
      
unused devices: <none>
 
[root@localhost ~]# mdadm -C /dev/md3 -l 0 -n 2 -a yes /dev/md{0,1}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md3 started.
[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] [raid0] 
md3 : active raid0 md1[1] md0[0]
      4190208 blocks super 1.2 512k chunks
      
md1 : active raid1 sde1[1] sdd1[0]
      2095360 blocks super 1.2 [2/2] [UU]
      
md0 : active raid1 sdc1[1] sdb1[0]
      2095360 blocks super 1.2 [2/2] [UU]
      
unused devices: <none>

对/dev/md3进行格式化

[root@localhost ~]# mke2fs -t ext4 /dev/md3
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=128 blocks, Stripe width=256 blocks
262144 inodes, 1047552 blocks
52377 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736
 
Writing inode tables: done                            
Creating journal (16384 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.

将/dev/md3挂载到/mnt/r10,并测试

[root@localhost mnt]# mkdir /mnt/r10
[root@localhost mnt]# mount /dev/md3 /mnt/r10
[root@localhost mnt]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/md3              4.0G   72M  3.7G   2% /mnt/r10
[root@localhost mnt]# cd /mnt/r10/
[root@localhost r10]# cat > test.txt <<EOF
>This is a test file.
>Welcome!
>EOF
[root@localhost r10]# cat /mnt/r10/test.txt
This is a test file.
Welcome!

至此,RAID10创建成功。当然如果要长久使用,还要修改/etc/fstab,在其中添加mount信息。

下一篇,Linux系统分区及LVM使用(二)

本文出自 “终南山下” 博客,请务必保留此出处http://me2xp.blog.51cto.com/6716920/1440617

Linux系统分区及LVM使用(一)