使用parted对GPT/MBR类型的磁盘做分区的扩容操作

使用parted对GPT/MBR类型的磁盘做分区的扩容操作

缺点:需要先停止现有业务才可以执行。
优点:可以对单个分区进行扩容,不可以缩容。

  • 查看磁盘分区概览

    parted -l
    
    Model: Virtio Block Device (virtblk)
    Disk /dev/vda: 42.9GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system     Flags
     1      1049kB  4296MB  4295MB  primary  linux-swap(v1)
     2      4296MB  42.9GB  38.7GB  primary  ext4            boot
    
    
    Model: Virtio Block Device (virtblk)
    Disk /dev/vdb: 537GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start  End  Size  File system  Name  Flags    
    
  • 目标:对磁盘进行一部分分区,之后进行移除扩容。

第一步:初始分区和挂载

  • 1.开始对/dev/vdb盘进行分区,本次分区给1G容量

    parted /dev/vdb 选中vdb磁盘
    
    GNU Parted 3.1
    Using /dev/vdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) mkpart 执行分区                                                           
    Partition name?  []? data 分区的名字                                                 
    File system type?  [ext2]? xfs 分区的类型                                        
    Start? 1 开始起点                                                                 
    End? 1G 分多大,-1为剩余所有
    (parted) print 查看分区情况
    Model: Virtio Block Device (virtblk)
    Disk /dev/vdb: 537GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End     Size   File system  Name  Flags
     1      1049kB  1000MB  999MB  xfs          data
    
    (parted) quit 执行完成后,立即生效。不用w
    Information: You may need to update /etc/fstab.
    
  • 2.分区后,对分区格式化

    mkfs.xfs -f /dev/vdb1
    meta-data=/dev/vdb1              isize=512    agcount=4, agsize=60992 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0, sparse=0
    data     =                       bsize=4096   blocks=243968, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=855, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    
  • 3.挂载

    mount /dev/vdb1 /data/ 挂载到data目录下
    df -h 查看挂载详情
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vdb1       950M   33M  918M   4% /data 挂载成功
    
  • 4.设置开启自动挂载

    blkid /dev/vdb1 查看uuid
    /dev/vdb1: UUID="58dd1e5c-ad82-41a2-a71d-6313a8c2c142" TYPE="xfs" PARTLABEL="data" PARTUUID="dc59f2ae-bdd0-4720-bc8a-3756824956c7" 
    vim /etc/fstab 打开配置文件
    #
    # /etc/fstab
    # Created by anaconda on Mon Dec 27 14:34:10 2021
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    UUID=58dd1e5c-ad82-41a2-a71d-6313a8c2c142 /data xfs defaults 0 0 配置uuid
    !wq 保存退出
    
  • 5.往data中写入数据

    [root@ecs-f7d0 data]# ll 有2个文件
    total 1548
    -rw-r--r-- 1 root root  114353 Jul  2 16:24 版本更新.png
    -rw-r--r-- 1 root root 1469507 Jul  2 16:24 版本更新.psd
    

第二步:将原分区进行扩容,验证数据是否丢失

  • 1.卸载挂载点

    umount /data
    
  • 2.开始搞

    [root@ecs-f7d0 ~]# parted -l
    Model: Virtio Block Device (virtblk)
    Disk /dev/vdb: 537GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    分区号,后面移除分区使用
    Number  Start   End     Size   File system  Name  Flags
     1      1049kB  1000MB  999MB  xfs          data
    --------------------------------------------------------
    [root@ecs-f7d0 ~]# parted /dev/vdb 选择vdb磁盘
    GNU Parted 3.1
    Using /dev/vdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) rm 移除分区,不会丢失数据       
    Partition number? 1 分区1号                                                       
    (parted) mkpart 创建分区                                                           
    Partition name?  []? data 分区名                                                 
    File system type?  [ext2]? xfs 分区类型                                           
    Start? 1 开始位置                                                                 
    End? -1 空间的尾部
    (parted) print  查看分区后的信息 
    Model: Virtio Block Device (virtblk)
    Disk /dev/vdb: 537GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    Number  Start   End    Size   File system  Name  Flags
     1      1049kB  537GB  537GB  xfs          data
    (parted) quit 退出
    
  • 3.重新挂载,同步分区信息

    mount /dev/vdb1 /data/ 重新挂载
    xfs_growfs /data/ 同步分区信息
    meta-data=/dev/vdb1              isize=512    agcount=4, agsize=60992 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0 spinodes=0
    data     =                       bsize=4096   blocks=243968, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal               bsize=4096   blocks=855, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 243968 to 131071488
    
  • 4.查看扩容后的情况

    df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vdb1       500G  133M  500G   1% /data 从1G成功扩容到500G
    
  • 5.查看数据是否存在

    [root@ecs-f7d0 /]# cd /data/
    [root@ecs-f7d0 data]# ls
    版本更新.png  版本更新.psd 确实都在
    
  • 重新设置开机自动挂载

    blkid /dev/vdb1 查看UUID
    /dev/vdb1: UUID="9aae5f3c-9b4f-431d-8bff-0938f55b0685" TYPE="xfs" PARTLABEL="data" PARTUUID="fa2b86e8-5848-43fa-9a8c-35aaaecb1201"
    编辑fstab
    vim /etc/fstab 
    UUID=9aae5f3c-9b4f-431d-8bff-0938f55b0685 /data xfs defaults 0 0
    

你可能感兴趣的:(使用parted对GPT/MBR类型的磁盘做分区的扩容操作)