磁盘分区

发布时间 2023-06-12 18:43:28作者: FouroFour

磁盘分区

一、fdisk

(1)fdisk命令只支持msdos,分区的时候只支持小容量硬盘(<=2T),但是如果不需要分区的话,那么整块sdb硬盘,类型为msdos,那么他的大小是可以大于2T的。
(2)fdisk命令不支持gpt,所以当使用fdisk命令给gpt类型硬盘分区是会出现告警

  1. 首先先把设备关机,添加一块新的磁盘

  2. fdisk -l
    
    磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x000e0f54
    
       设备 Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200    41943039    19921920   83  Linux
    
    磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x8ff20778
    
  3. [root@localhost ~]# fdisk /dev/sdb
    欢迎使用 fdisk (util-linux 2.23.2)。
    
    更改将停留在内存中,直到您决定将更改写入磁盘。
    使用写入命令前请三思。
    
    
    命令(输入 m 获取帮助):m
       a   toggle a bootable flag
       b   edit bsd disklabel
       c   toggle the dos compatibility flag
       d   delete a partition
       g   create a new empty GPT partition table
       G   create an IRIX (SGI) partition table
       l   list known partition types
       m   print this menu
       n   add a new partition
       o   create a new empty DOS partition table
       p   print the partition table
       q   quit without saving changes
       s   create a new empty Sun disklabel
       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 (experts only)
    
  4. 命令(输入 m 获取帮助):p
    
    磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x8ff20778
    
       设备 Boot      Start         End      Blocks   Id  System
    
  5. 添加主分区

    命令(输入 m 获取帮助):n
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p): p
    分区号 (1-4,默认 1):
    起始 扇区 (2048-41943039,默认为 2048):
    将使用默认值 2048
    Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+500M
    分区 1 已设置为 Linux 类型,大小设为 500 MiB
    
    命令(输入 m 获取帮助):p
    
    磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x8ff20778
    
       设备 Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     1026047      512000   83  Linux
    
    
  6. 添加扩展分区

    命令(输入 m 获取帮助):n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): e
    分区号 (2-4,默认 2):
    起始 扇区 (1026048-41943039,默认为 1026048):
    将使用默认值 1026048
    Last 扇区, +扇区 or +size{K,M,G} (1026048-41943039,默认为 41943039):
    将使用默认值 41943039
    分区 2 已设置为 Extended 类型,大小设为 19.5 GiB
    
    命令(输入 m 获取帮助):p
    
    磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x8ff20778
    
       设备 Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     1026047      512000   83  Linux
    /dev/sdb2         1026048    41943039    20458496    5  Extended
    
  7. 添加逻辑分区

    命令(输入 m 获取帮助):n
    Partition type:
       p   primary (1 primary, 1 extended, 2 free)
       l   logical (numbered from 5)
    Select (default p): l
    添加逻辑分区 5
    起始 扇区 (1028096-41943039,默认为 1028096): 
    将使用默认值 1028096
    Last 扇区, +扇区 or +size{K,M,G} (1028096-41943039,默认为 41943039):+10G
    分区 5 已设置为 Linux 类型,大小设为 10 GiB
    
    命令(输入 m 获取帮助):p
    
    磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x8ff20778
    
       设备 Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     1026047      512000   83  Linux
    /dev/sdb2         1026048    41943039    20458496    5  Extended
    /dev/sdb5         1028096    21999615    10485760   83  Linux
    
    命令(输入 m 获取帮助):n
    Partition type:
       p   primary (1 primary, 1 extended, 2 free)
       l   logical (numbered from 5)
    Select (default p): l
    添加逻辑分区 6
    起始 扇区 (22001664-41943039,默认为 22001664):
    将使用默认值 22001664
    Last 扇区, +扇区 or +size{K,M,G} (22001664-41943039,默认为 41943039):
    将使用默认值 41943039
    分区 6 已设置为 Linux 类型,大小设为 9.5 GiB
    
    命令(输入 m 获取帮助):p
    
    磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x8ff20778
    
       设备 Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     1026047      512000   83  Linux
    /dev/sdb2         1026048    41943039    20458496    5  Extended
    /dev/sdb5         1028096    21999615    10485760   83  Linux
    /dev/sdb6        22001664    41943039     9970688   83  Linux
    
  8. 最后w保存退出

    命令(输入 m 获取帮助):w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    正在同步磁盘。
    
  9. 最后使用 partx -a /dev/sdb 命令重新读取一下修改后的磁盘信息

    partx -a /dev/sdb
    

二、parted

(1)支持msdos和gpt,可以支持大硬盘,也支持小硬盘。

  1. 这里我还是用sdb磁盘 先把原来分好删除 (如果你创建新的,就可以对新的磁盘parted 不用删除sdb)

    [root@localhost ~]# fdisk /dev/sdb
    欢迎使用 fdisk (util-linux 2.23.2)。
    
    更改将停留在内存中,直到您决定将更改写入磁盘。
    使用写入命令前请三思。
    
    
    命令(输入 m 获取帮助):d
    分区号 (1,2,5,6,默认 6):
    分区 6 已删除
    
    命令(输入 m 获取帮助):d
    分区号 (1,2,5,默认 5):
    分区 5 已删除
    
    命令(输入 m 获取帮助):d
    分区号 (1,2,默认 2):
    分区 2 已删除
    
    命令(输入 m 获取帮助):d
    已选择分区 1
    分区 1 已删除
    
    命令(输入 m 获取帮助):d
    No partition is defined yet!
    
    命令(输入 m 获取帮助):w   
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    正在同步磁盘。
    
  2. 先parted /dev/sdb 然后创建新的磁盘标签

    [root@localhost ~]# parted /dev/sdb
    GNU Parted 3.1
    使用 /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
      
      resizepart NUMBER END                    resize partition NUMBER
      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
    警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
    是/Yes/否/No? yes                                                         
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start  End  Size  File system  Name  标志
    
    (parted)   
    
  3. 创建主分区 起始点0 分配500M

    (parted) mkpart primary 0 500
    警告: The resulting partition is not properly aligned for best performance.
    忽略/Ignore/放弃/Cancel? Ignore
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End    Size   File system  Name     标志
     1      17.4kB  500MB  500MB               primary
    
    (parted)
    
  4. 创建逻辑分区 起始点501 10000M 也就是 10G

    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End    Size   File system  Name     标志
     1      17.4kB  500MB  500MB               primary
    
    (parted) mkpart logical 501 10000
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End     Size    File system  Name     标志
     1      17.4kB  500MB   500MB                primary
     2      501MB   10.0GB  9499MB               logical
    
  5. fdisk -l 查看

    [root@localhost ~]# fdisk -l
    
    磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x000e0f54
    
       设备 Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200    41943039    19921920   83  Linux
    WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
    
    磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:gpt
    Disk identifier: 185D8D08-E6FF-4DAC-BED7-2D4C310FCCDA
    
    
    #         Start          End    Size  Type            Name
    1           34       976562  476.8M  Microsoft basic primary
    2       978944     19531775    8.9G  Microsoft basic logical