一、fdisk分区

1.使用fdisk -l查看新挂载的硬盘是否被识别

[root@Node2 test]# fdisk -l

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: 0x0008f184

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048     1026047      512000   83  Linux

/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors  

 #在vmware中添加一块1G的虚拟硬盘

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes


2.新挂载硬盘分区方法

2.1 fdisk /dev/sdb

[root@Node2 ~]# fdisk /dev/sdb    #使用fdisk命令对/dev/sdb进行分区
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x46480706.

Command (m for help): m    #使用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)
Command (m for help): p   #打印输出分区表

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x46480706

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n    #添加新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p    #添加主分区,默认的分区格式是主分区
Partition number (1-4, default 1): 1   #分区号为1
First sector (2048-2097151, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +200M  

#可以采用+sectors或者+size的方式定义分区大小,这里使用+200M这种方式定义分区大小,方便快捷

#定义分区的大小为200M

Partition 1 of type Linux and of size 200 MiB is set  

Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x46480706

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      411647      204800   83  Linux

#分配第一个主分区成功
Command (m for help): n

Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2

First sector (411648-2097151, default 411648): 
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-2097151, default 2097151): +200M
Partition 2 of type Linux and of size 200 MiB is set

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x46480706

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      411647      204800   83  Linux
/dev/sdb2          411648      821247      204800   83  Linux


Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): e  #选择的分区格式是扩展分区
Partition number (3,4, default 3): 4

First sector (821248-2097151, default 821248): 
Using default value 821248
Last sector, +sectors or +size{K,M,G} (821248-2097151, default 2097151): 

#未输入分区大小表示将剩下的所有空间分配给该扩展分区
Using default value 2097151

Partition 4 of type Extended and of size 623 MiB is set

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x46480706

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      411647      204800   83  Linux
/dev/sdb2          411648      821247      204800   83  Linux
/dev/sdb4          821248     2097151      637952    5  Extended


Command (m for help): n
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (823296-2097151, default 823296): 
Using default value 823296
Last sector, +sectors or +size{K,M,G} (823296-2097151, default 2097151): +300M
Partition 5 of type Linux and of size 300 MiB is set

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x46480706

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      411647      204800   83  Linux
/dev/sdb2          411648      821247      204800   83  Linux
/dev/sdb4          821248     2097151      637952    5  Extended
/dev/sdb5          823296     1437695      307200   83  Linux

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

[root@Node2 ~]# fdisk -l

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: 0x0008f184

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x522f80fa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      411647      204800   83  Linux
/dev/sdb2          411648      821247      204800   83  Linux
/dev/sdb4          821248     2050047      614400    5  Extended
/dev/sdb5          823296     2050047      613376   83  Linux



[root@Node2 ~]# mkfs.ext3 /dev/sdb1    #格式化某个分区
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)   #32位系统的块大小默认是1k
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@Node2 ~]#mkdir  /data   #创建新目录用于挂载新的分区

[root@Node2 ~]# mount /dev/sdb1 /data  #将/dev/sdb1挂载到/data目录下

wKiom1TUWzLwnaJLAAKsGlZNBNA903.jpg

[root@Node2 ~]# vim /etc/fstab 

因为使用mount挂载分区后,系统重启后分区挂载配置会失效,所以需要修改/etc/fstab配置文件使分区挂载永久生效



3.troubleshooting

学习速度最快的方法是在错误中学习。

3.1别人总是说在linux中一块硬盘最多只能有四个主分区,剩下的都是扩展分区。注意,扩展分区占用一个主分区位置,即为如分了三个主分区和一个扩展分区后无法再划分一个主分区。

3.2扩展分区、主分区、逻辑分区之间的关系:扩展分区大小=逻辑分区之和;硬盘大小=扩展分区+主分区;逻辑分区从扩展分区中划分。

3.3如果硬是要在有了四个主分区情况下任性地再划分一个主分区,会怎么样呢?试验为准。

Command (m for help): P


Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x522f80fa


   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048      411647      204800   83  Linux

/dev/sdb2          411648      821247      204800   83  Linux

/dev/sdb3          821248     1230847      204800   83  Linux

/dev/sdb4         1230848     1640447      204800   83  Linux

Command (m for help): n

If you want to create more than four partitions, you must replace a

primary partition with an extended partition first.

#提示已经很明显,如果想要创建更多的分区,首先得将一个主分区变成一个扩展分区。验证了linux中一块磁盘最多只能划分四个主分区,如果想划分更多分区,得至少有一个主分区为扩展分区,从扩展分区中划分更多分区。同时也验证了在linux中一块磁盘可以没有扩展分区。总结:主分区最少得有一个,扩展分区最多能有三个。

3.4遇到过一个问题。在进行分区操作中,如果因为意外因素没有保存(输入w)而直接退出了操作命令行,则分区操作不会被保存。

3.5能不能把全部磁盘都变成扩展分区呢?

  可以:

Command (m for help): P


Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x522f80fa


   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048      411647      204800    5  Extended

3.6不能从扩展分区中划分主分区,只能划分为逻辑分区

Command (m for help): n

Partition type:

   p   primary (0 primary, 1 extended, 3 free)

   l   logical (numbered from 5)

Select (default p): l

Adding logical partition 5

First sector (4096-411647, default 4096): 

Using default value 4096

Last sector, +sectors or +size{K,M,G} (4096-411647, default 411647): 700+

Value out of range.

#强制划分会提示 value out of range ,划分失败



4.练习

在虚拟机中添加一块1G的虚拟磁盘,使用fdisk进行分区,分成2个主分区,个扩展分区。

主分区大小分别是200M、200M;

扩展分区大小为600M;

对于扩展分区,分成两个逻辑驱动器,分别为300M。

思路:将物理硬盘挂载到服务器上----->查看新挂载的硬盘并对硬盘进行分区---->分区完成后对新的分区进行格式化--->将分区挂载到某个目录下----->修改/etc/fstab配置文件