文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Linux系统下的分区和文件系统详解(3)

Linux系统下的分区和文件系统详解(3)

时间:2010-09-04  来源:mydear

使用 fdisk 进行分区

您刚刚已经看到如何使用 fdisk 命令来显示分区信息。这个命令还提供了一个菜单驱动的环境来编辑分区表,以便创建或移除分区。

警告

开始修改分区之前,需要记住一些重要事项。如果不遵守以下这些原则,就会出现丢失您的现有数据的风险。

  1. 开始之前备份重要数据,进行任何可能会导致数据丢失的操作之前都应这样做。
  2. 不要更改正在使用的分区。事先详细计划,然后小心执行。从 CD、DVD 或 USB 启动一个实时发行版是确保没有硬盘驱动器分区正在使用的一个好办法。
  3. 了解您的工具。fdisk 命令不会向您的磁盘提交任何更改,除非您要求它那样做。其他工具,其中包括 parted 可能会随着您的操作提交更改。
  4. 如果的确犯了错误,那么应立即停止。分区工具对分区表进行写操作。除非您使用的工具还包含对您的磁盘的数据区域的移动、大小重置、格式化等写操作功能,否则您的数据将不会受到影响。如果您的确犯了错误,尽快停止操作并寻求帮助。您仍然有可能恢复此前的分区表定义,从而恢复您的分区和数据。

启动 fdisk

要以交互模式启动 fdisk,只需提供一个磁盘的名称(比如 /dev/hda 或 /dev/sdb)作为参数。以下示例启动一个 Knoppix 实时 DVD。您需要根权限,并将看到类似于清单 3 的输出。

清单 3. 启动交互式 fdisk

knoppix@Microknoppix:~$ su -
root@Microknoppix:~# fdisk /dev/sda

The number of cylinders for this disk is set to 121601.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help):

大多数磁盘拥有超过 1024 个柱面,因此您通常会看到清单 3 中显示的警告。类型 m 将显示一个可用的单字母命令列表,如清单 4 所示。

清单 4. fdisk 中的帮助

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
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)

Command (m for help):

使用 p 命令来显示这个特定磁盘上的现有分区,清单 5 显示输出。

清单 5. 显示现有分区表

Command (m for help): p

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000de20f

Device Boot Start End Blocks Id System
/dev/sda1 * 1 9111 73184076 7 HPFS/NTFS
/dev/sda2 9634 9730 779152+ 83 Linux
/dev/sda3 9731 116679 859067842+ 5 Extended
/dev/sda5 9731 20917 89859546 83 Linux
/dev/sda6 20918 39644 150424596 83 Linux
/dev/sda7 39645 53905 114551451 83 Linux

Command (m for help):

这个特殊磁盘是一个 1TB 磁盘,包含一个略低于 80GB 的 Windows/XP 分区。它是一个主分区,标记为 bootable,这是 Windows 系统的典型特征。

添加分区

现在我们使用部分闲置空间来添加一些分区。

  1. 我们将创建一个交换分区:/dev/sda4。这将是一个主分区,填充 /dev/sda1 的结束柱面和 /dev/sda2 的起始柱面之间的 521 个柱面空隙。您不必对导致这个空隙的奇怪原因感到好奇;我只是为了撰写本文而创建这个空隙。
  2. 我们将创建一个 40GB 的逻辑分区:/dev/sda8。
  3. 最后,我们将创建一个 2000MB 的小型逻辑分区,以便在 Linux 和 Windows 系统之间共享数据。这个分区最终将格式化为 FAT32(或 vfat)。这个分区将命名为 /dev/sda9。

创建我们的分区

我们首先使用 n 命令来创建一个新分区,如清单 6 所示。

清单 6. 创建第一个分区

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
p
Selected partition 4
First cylinder (9112-121601, default 9112):
Using default value 9112
Last cylinder, +cylinders or +size{K,M,G} (9112-9633, default 9633): +521

Command (m for help): p

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000de20f

