linux's docs

7.2.0: 磁盘管理-分区


1、磁盘分区

1.1 fdisk
作用:查看磁盘情况、管理磁盘分区
语法:
查看磁盘状态
fdisk -l [磁盘名称] 进入fdisk工具界面执行分区操作
fdisk 磁盘名称

fdisk工具箱用法:
"fdisk 磁盘名称"进入fdisk工具,其中选项:

  • m 帮助,列出所有选项及其解释
  • p "print"列出当前磁盘的所有分区
  • d "delete"删除分区,接下来需要输入需要删除的分区号
  • n "new"新建分区,接下来需要按提示输入分区类型、分区号、分区大小
  • w "write"讲创建的分区表写入磁盘
  • q "quit"退出
# 进入fdisk工具箱
fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xd6b2d026.
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').

# 新建主分区sdb1
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +5G

# 打印目前分区情况
Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0xd6b2d026

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

1.2 parted
作用:

  • 查看磁盘情况
  • 管理磁盘分区
  • 专门针对大容量磁盘

与fdisk的不同点:

  • parted支持2TB以上容量的硬盘,而fdisk只支持2TB以下容量的硬盘;
  • parted可不指定目标进入工具,然后在select 目标硬盘,而fdisk必须指定目标硬盘名称才可进入分区模式;
  • parted是实时分区,而fdisk在输入w命令后才开始写入分区表。

语法:
查看磁盘信息

parted -l
parted dev_name print
parted dev_name unit mb print
parted dev_name unit gb print

主要语法
parted dev_name [command [参数]]

交互模式常用选项(由help命令可列出):

  • select 选择需要操作的disk(select /dev/sdb);
  • mkpart 分区类型 [FS-TYPE] START END 分区命令(mkpart primary ext4 0GB 2GB);
  • print 显示磁盘分区信息;
  • rm NUMBER 按照分区号码删除分区(rm 5),number可以用print看出;
  • quit 退出parted工具。

用法举例:

# 查看磁盘情况
parted /dev/sdb unit gb print
Model: ATA CentOS6-1 SSD (scsi)
Disk /dev/sdb: 5.37GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos

Number  Start  End  Size  Type  File system  Flags

# 修改分区表为gpt(慎用,无法复原)
parted /dev/sdb mklabel gpt
Warning: 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? y                                                                 
Information: You may need to update /etc/fstab.                           

parted /dev/sdb print | grep Partition
Partition Table: gpt

# 创建分区
# 语法:parted dev_name part_type fs_type start end
# dev_name,设备名称,如果未制定,则使用找到的第一个设备
# part_type,分区类型,"primary", "logical", or "extended"
# fs_type,分区格式
# "fat16","fat32", "ext2", "HFS", "linux-swap", "NTFS",
# "reiserfs",or  "ufs"
parted /dev/sdb mkpart primary ext4 0GB 1GB
Information: You may need to update /etc/fstab.                           

parted /dev/sdb mkpart primary xfs 1GB 2GB
Information: You may need to update /etc/fstab.                           

parted /dev/sdb unit mb print
Model: ATA CentOS6-1 SSD (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt

Number  Start   End     Size   File system  Name     Flags
 1      1.05MB  1000MB  999MB               primary
 2      1000MB  2000MB  999MB               primaryPS:

超过2T的硬盘如果不希望容量浪费(MBR最大识别2T),只能将磁盘转化成GPT格式;
传统系统盘是BIOS+MBR,如果希望GPT做系统盘,linux下需要UEFI替代BIOS,采用UEFI+GPT的方式;
MBR分区表占64字节,采用主+扩展<=4,而GPT只有16字节,类似于扩展分区,真正起作用的分区表在512字节之后,因此对GPT分区表来说是没有4个主分区的限制。

Contents