磁盘分区和格式化

一、分区工具 fdisk

命令使用
fdisk 磁盘       :注意一定是要磁盘,不能是磁盘的分区
fdisk /dev/sdb  :不能是 fdisk /dev/sdb1

d :删除分区
n :创建分区
p :显示分区
l  :显示文件类型

1.例:
root@ubuntu:~# 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').

Command (m for help): p

Disk /dev/sdb: 16.0 GB, 15971909632 bytes
64 heads, 32 sectors/track, 15232 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      102384    c  W95 FAT32 (LBA)
/dev/sdb2             101         200      102400    c  W95 FAT32 (LBA)
/dev/sdb3             201         300      102400    c  W95 FAT32 (LBA)
/dev/sdb4             301         400      102400    5  Extended

Command (m for help): d
Partition number (1-5): 1

Command (m for help): d
Partition number (1-5): 2

Command (m for help): d
Partition number (1-5): 3

Command (m for help): d
Selected partition
Command (m for help): p

Disk /dev/sdb: 16.0 GB, 15971909632 bytes
64 heads, 32 sectors/track, 15232 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b

   Device Boot      Start         End      Blocks   Id  System
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-15232, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-15232, default 15232): 15232

Command (m for help): p

Disk /dev/sdb: 16.0 GB, 15971909632 bytes
64 heads, 32 sectors/track, 15232 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       15232    15597552   83  Linux
Command (m for help): w
The partition table has been altered!

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

2.linux 1块磁盘最多有4个主分区。
  MBR(Master Boot Recorder)主要开机扇区,放置硬盘的信息。MBR 可以说是整个硬盘最重要的地方了,因为在 MBR 里面记录了两个重要的东西,分别是:开机管理程序,与磁盘分区表 ( partition table )。所以我们所做的硬盘分区,也就是在修改partition table。

    由于这个 MBR 区块的容量有限,所以,当初设计的时候,就只有设计成 4 个分区纪录,这些分区记录就被称为 Primary ( 主分区 ) 及 Extended ( 扩展分区 ) ,也就是说,一颗硬盘最多可以有 4 个 Primary + Extended 的扇区,其中,Extended 只能有一个,因此,你如果要分区成四块磁盘分区的话,那么最多就是可以:
    P + P + P + P
    P + P + P + E
的情况来分区了。需要特别留意的是,如果上面的情况中, 3P +E 只有三个『可用』的磁盘,如果要四个都『可用』,就得分区成 4P 了!( 因为 Extended 不能直接被使用,还需要分区成 Logical 才行)

    思考一:如果我要将我的大硬盘暂时分区成四个 partition ,同时,还有其它的空间可以让我在未来的时候进行规划,那么该如何分区?

    由刚刚的说明,我们可以知道, Primary + Extended 最多只能有四个 partition,而如果要超过 5 个 partition 的话,那么就需要 Extended 的帮忙。因此,在这个例子中,我们千万不能分区成四个 Primary 为什么呢?假如您是一个 20 GB 的硬盘,而 4 个 primary 共用去了 15 GB ,您心想还有 5 GB 可以利用对吧?错!剩下的 5 GB 完全不能使用,这是因为已经没有多余的 partition table 纪录区可以记录了,因此也就无法进行额外的分区,当然啰,空间也就被浪费掉了!因此,请千万注意,如果您要分区超过 4 槽以上时,请记得一定要有 Extended 分区区,而且必须将所有剩下的空间都分配给 Extended ,然后再以 logical 的分区区来规划 Extended 的空间。

   思考二:我可不可以仅分区 1 个 Primary 与 1 个 Extended 呢?

    当然可以!基本上, Logical 可以有 64 个,因此,你可以仅分区一个主分区,并且将所有其它的分区都给 Extended ,利用 Logical 分区来进行其它的 partition 规划即可!

    思考三:假如我的硬盘安装在 IDE 1 的 Master ,并且我想要分区成 6 个可以使用的硬盘扇区,那么每个磁盘在 Linux 底下的代号为何?

    说明:

    由于硬盘在 Primary + Extended 最多可以有四个,因此,在 Linux 底下,已经将 partition table 1 ~ 4 先留下来了,如果只用了 2 个 P + E 的话,那么将会空出两个 partition number 呦!再详细的说明一下,假设我将四个 P + E 都用完了。其中Extended /dev/hda4包括三个逻辑分区。

    实际可以使用的是 /dev/hda1, /dev/hda2, /dev/hda3, /dev/hda5, /dev/hda6, /dev/hda7 这六个 partition!至于 /dev/hda4 这个 Extended 扇区本身仅是用来规划出让 Logical 可以利用的磁盘空间而已!

    那么万一我只想要分区 1 个 Primary 与 1 个 Extended 呢?

    因为 1~4 号已经被预留下来了,所以第一个 Logical 的代号由 5 号开始计算起来,而后面在被规划的,就以累加的方式增加磁盘代号啰!而其中 /dev/hda3, /dev/hda4 则是空的,被保留下来的代号。


Disk /dev/sda: 1497.2 GB, 1497198755840 bytes
255 heads, 63 sectors/track, 182024 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: 0x00054c4e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          37      291840   83  Linux
/dev/sda2              37        6116    48828416   83  Linux
/dev/sda3            6116       12195    48828416   83  Linux
/dev/sda4           12195      182024  1364156417    5  Extended
/dev/sda5           12195       23866    93749248   82  Linux swap / Solaris
/dev/sda6           23866       29945    48827392   83  Linux
/dev/sda7           29945      182024  1221577728   83  Linux

使用扩展分区,分7个区


二、格式化工具 mkfs

mkfs相当于DOS/Windows中的格式化命令。
不同的是Linux可以支持ext2/dos/vfat/等分区的格式化工作,
不像Windows通常只能格式化 DOS/FAT32/FAT16/NTFS分区。

mkdosfs一看就知道是DOS格式的;

mke2fs格式化ext2文件系统;

mkswap格式化Linux 的交换分区。

猜你喜欢

转载自fujinbing.iteye.com/blog/1153371