fdisk 命令

认识磁盘

命令 lsblk, 输出如下:

[root@localhost wangxiao]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   40G  0 disk
└─sdb1            8:17   0   40G  0 part /home/lfs/future
sr0              11:0    1  4.5G  0 rom
  • sda 表示第一块硬盘

  • sdb 表示第二块硬盘,依次类推,cdefg…

  • sr0 第一个SCSI CD-ROM

除了常见的外,还有驱动之类的,/dev/fd0 etc,均不常用,知晓sda/sdb/sdc是属于磁盘即可。

fdisk 命令

[root@localhost bin]# fdisk -h
Usage:
 fdisk [options] <disk>    change partition table
 fdisk [options] -l <disk> list partition table(s)
 fdisk -s <partition>      give partition size(s) in blocks

Options:
 -b <size>             sector size (512, 1024, 2048 or 4096)
 -c[=<mode>]           compatible mode: 'dos' or 'nondos' (default)
 -h                    print this help text
 -u[=<unit>]           display units: 'cylinders' or 'sectors' (default)
 -v                    print program version
 -C <number>           specify the number of cylinders
 -H <number>           specify the number of heads
 -S <number>           specify the number of sectors per track
  • 指 /dev/sda、/dev/sda1、/dev/sdb 等等具体磁盘

  • [option] 参数不常用

  • -l/-s 也不常用

  • fdisk [options] 用于操作某个具体的 disk

fdisk [options] 具体使用

这里以 /dev/sda 为例

[root@localhost bin]# fdisk -l /dev/sda

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009e004

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
[root@localhost bin]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help):  m
Command action
   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)
  • 输入m,查看所有的操作指令

  • 常用 n(创建一个新的分区)d(删除一个分区)

按照提示,输入指定含义字母,最后输入 w进行保存生效。

案例:加入一块磁盘 /dev/sdb

进入分配: fdisk /dev/sdb 新建分区 n 选择分区类型: p:主分区,e:扩展分区,空格默认主分区 p 选择起始范围:磁盘有多个扇区组成,输入数字也即起始分区点

选择截至范围:磁盘有多个扇区组成,输入数字也即结束分区点

写入,并完成分区,在/dev目录下就会多出一个 /dev/sdb1 w

格式化/dev/sdb1

mkfs[tab][tab] 可以显示支持的文件系统类型

mkfs -t ext4 /dev/sdb1

挂载到某个目录

mount /dev/sdb1 /opt

取消挂载 umount /deb/sdb1