磁盘体系结构和磁盘阵列技术(RAID&LVM)及分区实践


磁盘知识层次图

磁盘知识学习层次图

磁盘物理结构

磁盘的外部结构

磁盘外部结构图

磁盘主轴:决定磁盘转速
磁盘盘片:用于存储数据
磁盘磁头:用于读写数据
磁盘接口:用于连接主板或阵列卡

磁盘的内部结构

磁盘内部结构图

磁头:用来写入和读取数据

  • 磁头数量等于盘面数量
  • 采用径向运动读写数据

磁道:用来存储用户数据

  • 同一盘面不同半径的同心圆为磁道
  • 最外面的同心圆为0磁道,从外向内从0开始顺序编号
  • 磁盘默认按照磁道寻找数据(磁头径向运动为机械运动)

扇区:用来存储用户数据

  • 磁盘存储最小单位(系统存储最小单位是block)
  • 默认磁盘扇区从1扇区开始,扇区大小通常为512字节

柱面:用来存储用户数据

  • 不同盘面上相同位置的磁道组成
  • 磁盘默认按照柱面从外向内进行读写,而不是按盘面进行
  • 定位时,首先确定柱面、再确定盘面、然后确定扇区
  • 写数据时,当前柱面的当前磁道写满后,开始在当前柱面的下一个磁道写入,只有当前柱面写满后才将磁头移动到下一个柱面

计算磁盘大小

[root@localhost Desktop]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0003ac3f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     2508799     1048576   82  Linux swap / Solaris
/dev/sda3         2508800   104857599    51174400   83  Linux

如上所示:磁盘/dev/sda拥有104857600个扇区,每个扇区为512字节,因此磁盘大小 = 扇区数量 * 扇区大小

[root@localhost Desktop]# echo $((104857600*512))
53687091200

磁盘分区

磁盘分区概述

磁盘分区表主要有两种格式:MBR分区表和GPT分区表

MBR分区表

磁盘分区图
MBR(Master Boot Record,主引导记录),位于整个磁盘0柱面0磁道的0扇区,在512Byte的主引导记录中,MBR又可以分为三部分:

  • pre-boot区(BOOTLOADER引导程序):占446字节,负责检查硬盘分区表是否完好、寻找可引导分区并负责将可引导分区的引导扇区(DBR)装入内存,开始系统的启动。
  • Partition table区(分区表):占64字节,每个分区占16字节(因此一块磁盘最多可以分4个主分区活3个主分区+1个扩展分区,一个扩展分区可以分为多个逻辑分区),记录了分区的类型、大小和起始位置等内容。
  • 有效标志:两个字节。

MBR分区的缺点:

  • MBR分区不支持容量大于2.2TB的分区;
  • MBR仅有一个主引导扇区,一旦被破坏很难恢复;
  • MBR内存放开机管理程序的区块仅有446bytes,无法容纳更多的程序代码。

GPT分区表

早期的扇区大小只有512bytes,目前已有4K的扇区设计出现,为了兼容所有的磁盘,在扇区的定义上一般使用逻辑区块地址(Logical Block Adderss,LBA)处理。GPT格式将磁盘所有区块以LBA(预设为516bytes)来规划,第一个LBA称为LBA0。

与MBA仅使用第一个512bytes区块来记录不同,GPT使用了34个LBA区块来记录分区信息,并使用整个磁盘最后33个LBA区块作一个备份。
GPT格式!
LBA0(MBA相容模块):与MBA格式相似,该兼容区块也分为两个部分:

  • 在之前与446bytes相似的区块放置第一阶段的开机管理程序;
  • 在原本分区表的记录区内放入一个特殊标志的分区以此表示该磁盘为GPT格式。

LBA1(GPT表头记录):记录分区表本身的位置和大小,同时记录备份用的GPT分区。并放置了分区表的校验机制码(CRC32),操作系统可以通过该码判断GPT是否正确,如果有错误可以透过该记录区取得备份分区表恢复GPT正常运作。