Device Boot Start End Blocks Id System
/dev/sda1 * 1 9111 73184076 7 HPFS/NTFS
/dev/sda2 9634 9730 779152+ 83 Linux
/dev/sda3 9731 116679 859067842+ 5 Extended
/dev/sda4 9112 9633 4192965 83 Linux
/dev/sda5 9731 20917 89859546 83 Linux
/dev/sda6 20918 39644 150424596 83 Linux
/dev/sda7 39645 53905 114551451 83 Linux

Partition table entries are not in disk order

Command (m for help):

我们采用默认值作为第一个柱面并指定值 +521 作为柱面的数量。清单 6 显示,我们的分区大小约为 4GB。由于它是一个主分区,因此它的编号必须为 1 至 4。顺序指定分区编号是个好主意;否则一些工具会报告错误,fdisk 将警告我们:我们的分区表条目不再按磁盘顺序排列。

还要注意一点:我们的新分区类型被指定为 83,表明这是一个 Linux 数据分区。可以将这个数字视为分区预定用途的操作系统的指示符。分区的最终用途应该匹配这个设置,但这时我们的分区甚至还没有格式化,更谈不上有数据了。 我们将首先创建其他分区,然后介绍如何更改分区类型。

您可能已经注意到,当我们输入 n 子命令创建一个新分区时,惟一的选择是 “l”(表示逻辑分区)和 “p”(表示主分区)。您只能看到剩余的可能分区类型的选项。如果驱动器还没有扩展分区,您将看到选项 “e”(表示扩展分区)。还要注意,我们的扩展分区(/dev/sda3)为类型 5。

现在我们来定义 40GB Linux 分区和 2000MB FAT32 分区。这次我们只需指定大小 +40G 和 +2000M,分别表示 40GB 和 2000MB。结果如清单 7 所示。

清单 7. 创建数据分区

Command (m for help): n
First cylinder (53906-116679, default 53906):
Using default value 53906
Last cylinder, +cylinders or +size{K,M,G} (53906-116679, default 116679): +40G

Command (m for help): n
First cylinder (59129-116679, default 59129):
Using default value 59129
Last cylinder, +cylinders or +size{K,M,G} (59129-116679, default 116679): +2000M

Command (m for help): p

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000de20f

Device Boot Start End Blocks Id System
/dev/sda1 * 1 9111 73184076 7 HPFS/NTFS
/dev/sda2 9634 9730 779152+ 83 Linux
/dev/sda3 9731 116679 859067842+ 5 Extended
/dev/sda4 9112 9633 4192965 83 Linux
/dev/sda5 9731 20917 89859546 83 Linux
/dev/sda6 20918 39644 150424596 83 Linux
/dev/sda7 39645 53905 114551451 83 Linux
/dev/sda8 53906 59128 41953716 83 Linux
/dev/sda9 59129 59384 2056288+ 83 Linux

Partition table entries are not in disk order

Command (m for help):

更改分区类型

在离开 fdisk 之前,我们将更改 swap 和 vfat 分区的分区类型。为此,我们使用 t 子命令来设置分区类型。我们将 /dev/sda4 设置为类型 82(Linux 交换分区),将 /dev/sda9 设置为类型 9(FAT32)。如果您想查看受支持类型的完整列表,请输入 L,如清单 8 所示。

清单 8. 更改分区类型

Command (m for help): t
Partition number (1-9): 4
Hex code (type L to list codes): 82
Changed system type of partition 4 to 82 (Linux swap / Solaris)

Command (m for help): t
Partition number (1-9): 9
Hex code (type L to list codes): b
Changed system type of partition 9 to b (W95 FAT32)

Command (m for help):

保存分区表

