扩展分区

扩展分区采用主分区可以进行的分配之一。但是,扩展分区不能包含可用数据。相反,它包含引用逻辑分区。您最多可以有12个逻辑分区。物理磁盘上仅允许一个扩展分区。

分区布局的示例

要显示当前分区布局,您可以以root身份执行命令fdisk -l

root@john-desktop:~# fdisk -l /dev/sda

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders, total 312581808 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: 0x000132a7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   307357695   153677824   83  Linux
/dev/sda2       307359742   312580095     2610177    5  Extended
/dev/sda5       307359744   312580095     2610176   82  Linux swap / Solaris

什么是Linux文件系统?

文件系统是存储设备上的数据和元数据的组织。基本上,文件系统包含文件,目录和子目录。Linux具有树状结构,其根目录为/。您在块设备或磁盘上创建的每个分区都将添加一个文件系统。当您在称为挂载点的点将文件系统挂载到设备上时,将创建树形视图结构。在下面的示例中,这将变得更加清楚。
该树状结构中目录的一般布局在我们的文件层级标准部分中介绍。

逻辑分区

逻辑分区存在于扩展分区中。逻辑分区的编号从5到16(共12个)。

主分区

如前所述,磁盘上最多可以有四个主分区,这些分区通常编号为:

/ dev / hda1
/ dev / hda2
/ dev / hda3
/ dev / hda4

分区和文件系统

如何在Linux系统上创建文件系统,使用fdisk,使用mkfs创建文件系统,挂载和卸载文件系统。

用fdisk分区新磁盘

fdisk是与大多数Linux发行版捆绑在一起的最受欢迎的分区工具之一。

要使用fdisk,您将需要使用提升的特权或以root用户身份运行。通过键入fdisk以及要使用的设备来启动fdisk

警告-fdisk是一个非常强大的命令,可以完全破坏有效的工作分区。只有当你100%确定自己要做什么时,才使用此命令。

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)

查看当前配置:

[root@centos ~]# fdisk -l

Disk /dev/sda: 9663 MB, 9663676416 bytes
255 heads, 63 sectors/track, 1174 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: 0x00095943

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1175     8924160   8e  Linux LVM

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x00000000


Disk /dev/mapper/vg_centos-lv_root: 7021 MB, 7021264896 bytes
255 heads, 63 sectors/track, 853 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: 0x00000000


Disk /dev/mapper/vg_centos-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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: 0x00000000

从上面我们可以看到,我们的CentOS系统上有两个设备。/dev/sda/dev/sdb。第一个设备/dev/sda是一个9GB磁盘,其中包含两个分区sda1和sda2。第二个设备/dev/sdb的大小为2GB,没有分区表。这是我们要分区的磁盘。

在下面的示例中,我们将在2GB磁盘上创建七个分区。2147MB除以七个分区大约相当于每个分区300MB。
基于以上所述,我有261个扇区,因此261个扇区除以7个分区,每个分区大约有37个扇区。

我有4个主分区,其中3个分区为37个扇区柱面。扩展分区将为4 * 37 = 148个扇区柱面。

划分主分区和扩展分区

[root@centos ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xb85002be.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): 37

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (38-261, default 38): 38
Last cylinder, +cylinders or +size{K,M,G} (38-261, default 261): 75

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (76-261, default 76): 76
Last cylinder, +cylinders or +size{K,M,G} (76-261, default 261): 113

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Selected partition 4
First cylinder (114-261, default 114): 114
Last cylinder, +cylinders or +size{K,M,G} (114-261, default 261):
Using default value 261

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0xb85002be

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          37      297171   83  Linux
/dev/sdb2              38          75      305235   83  Linux
/dev/sdb3              76         113      305235   83  Linux
/dev/sdb4             114         261     1188810    5  Extended

现在已经创建了四个分区(三个主分区和一个扩展分区)

创建逻辑分区

Command (m for help): n
First cylinder (114-261, default 114): 114
Last cylinder, +cylinders or +size{K,M,G} (114-261, default 261): 151

Command (m for help): n
First cylinder (152-261, default 152): 152
Last cylinder, +cylinders or +size{K,M,G} (152-261, default 261): 189

Command (m for help): n
First cylinder (190-261, default 190): 190
Last cylinder, +cylinders or +size{K,M,G} (190-261, default 261): 227

