Linux-硬盘分区和分区工具(一)

当新的硬件设备被添加到Linux中时,系统能自动识别,但新添加的硬盘必须要进行分区、格式化和挂载后才能使用。

MBR分区和fdisk

查看系统硬盘信息

fdisk -l
#硬盘sda的分区信息:
#sda被分成两个主分区sda1和sda3,一个swap分区sda2,一个扩展分区sda4,其中包含逻辑分区sda5
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 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 identifier: 0x000e50ae

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
/dev/sda3         4605952     8701951     2048000   83  Linux
/dev/sda4         8701952    41943039    16620544    5  Extended
/dev/sda5         8704000    41943039    16619520   83  Linux

#硬盘sdb的信息:8GB空间,未分区
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders, total 16777216 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 identifier: 0x740e36e3

   Device Boot      Start         End      Blocks   Id  System

进入硬盘sdb的分区模式

fdisk /dev/sdb
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').
#命令u:以扇区sector为单位分区
Command (m for help): u
Changing display/entry units to sectors

#划分主分区sdb1,并分配2GB空间
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (63-16777215, default 63): #直接回车,采用默认值
Using default value 63
Last sector, +sectors or +size{K,M,G} (63-16777215, default 16777215): +2G

#划分扩展分区sdb2,并分配剩余所有磁盘空间
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 2
First sector (4194368-16777215, default 4194368): 
Using default value 4194368
Last sector, +sectors or +size{K,M,G} (4194368-16777215, default 16777215): 
Using default value 16777215

#划分逻辑分区sdb5,并分配2GB磁盘空间
Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First sector (4194431-16777215, default 4194431): 
Using default value 4194431
Last sector, +sectors or +size{K,M,G} (4194431-16777215, default 16777215): +2G

检查分区方案,将其写入磁盘分区表中

#命令p:打印磁盘分区方案(比上一步新增逻辑分区sdb6分配剩余所有空间)
Command (m for help): p

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders, total 16777216 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 identifier: 0x740e36e3

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1              63     4194367     2097152+  83  Linux
/dev/sdb2         4194368    16777215     6291424    5  Extended
/dev/sdb5         4194431     8388735     2097152+  83  Linux
/dev/sdb6         8388799    16777215     4194208+  83  Linux

#命令w:将分区信息写入磁盘并退出
Command (m for help): w
The partition table has been altered!

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

p.s.

  • fdisk分区工具采用交互式,即系统提问、用户作答响应的方式进行分区;
  • 简单易懂、不易出错,但相较命令方式慢、效率低下;
  • 可以随时使用m命令调用help;
  • 分区结束不是即时生效的,要调用w命令将分区方案写入磁盘分区表。

你可能感兴趣的:(Linux)