到目前为止,我们只是对分区表进行了内存中编辑。我们可以用 q 命令退出,不保存更改。如果某些分区不能满足您的要求,您可以使用 d 命令来删除一个或多个分区,以便重新定义它们。如果您对设置满意,则可以使用 v 命令来验证您的设置,然后使用 w 命令来写入新的分区表并退出。参见清单 9。如果您再次运行 fdisk -l 命令,您将看到,Linux 现在已能够识别新分区。与其他操作系统不同的是,Linux 并不总是需要重新启动才能看到更改。举例来说,如果 /dev/hda3 变成 /dev/hda2,则需要重新启动,因为原来的 /dev/hda2 已被删除。如果需要重新启动,fdisk 应该会告诉您。

清单 9. 保存分区表

Command (m for help): v
999521580 unallocated 512-byte sectors

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.
root@Microknoppix:~# fdisk -l /dev/sda

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000de20f

Device Boot Start End Blocks Id System
/dev/sda1 * 1 9111 73184076 7 HPFS/NTFS
/dev/sda2 9634 9730 779152+ 83 Linux
/dev/sda3 9731 116679 859067842+ 5 Extended
/dev/sda4 9112 9633 4192965 82 Linux swap / Solaris
/dev/sda5 9731 20917 89859546 83 Linux
/dev/sda6 20918 39644 150424596 83 Linux
/dev/sda7 39645 53905 114551451 83 Linux
/dev/sda8 53906 59128 41953716 83 Linux
/dev/sda9 59129 59384 2056288+ b W95 FAT32

Partition table entries are not in disk order

高级 fdisk

您可能已注意到,我们没有更改任意分区上的 bootable 标志。现在,我们的磁盘还有 Windows Master Boot Record (MBR),因此将启动标记为 bootable 的第一个主分区(我们的示例中为 NTFS 分区)。

LILO 和 GRUB 都不使用 bootable 标志。如果其中一个安装在 MBR 中,那么它就能够引导 Windows/XP 分区。您也可以将 LILO 或 GRUB 安装到您的 /boot 分区(/dev/hda2)中,将该分区标记为 bootable 并移除 /dev/hda1 的 bootable 标志。如果机器今后将恢复为 Windows 专用机器,那么保留原始 MBR 可能会有用。

如果需要,您还可以使用 fdisk 来修复分区表中的分区顺序。这通常将会更改分区编号,因此您可能需要完成其他工作才能将您的系统恢复为工作系统。要进行这个更改,使用 f 子命令切换到专家模式,然后使用 r 子命令修复分区顺序,如清单 10 所示。如果您只是想看看新的分区顺序而不实际更改,可以使用 q 子命令退出(本例的做法),而不是将更新后的分区表写到磁盘中。

清单 10. 修复分区表顺序

Command (m for help): p

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000de20f

Device Boot Start End Blocks Id System
/dev/sda1 * 1 9111 73184076 7 HPFS/NTFS
/dev/sda2 9634 9730 779152+ 83 Linux
/dev/sda3 9731 116679 859067842+ 5 Extended
/dev/sda4 9112 9633 4192965 82 Linux swap / Solaris
/dev/sda5 9731 20917 89859546 83 Linux
/dev/sda6 20918 39644 150424596 83 Linux
/dev/sda7 39645 53905 114551451 83 Linux
/dev/sda8 53906 59128 41953716 83 Linux
/dev/sda9 59129 59384 2056288+ b W95 FAT32

Partition table entries are not in disk order

Command (m for help): x

Expert command (m for help): f
Done.

Expert command (m for help): r

Command (m for help): p

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000de20f

Device Boot Start End Blocks Id System
/dev/sda1 * 1 9111 73184076 7 HPFS/NTFS
/dev/sda2 9112 9633 4192965 82 Linux swap / Solaris
/dev/sda3 9634 9730 779152+ 83 Linux
/dev/sda4 9731 116679 859067842+ 5 Extended
/dev/sda5 9731 20917 89859546 83 Linux
/dev/sda6 20918 39644 150424596 83 Linux
/dev/sda7 39645 53905 114551451 83 Linux
/dev/sda8 53906 59128 41953716 83 Linux
/dev/sda9 59129 59384 2056288+ b W95 FAT32

Command (m for help): q
您现在已经了解了向 Linux 工作站添加分区的一种方法。
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载