Command (m for help): n
First cylinder (228-261, default 228): 228
Last cylinder, +cylinders or +size{K,M,G} (228-261, default 261): 261

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0xb85002be

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          37      297171   83  Linux
/dev/sdb2              38          75      305235   83  Linux
/dev/sdb3              76         113      305235   83  Linux
/dev/sdb4             114         261     1188810    5  Extended
/dev/sdb5             114         151      305203+  83  Linux
/dev/sdb6             152         189      305203+  83  Linux
/dev/sdb7             190         227      305203+  83  Linux
/dev/sdb8             228         261      273073+  83  Linux


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

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

完成逻辑分区的创建后,我们可以通过从菜单中选择P选项来检查布局。这将显示我们当前的分区表。重要的是要记住,此时我们还没有编写这些更改,它们仅在内存中。为了使更改永久生效,我们需要执行w命令以写出新的分区表。

磁盘和Linux

Linux操作系统可以支持许多不同类型的磁盘。Linux可以处理IDE、SCSI、cd、dvd和可移动媒体,如USB记忆棒和外部驱动器。最常见的磁盘类型是IDE和较新的SATA磁盘。原来的IDE标准可以在一个连接上支持两个磁盘。这些联系被称为主人奴隶。许多pc还具有第二个IDE连接,允许访问另外两个次要设备。另一种不太流行的pc使用的磁盘类型是SCSI。虽然这些通常要快得多,但它们的价格也要高得多。最近几年出现了一种新的标准,叫做萨塔。SATA是IDE的现代版本。但是,在Linux下,它们将具有SCSI磁盘(SCSI仿真层)使用的相同命名标准。

创建挂载点

挂载点基本上是连接到当前文件系统的空目录。创建之后,我们可以将新文件系统附加到这一点。

挂载文件系统时,基本上是在向系统指示要在给定的挂载点挂载哪个分区/设备。

mount语法

mount 文件系统  挂载点

挂载文件系统

[root@centos /]# mkdir test{1..7}

[root@centos /]# ls -l

drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test1
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test2
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test3
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test4
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test5
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test6
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test7
[root@centos /]# mount /dev/sdb1 /test1
[root@centos /]# mount /dev/sdb2 /test2
[root@centos /]# mount /dev/sdb3 /test3
[root@centos /]# mount /dev/sdb5 /test4
[root@centos /]# mount /dev/sdb6 /test5
[root@centos /]# mount /dev/sdb7 /test6
[root@centos /]# mount /dev/sdb8 /test7

检查文件系统

[root@centos /]# df

配置文件系统自动挂载

/etc/fstab中添加新条目。系统重启后,它将自动挂载我们的文件系统。

详细请看下一教程。

Disk naming conventions

IDE 设备

/dev/hda 主通道的主IDE硬盘
/dev/hdb 主通道的副IDE硬盘
/dev/hdc 副通道的主IDE硬盘
/dev/hdd 副通道的副IDE硬盘

SCSI设备

/dev/sda 第一块SCSI驱动设备
/dev/sdb 第二块SCSI驱动设备
/dev/sdc 第三块SCSI驱动设备

值得注意的是,SCSI设备不像IDE设备那样有四个设备限制。还有一些Linux发行版使用SCSI仿真层来处理磁盘,即使它们本身可能是IDE硬盘!

在分区上创建文件系统

使用我们选择的文件系统格式化这些分区。用于创建文件系统的命令是mkfs

mkfs语法

mkfs [选项] [-t类型fs-options]设备[大小]

在新分区上创建文件系统

只有主分区和逻辑分区才需要文件系统。

[root@centos ~]# mkfs.ext3 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
74296 inodes, 297168 blocks
14858 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
37 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
    8193, 24577, 40961, 57345, 73729, 204801, 221185

Writing inode tables: done                           
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

在分区sdb1上创建Ext3的文件系统类型。我们将对每个分区(sdb2,sdb3,sdb5,sdb6,sdb7,sdb8)重复此过程。

分区

分区是将硬盘分割成片或分区的名称。每个分区都是按给定的大小创建的,没有分区可能重叠。磁盘上未使用的任何空间都被归为空闲空间。Linux支持许多不同的分区格式,但是Linux PC上最常见的类型是MS-DOS格式。这种格式最多支持四个主分区。其中一个主分区可以替换为一个扩展分区,扩展分区最多可以包含12个逻辑分区。这给了我们15个可用的分区,因为我们没有计算扩展分区,因为它不能用于普通数据。在接下来的示例中,我们将使用上述格式,但是,对于更大的驱动器确实存在一种格式,这种格式允许使用更多的分区。这种格式称为GPTGUID分区表。这种格式默认最多支持128个分区。

日期:2019-04-29 03:17:31 来源:oir作者:oir