使用 fdisk /dev/vdb 进入磁盘设备进行操作, 可以进行分区、保存分区表、打印分区表等等操作

  1. $ fdisk /dev/vdb
  2. Welcome to fdisk (util-linux 2.23.2).
  3. Changes will remain in memory only, until you decide to write them.
  4. Be careful before using the write command.
  5. Device does not contain a recognized partition table
  6. Building a new DOS disklabel with disk identifier 0x11171dc8.

以下是一些常用的命令:

  • m 打开帮助
  • n 添加新分区
  • p 打印分区表
  • w 保存分区表并退出
  • q 不保存分区表并退出
  • l 列出所有支持的分区格式

获取帮助 (m)

  1. Command (m for help): m
  2. Command action
  3. a toggle a bootable flag
  4. b edit bsd disklabel
  5. c toggle the dos compatibility flag
  6. d delete a partition
  7. g create a new empty GPT partition table
  8. G create an IRIX (SGI) partition table
  9. l list known partition types
  10. m print this menu
  11. n add a new partition
  12. o create a new empty DOS partition table
  13. p print the partition table
  14. q quit without saving changes
  15. s create a new empty Sun disklabel
  16. t change a partition's system id
  17. u change display/entry units
  18. v verify the partition table
  19. w write table to disk and exit
  20. x extra functionality (experts only)

列出所有支持的分区格式 (l)

  1. Command (m for help): l
  2. 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
  3. 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
  4. 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
  5. 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
  6. 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
  7. 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
  8. 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
  9. 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
  10. 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
  11. 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
  12. a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
  13. b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
  14. c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
  15. e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
  16. f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
  17. 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
  18. 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
  19. 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
  20. 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
  21. 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
  22. 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
  23. 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
  24. 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
  25. 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
  26. 1e Hidden W95 FAT1 80 Old Minix

创建新分区 (n)

  • Partition type 分区类型 (直接回车就好)
    • p: 主分区
    • e: 扩展分区
  • Partition number 分区序号 (直接回车就好)
  • First sector 起始扇区 (直接回车就好)
  • Last sector 分区大小, 输入 +100G 表示为此分区分配 100G 的空间
  1. Command (m for help): n
  2. Partition type:
  3. p primary (0 primary, 0 extended, 4 free)
  4. e extended
  5. Select (default p):
  6. Using default response p
  7. Partition number (1-4, default 1):
  8. First sector (2048-1048575999, default 2048):
  9. Using default value 2048
  10. Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999): +100G
  11. Partition 1 of type Linux and of size 100 GiB is set

多次输入 n 命令, 可对磁盘进行多个分区划分

打印分区表 (p)

创建完分区后, 使用 p 命令打印分区表

  1. Command (m for help): p
  2. Disk /dev/vdb: 536.9 GB, 536870912000 bytes, 1048576000 sectors
  3. Units = sectors of 1 * 512 = 512 bytes
  4. Sector size (logical/physical): 512 bytes / 512 bytes
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes
  6. Disk label type: dos
  7. Disk identifier: 0x11171dc8
  8. Device Boot Start End Blocks Id System
  9. /dev/vdb1 2048 209717247 104857600 83 Linux
  10. /dev/vdb2 209717248 419432447 104857600 83 Linux
  11. /dev/vdb3 419432448 629147647 104857600 83 Linux
  12. /dev/vdb4 629149696 1048575999 209713152 83 Linux

保存分区表 (w)

使用 w 命令对已划分的分区表进行保存

  1. Command (m for help): w
  2. The partition table has been altered!
  3. Calling ioctl() to re-read partition table.
  4. Syncing disks.

查看新的磁盘设备

创建完分区表并退出后, 使用 ls /dev/vdb* 可以看到, 已划分的4个分区都已出现

  1. $ ls /dev/vdb*
  2. /dev/vdb /dev/vdb1 /dev/vdb2 /dev/vdb3 /dev/vdb4

再次使用 fdisk -l 查看磁盘信息

  1. fdisk -l
  2. Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
  3. Units = sectors of 1 * 512 = 512 bytes
  4. Sector size (logical/physical): 512 bytes / 512 bytes
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes
  6. Disk label type: dos
  7. Disk identifier: 0x0008d73a
  8. Device Boot Start End Blocks Id System
  9. /dev/vda1 * 2048 83884031 41940992 83 Linux
  10. Disk /dev/vdb: 536.9 GB, 536870912000 bytes, 1048576000 sectors
  11. Units = sectors of 1 * 512 = 512 bytes
  12. Sector size (logical/physical): 512 bytes / 512 bytes
  13. I/O size (minimum/optimal): 512 bytes / 512 bytes
  14. Disk label type: dos
  15. Disk identifier: 0x11171dc8
  16. Device Boot Start End Blocks Id System
  17. /dev/vdb1 2048 209717247 104857600 83 Linux
  18. /dev/vdb2 209717248 419432447 104857600 83 Linux
  19. /dev/vdb3 419432448 629147647 104857600 83 Linux
  20. /dev/vdb4 629149696 1048575999 209713152 83 Linux
  21. Disk /dev/mapper/vg_f08a5cd7b4e31c2b93f7d6c385860882-brick_3d7dc9f1e6346efc7361d0f4ec4ddf8c: 2147 MB, 2147483648 bytes, 4194304 sectors
  22. Units = sectors of 1 * 512 = 512 bytes
  23. Sector size (logical/physical): 512 bytes / 512 bytes
  24. I/O size (minimum/optimal): 262144 bytes / 262144 bytes
  25. Disk /dev/mapper/vg_f08a5cd7b4e31c2b93f7d6c385860882-brick_0027aec42ae3a4b5171cc7fddea50bc0: 1073 MB, 1073741824 bytes, 2097152 sectors
  26. Units = sectors of 1 * 512 = 512 bytes
  27. Sector size (logical/physical): 512 bytes / 512 bytes
  28. I/O size (minimum/optimal): 262144 bytes / 262144 bytes

可以看到 /dev/vdb 的4个分区都已分配。

分区格式化

使用 mkfs 进行磁盘格式化, 通过 -t 选项指定文件系统的格式

  1. $ mkfs -t ext3 /dev/vdb1
  2. mke2fs 1.42.9 (28-Dec-2013)
  3. Filesystem label=
  4. OS type: Linux
  5. Block size=4096 (log=2)
  6. Fragment size=4096 (log=2)
  7. Stride=0 blocks, Stripe width=0 blocks
  8. 6553600 inodes, 26214400 blocks
  9. 1310720 blocks (5.00%) reserved for the super user
  10. First data block=0
  11. Maximum filesystem blocks=4294967296
  12. 800 block groups
  13. 32768 blocks per group, 32768 fragments per group
  14. 8192 inodes per group
  15. Superblock backups stored on blocks:
  16. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  17. 4096000, 7962624, 11239424, 20480000, 23887872
  18. Allocating group tables: done
  19. Writing inode tables: done
  20. Creating journal (32768 blocks): done
  21. Writing superblocks and filesystem accounting information:
  22. done