分区磁盘和格式化磁盘
Table of Contents
1 fdisk 分区介绍
分区是将一个硬盘驱动器分成若干个逻辑驱动器,分区是把硬盘连续的区块当做一个独立 的磁硬使用。分区表是一个硬盘分区的索引,分区的信息都会写进分区表。
sudo fdisk -l /dev/sdc
Disk /dev/sdc: 465.8 GiB, 500107862016 bytes, 976773168 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 33553920 bytes Disklabel type: dos Disk identifier: 0x0ead1311 Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 976773119 976771072 465.8G 83 Linux
fdisk 的常见命令可以通过 m 指令来打印出来
$ sudo fdisk /dev/sdc Welcome to fdisk (util-linux 2.31.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): m Help: DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition Misc m print this menu u change display/entry units x extra functionality (experts only) Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file Save & Exit w write table to disk and exit q quit without saving changes Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table
也可以使用 p 命令查看当前分区的状态
sudo fdisk /dev/sdc Welcome to fdisk (util-linux 2.31.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/sdc: 465.8 GiB, 500107862016 bytes, 976773168 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 33553920 bytes Disklabel type: dos Disk identifier: 0x0ead1311 Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 976773119 976771072 465.8G 83 Linux
最后是 w 命令来保存设置, 如果不想保存可以使用 q 退出
2 MBR 和 GPT 分区表对比
场景的分区表有 MBR 和 GPT 两种类型,他们之间的对比如下:
- 兼容性:MBR 是 IBM 公司早年提出的,兼容性普遍比 GPT 要好,但是目前基本主板 都支持 GPT 格式磁盘的启动方式
- 启动方式:MBR 是一般 BIOS 启动的默认 (Legacy) 使用格式,而 GPT 分区需要主板 支持 UEFI 模式启动
- 分区数量:MBR 最多支持 4 个主分区,GPT 没有限制,理论上可以支持无数个
- 磁盘容量:MBR 最多支持 2T 的容量,而 GPT 支持容量没有限制
3 使用 fdisk 分区
对磁盘分区的操作过程如下
sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.31.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. # 使用 g 将分区表修改成 GPT Command (m for help): g Created a new GPT disklabel (GUID: 4665D90B-8962-994B-B8DF-A6F912AED4FE). The old dos signature will be removed by a write command. # 使用 n 来新建分区,全部使用默认值 Command (m for help): n Partition number (1-128, default 1): First sector (65535-976773134, default 65535): Last sector, +sectors or +size{K,M,G,T,P} (65535-976773134, default 976773134): Created a new partition 1 of type 'Linux filesystem' and of size 465.7 GiB. Command (m for help): p Disk /dev/sdc: 465.8 GiB, 500107862016 bytes, 976773168 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 33553920 bytes Disklabel type: gpt Disk identifier: 4665D90B-8962-994B-B8DF-A6F912AED4FE Device Start End Sectors Size Type /dev/sdc1 65535 976773134 976707600 465.7G Linux filesystem # 保存应用并退出 Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
在第一个分区中新建 ext4 文件系统
sudo mkfs.ext4 /dev/sdc1
mke2fs 1.44.1 (24-Mar-2018) Creating filesystem with 122088450 4k blocks and 30523392 inodes Filesystem UUID: 7b37437c-9ad2-4dd0-b36d-cfc05866c9b9 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000 Allocating group tables: done Writing inode tables: done Creating journal (262144 blocks): done Writing superblocks and filesystem accounting information: done