fdisk分區實戰

一、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配置文件


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章