LBA2-33(实际记录分区信息):从LBA2区块开始,每个LBA都可以记录4个分区记录,因此在默认情况下,总共可以有4*32=128个分区记录。因此GPT分区没有所谓的主分区、扩展分区、逻辑分区的概念。每个分区记录拥有128bytes空间,GPT在每个分区记录中记载开始/结束的扇区号码的大小为64bits,因此每个分区的最大容量为2^64 *512bytes。

注意:GPT不可以使用fdisk管理工具,可以使用gdisk和parted命令。

典型Linux分区

Linux分区图
第一个部分:启动区(boot block),主要为Linux开机服务。Linux开机启动后会首先载入MBR,随后MBR从硬盘的启动区加载开机管理程序。一般为了方便管理,即使某个分区没有安装操作系统,也会预留启动区。

第二个部分:超级区(super block),记录文件系统的整体信息,包括inode和block的总量、剩余量、使用量,以及文件系统的类型与相关信息等。

第三个部分:inodes区,inode记录文件的权限和属性,一个文件对应一个inode,inode中包含多个指针指向属于该文件的各个data block。

最后一个部分:data blocks区,实际记录文件的内容,大文件会占用多个数据块。

磁盘分区完毕后需要进行格式化之后操作系统才能使用这个文件系统,因为每种操作系统所设定的文件属性/权限不同,为了存放这些文件所需的数据,需要将分区格式化称为操作系统能够使用的文件系统格式(格式化称为不同的文件系统,inode和block的分配方式有所不同)。

磁盘分区实践

磁盘小于2T(MBR)

  1. 准备磁盘环境:添加一块20G的磁盘

  2. 检查系统是否识别磁盘:fdisk -l(查看磁盘、分区信息)

    [root@localhost Desktop]# fdisk -l
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    
    
    Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x0003ac3f
    ......
    
  3. 分区规划:分4个区,3个主分区和1个逻辑分区,每个分区2G

  4. 对磁盘进行分区处理:fdisk /dev/sdb

    [root@localhost Desktop]# fdisk /dev/sdb
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0xe10ba6b2.
    
    新建第一主分区:
    Command (m for help): n    新建分区
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p):     默认为主分区
    Using default response p
    Partition number (1-4, default 1):     分区编号默认为1
    First sector (2048-41943039, default 2048):     第一分区起始扇区
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G    设置分区大小为2G
    Partition 1 of type Linux and of size 2 GiB is set
    
    新建第二主分区:
    Command (m for help): n    新建分区
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p):     默认为主分区
    Using default response p
    Partition number (2-4, default 2):    分区编号默认为2 
    First sector (4196352-41943039, default 4196352):     第二分区起始扇区
    Using default value 4196352
    Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +2G    设置分区大小为2G
    Partition 2 of type Linux and of size 2 GiB is set
    
    新建第三主分区:
    Command (m for help): n    新建分区
    Partition type:
       p   primary (2 primary, 0 extended, 2 free)
       e   extended
    Select (default p):     默认为主分区
    Using default response p
    Partition number (3,4, default 3):    分区编号默认为3 
    First sector (8390656-41943039, default 8390656):     第三分区起始扇区
    Using default value 8390656
    Last sector, +sectors or +size{K,M,G} (8390656-41943039, default 41943039): +2G    设置分区大小为2G
    Partition 3 of type Linux and of size 2 GiB is set
    
    新建扩展分区:
    Command (m for help): n    新建分区
    Partition type:
       p   primary (3 primary, 0 extended, 1 free)
       e   extended
    Select (default e):     默认为拓展分区
    Selected partition 4  
    First sector (12584960-41943039, default 12584960):     扩展分区起始扇区
    Using default value 12584960
    Last sector, +sectors or +size{K,M,G} (12584960-41943039, default 41943039):     将剩余磁盘容量均分配给扩展分区
    Using default value 41943039
    Partition 4 of type Extended and of size 14 GiB is set
    
    新建逻辑分区:
    Command (m for help): n    新建分区
    All primary partitions are in use
    Adding logical partition 5    (逻辑分区都是从编号5开始) 
    First sector (12587008-41943039, default 12587008):     逻辑分区起始扇区
    Using default value 12587008
    Last sector, +sectors or +size{K,M,G} (12587008-41943039, default 41943039): +2G    设置该逻辑分区大小为2G
    Partition 5 of type Linux and of size 2 GiB is set
    
    Command (m for help): p    打印分区表
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0xe10ba6b2
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     4196351     2097152   83  Linux(分区类型为Linux,可以使用t改变分区类型)
    /dev/sdb2         4196352     8390655     2097152   83  Linux
    /dev/sdb3         8390656    12584959     2097152   83  Linux
    /dev/sdb4        12584960    41943039    14679040    5  Extended
    /dev/sdb5        12587008    16781311     2097152   83  Linux
    
    Command (m for help): w    保存并退出(q为不保存退出)
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
  5. 将分区信息同步到内核:partprobe(两次使用效果更好)

    [root@localhost Desktop]# partprobe /dev/sdb
    [root@localhost Desktop]# partprobe /dev/sdb
    
  6. 格式化操作(创建文件系统):mkfs.xxx命令

    [root@localhost Desktop]# mkfs
    mkfs         mkfs.cramfs  mkfs.ext3    mkfs.fat     mkfs.msdos   mkfs.xfs     
    mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.minix   mkfs.vfat  
    

    将/dev/sdb1格式化为XFS文件系统

    [root@localhost Desktop]# mkfs.xfs /dev/sdb1
    meta-data=/dev/sdb1              isize=256    agcount=4, agsize=131072 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=0        finobt=0
    data     =                       bsize=4096   blocks=524288, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
    log      =internal log           bsize=4096   blocks=2560, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    
  7. 将/dev/sdb1挂载到/mnt
    临时挂载:mount /dev/sdb1/mnt

    [root@localhost Desktop]# mount /dev/sdb1 /mnt
    [root@localhost Desktop]# cd /mnt
    [root@localhost mnt]# 
    

    永久挂载:vim /etc/fstab
    /etc/fstab
    fstab

    查看挂载信息:df -h

    	[root@localhost ~]# df -h
    	Filesystem      Size  Used Avail Use% Mounted on
    	/dev/sda3        49G  4.5G   45G  10% /
    	devtmpfs        979M     0  979M   0% /dev
    	tmpfs           993M  144K  993M   1% /dev/shm
    	tmpfs           993M  9.0M  984M   1% /run
    	tmpfs           993M     0  993M   0% /sys/fs/cgroup
    	/dev/sda1       197M  123M   74M  63% /boot
    	tmpfs           199M  8.0K  199M   1% /run/user/0
    	/dev/sr0        4.1G  4.1G     0 100% /run/media/root/CentOS 7 x86_64
    	/dev/sdb1       2.0G   33M  2.0G   2% /mnt
    	```
    
    

磁盘大于2T(GPT)

  1. 准备磁盘环境:添加一块3T的磁盘

  2. 使用parted命令进行分区

    [root@localhost Desktop]# parted /dev/sdb
    GNU Parted 3.1
    Using /dev/sdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    
    (parted) help    查看可以使用的命令                                                             
      align-check TYPE N                        check partition N for TYPE(min|opt) alignment
      help [COMMAND]                           print general help, or help on COMMAND
      mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)    
      mkpart PART-TYPE [FS-TYPE] START END     make a partition
      name NUMBER NAME                         name partition NUMBER as NAME
      print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space,
            all found partitions, or a particular partition
      quit                                     exit program
      rescue START END                         rescue a lost partition near START and END
      rm NUMBER                                delete partition NUMBER
      select DEVICE                            choose the device to edit
      disk_set FLAG STATE                      change the FLAG on selected device
      disk_toggle [FLAG]                       toggle the state of FLAG on selected device
      set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
      toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
      unit UNIT                                set the default unit to UNIT
      version                                  display the version number and copyright information of GNU
            Parted
            
    (parted) mklabel gpt     将分区表格式修改为GPT                                                     
    (parted) print    显示分区信息                                                            
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 3221GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt    确认分区表为gpt
    Disk Flags: 
    
    Number  Start  End  Size  File system  Name  Flags
    
    (parted) mkpart primary 0 100G    创建起始地址从0开始、大小为100G的主分区                                            
    Warning: The resulting partition is not properly aligned for best performance.
    Ignore/Cancel? ignore                                                     
    (parted) print    显示分区信息                                                            
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 3221GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End    Size   File system  Name     Flags
     1      17.4kB  100GB  100GB               primary    分区已创建
    
    (parted) quit    分区完毕,退出分区状态(parted分区操作实时生效)                                                         
    Information: You may need to update /etc/fstab.      
    
  3. 将分区信息同步到内核:partprobe(两次使用效果更好)

    [root@localhost Desktop]# partprobe /dev/sdb
    [root@localhost Desktop]# partprobe /dev/sdb
    
  4. 格式化操作(创建文件系统):将/dev/sdb1格式化为XFS文件系统

    [root@localhost Desktop]# mkfs.xfs /dev/sdb1
    meta-data=/dev/sdb1              isize=256    agcount=4, agsize=6103515 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=0        finobt=0
    data     =                       bsize=4096   blocks=24414058, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
    log      =internal log           bsize=4096   blocks=11920, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    
  5. 将/dev/sdb1挂载到/mnt
    临时挂载:mount /dev/sdb1/mnt

    [root@localhost Desktop]# mount /dev/sdb1 /mnt
    [root@localhost Desktop]# cd /mnt
    [root@localhost mnt]# 
    

    永久挂载:vim /etc/fstab
    /etc/fstab

调整swap分区

  1. 查看当前swap分区信息
    [root@localhost Desktop]# free -h
                  total        used        free      shared  buff/cache   available
    Mem:           1.9G        479M        1.1G         10M        355M        1.3G
    Swap:          1.0G          0B        1.0G
    
  2. 将磁盘分出一部分空间给swap分区使用
    [root@localhost Desktop]# dd if=/dev/zero of=/home/swap bs=1024M count=2
    2+0 records in
    2+0 records out
    2147483648 bytes (2.1 GB) copied, 33.9819 s, 63.2 MB/s
    
  3. 格式化新建的分区文件
    [root@localhost Desktop]# mkswap /home/swap 
    Setting up swapspace version 1, size = 2097148 KiB
    no label, UUID=64b710b5-6e21-4c2a-9709-bb90e53dd115
    
  4. 挂载新增的swap分区
    临时挂载:
    [root@localhost Desktop]# swapon /home/swap
    swapon: /home/swap: insecure permissions 0644, 0600 suggested.
    [root@localhost Desktop]# free -h
                  total        used        free      shared  buff/cache   available
    Mem:           1.9G        483M        1.1G         10M        412M        1.3G
    Swap:          3.0G          0B        3.0G
    
    永久挂载:
    [root@localhost Desktop]# vim /etc/fstab
    /swap      swap    swap    defaults        0 0
    

磁盘阵列技术

RAID

RAID(Redundant Array of Independent Disks,独立冗余磁盘阵列)通过把多个磁盘设备组合成一个容量更大、安全性更好的磁盘阵列,并把数据切割成多个区段后分别存放在各个不同的物理磁盘设备上,然后利用分散读写技术来提升磁盘阵列整体的性能,同时把多个重要数据的副本同步到不同的物理磁盘设备上,起到了非常好的数据冗余备份效果。

RAID技术虽然有非常好的数据冗余备份功能,但是也相应提高了成本支出。但是与数据本身的价值相较,RAID技术所具备的冗余备份机制带来的磁盘吞吐量的提升更被企业看重。

RAID技术针对在数据可靠性及读写性能上不同的需求制定不同方案。目前已有的RAID磁盘阵列的方案至少十几种,RAID 0、RAID 1、RAID 2、RAID 5、 RAID 10这四种是最常见的方案。

RAID 0

RAID 0技术把多块物理硬盘设备(至少两块)通过硬件或软件的方式串联在一起,组成一个大的卷组,并将数据依次写入到各个物理硬盘中。这样硬盘设备的读写性能在理想状态下会提升数倍,但其中任意一块硬盘损坏都会导致整个系统的数据遭到破坏。

RAID 0方案虽然能够有效的提升硬盘数据的吞吐速度,但是不具备数据备份和错误修复能力。
RAID 0

RAID 1

RAID 1技术将两块以上的硬盘设备进行绑定,在写入数据时,将数据同时写入到多块硬盘设备上(可以将其视为数据的镜像或者备份),当其中某一块硬盘发生故障后,一般会立即以热交换的方式来恢复数据的正常使用。

RAID 1方案虽然十分注重数据的安全性,但是因为在多块硬盘设备中写入了相同的数据,因此硬盘设备的利用率得以下降。
RAID 1

RAID 5

RAID 5技术是把硬盘设备的数据奇偶校验信息保存到其他硬盘设备中(RAID 5技术并没有备份硬盘中的真实数据信息)。RAID 5磁盘阵列组中数据的奇偶校验信息并不是单独保存到某一块硬盘设备中,而是存储到除自身以外的其他每一块硬盘设备上,这样任何一块硬盘设备损坏都可以通过存储在其他硬盘设备上的奇偶校验信息重建损坏的数据。

RAID 5方案妥协的兼顾了硬盘设备的读写速度、数据安全性与存储成本问题。
RAID 5

RAID 10

RAID 10技术就是RAID 1 + RAID 0技术的一个组合体,至少需要四块硬盘组成。RAID 10先分别两两制作RAID 1磁盘阵列,以保证数据的安全性;然后再对两个RAID 1磁盘阵列实施RAID 0技术,进一步提高磁盘设备的读写速度。

RAID 10方案继承了RAID 0的高读写速度和RAID 1的数据安全性,在不考虑成本的情况下RAID 10的性能超过了RAID 5,成为当前广泛使用的一种存储技术。
RAID 10

LVM

LVM概述

LVM(Logical Volume Manager,逻辑卷管理器)是Linux系统用于对硬盘分区进行管理的一种机制,其创建初衷是为了解决硬盘设备在创建分区后不易修改分区大小的缺陷。

LVM技术在硬盘分区和文件系统之间添加了一个逻辑层,它提供了一个抽象的卷组,可以把多块硬盘进行卷组合并,如此用户就不必关心物理硬盘设备的底层架构和布局,即可实现对硬盘分区的动态调整。
LVM

PV(Physical Volume,物理卷):由PE组成

  • PE(Physical Extent,基本单元)

VG(Volume Group,卷组):由PV组成
LV(Logical Volume,逻辑卷):划分卷组

LVM部署命令

部署LVM时,需要逐个配置物理卷、卷组和逻辑卷,常用的部署命令如下:
常用LVM部署命令

LVM部署实践

新建逻辑卷
  1. 新添加一个硬盘,将其分出/dev/sdb1、/dev/sdb2、/dev/sdb3三个分区

    [root@192 Desktop]# fdisk /dev/sdb
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0xe3a26c29.
    
    Command (m for help): n
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p): 
    Using default response p
    Partition number (1-4, default 1): 
    First sector (2048-41943039, default 2048): 
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +100M
    Partition 1 of type Linux and of size 100 MiB is set
    
    Command (m for help): n   
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): 
    Using default response p
    Partition number (2-4, default 2): 
    First sector (206848-41943039, default 206848): 
    Using default value 206848
    Last sector, +sectors or +size{K,M,G} (206848-41943039, default 41943039): +100M
    Partition 2 of type Linux and of size 100 MiB is set
    
    Command (m for help): n
    Partition type:
       p   primary (2 primary, 0 extended, 2 free)
       e   extended
    Select (default p): 
    Using default response p
    Partition number (3,4, default 3): 
    First sector (411648-41943039, default 411648): 
    Using default value 411648
    Last sector, +sectors or +size{K,M,G} (411648-41943039, default 41943039): +100M
    Partition 3 of type Linux and of size 100 MiB is set
    
    Command (m for help): p
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0xe3a26c29
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048      206847      102400   83  Linux
    /dev/sdb2          206848      411647      102400   83  Linux
    /dev/sdb3          411648      616447      102400   83  Linux
    
    Command (m for help): t
    Partition number (1-3, default 3): 
    Hex code (type L to list all codes): L
    
     0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
     1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
     2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
     3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
     4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
     5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
     6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
     7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
     8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
     9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
     a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
     b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
     c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
     e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
     f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
    10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
    11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
    12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
    14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
    16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      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            
    1e  Hidden W95 FAT1 80  Old Minix      
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux' to 'Linux LVM'
    
    Command (m for help): t
    Partition number (1-3, default 3): 2
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux' to 'Linux LVM'
    
    Command (m for help): t
    Partition number (1-3, default 3): 1
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux' to 'Linux LVM'
    
    Command (m for help): p
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0xe3a26c29
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048      206847      102400   8e  Linux LVM
    /dev/sdb2          206848      411647      102400   8e  Linux LVM
    /dev/sdb3          411648      616447      102400   8e  Linux LVM
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
  2. pvcreate使得这三个分区支持LVM技术

    [root@192 Desktop]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3
      Physical volume "/dev/sdb1" successfully created
      Physical volume "/dev/sdb2" successfully created
      Physical volume "/dev/sdb3" successfully created
    
  3. vgcreate将/dev/sdb1、/dev/sdb2分区添加到vg0卷组中

    root@192 Desktop]# vgcreate vg0 /dev/sdb1 /dev/sdb2
      Volume group "vg0" successfully created
    
  4. vgdisplay查看vg0卷组的状态

    [root@192 Desktop]# vgdisplay
      --- Volume group ---
      VG Name               vg0
      System ID             
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  1
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                0
      Open LV               0
      Max PV                0
      Cur PV                2
      Act PV                2
      VG Size               192.00 MiB
      PE Size               4.00 MiB
      Total PE              48
      Alloc PE / Size       0 / 0   
      Free  PE / Size       48 / 192.00 MiB
      VG UUID               mYC1zX-hc6o-UYcy-1NZR-A63T-Hfqv-aJrZpN
    
  5. lvcreate切割出一个50MB的逻辑卷设备

    [root@192 Desktop]# lvcreate -n data -L 50M vg0
      Rounding up size to full physical extent 52.00 MiB
      Logical volume "data" created.
    
  6. 将生成好的逻辑卷进行格式化,然后挂载使用(Linux系统将LVM逻辑卷设备放置在/dev/卷组名称/逻辑卷名称)

    [root@192 ~]# mkfs.ext4 /dev/vg0/data
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=1024 (log=0)
    Fragment size=1024 (log=0)
    Stride=0 blocks, Stripe width=0 blocks
    26624 inodes, 106496 blocks
    5324 blocks (5.00%) reserved for the super user
    First data block=1
    Maximum filesystem blocks=33685504
    13 block groups
    8192 blocks per group, 8192 fragments per group
    2048 inodes per group
    Superblock backups stored on blocks: 
    	8193, 24577, 40961, 57345, 73729
    
    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done 
    
    [root@192 Desktop]# mount /dev/vg0/data /mnt
    [root@192 Desktop]# cd /mnt
    [root@192 mnt]# 
    
扩容逻辑卷

只要卷组中有足够的资源,就可以为逻辑卷扩容(扩容前必须卸载设备和挂载点的关联)

  1. 卸载文件系统并将上述实验中的data逻辑卷扩容到100M

    [root@192 ~]# umount /dev/vg0/data
    [root@192 ~]# lvextend -L +50M /dev/vg0/data
      Rounding size to boundary between physical extents: 52.00 MiB
      Size of logical volume vg0/data changed from 52.00 MiB (13 extents) to 104.00 MiB (26 extents).
      Logical volume data successfully resized.
    
  2. 检查硬盘完整性,并重置硬盘容量

    [root@192 Desktop]# e2fsck -f /dev/vg0/data
    e2fsck 1.42.9 (28-Dec-2013)
    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/vg0/data: 11/13328 files (9.1% non-contiguous), 6823/53248 blocks
    [root@192 Desktop]# resize2fs /dev/vg0/data
    resize2fs 1.42.9 (28-Dec-2013)
    Resizing the filesystem on /dev/vg0/data to 106496 (1k) blocks.
    The filesystem on /dev/vg0/data is now 106496 blocks long.
    
  3. 重新挂载并查看挂载状态

    [root@192 Desktop]# mount /dev/vg0/data /mnt
    [root@192 Desktop]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda3              49G  6.5G   43G  14% /
    devtmpfs              979M     0  979M   0% /dev
    tmpfs                 993M  144K  993M   1% /dev/shm
    tmpfs                 993M  9.0M  984M   1% /run
    tmpfs                 993M     0  993M   0% /sys/fs/cgroup
    /dev/sda1             197M  123M   74M  63% /boot
    tmpfs                 199M   12K  199M   1% /run/user/0
    /dev/sr0              4.1G  4.1G     0 100% /run/media/root/CentOS 7 x86_64
    /dev/mapper/vg0-data   97M  1.6M   89M   2% /mnt
    
缩小逻辑卷

相较于扩容逻辑卷,对逻辑卷进行缩容操作的丢失数据的风险更大。Linux系统规定在LVM逻辑卷进行缩容操作之前,要先检查文件系统的完整性(缩容前必须卸载设备和挂载点的关联)

  1. 卸载文件系统并检查文件系统的完整性

    [root@192 Desktop]# umount /dev/vg0/data
    [root@192 Desktop]# e2fsck -f /dev/vg0/data
    e2fsck 1.42.9 (28-Dec-2013)
    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/vg0/data: 11/24752 files (9.1% non-contiguous), 8779/106496 blocks
    
  2. 将逻辑卷data的容量减少到60MB

    [root@192 Desktop]# resize2fs /dev/vg0/data 60M
    resize2fs 1.42.9 (28-Dec-2013)
    Resizing the filesystem on /dev/vg0/data to 61440 (1k) blocks.
    The filesystem on /dev/vg0/data is now 61440 blocks long.
    
    [root@192 Desktop]# lvreduce -L 60M /dev/vg0/data
      WARNING: Reducing active logical volume to 60.00 MiB
      THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce data? [y/n]: y
      Size of logical volume vg0/data changed from 104.00 MiB (26 extents) to 60.00 MiB (15 extents).
      Logical volume data successfully resized.
    
  3. 重新挂载并查看挂载状态

    [root@192 Desktop]# mount /dev/vg0/data /mnt
    [root@192 Desktop]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda3              49G  6.5G   43G  14% /
    devtmpfs              979M     0  979M   0% /dev
    tmpfs                 993M  144K  993M   1% /dev/shm
    tmpfs                 993M  9.0M  984M   1% /run
    tmpfs                 993M     0  993M   0% /sys/fs/cgroup
    /dev/sda1             197M  123M   74M  63% /boot
    tmpfs                 199M   12K  199M   1% /run/user/0
    /dev/sr0              4.1G  4.1G     0 100% /run/media/root/CentOS 7 x86_64
    /dev/mapper/vg0-data   55M  1.3M   49M   3% /mnt
    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值