首页 > 代码库 > Linux磁盘分区和格式化

Linux磁盘分区和格式化

1.磁盘分区

在Linux系统中非常多的磁盘分区工具,比如命令方式的fdisk、parted、cfdisk和sfdisk,最常使用的是fdisk和parted。

1.1 使用fdisk命令磁盘分区实例

fdisk命令语法

fdisk 【选项】【设备】

技术分享

 

fdisk交互式操作子命令

技术分享

 

 (1)进入fdisk,列出所有子命令

 

技术分享
 1 [root@test ~]# fdisk /dev/sda
 2 
 3 WARNING: DOS-compatible mode is deprecated. Its strongly recommended to
 4          switch off the mode (command c) and change display units to
 5          sectors (command u).
 6 
 7 Command (m for help): m
 8 Command action
 9    a   toggle a bootable flag
10    b   edit bsd disklabel
11    c   toggle the dos compatibility flag
12    d   delete a partition
13    l   list known partition types
14    m   print this menu
15    n   add a new partition
16    o   create a new empty DOS partition table
17    p   print the partition table
18    q   quit without saving changes
19    s   create a new empty Sun disklabel
20    t   change a partitions system id
21    u   change display/entry units
22    v   verify the partition table
23    w   write table to disk and exit
24    x   extra functionality (experts only)
View Code

 

(2)显示硬盘分区信息

 

技术分享
 1 Command (m for help): p
 2 
 3 Disk /dev/sda: 21.5 GB, 21474836480 bytes
 4 255 heads, 63 sectors/track, 2610 cylinders
 5 Units = cylinders of 16065 * 512 = 8225280 bytes
 6 Sector size (logical/physical): 512 bytes / 512 bytes
 7 I/O size (minimum/optimal): 512 bytes / 512 bytes
 8 Disk identifier: 0x0007b17a
 9 
10    Device Boot      Start         End      Blocks   Id  System
11 /dev/sda1   *           1          26      204800   83  Linux
12 Partition 1 does not end on cylinder boundary.
13 /dev/sda2              26        2611    20765696   8e  Linux LVM
View Code

磁盘分区信息描述

 

 

技术分享

(3)创建和删除主分区

 

技术分享
 1 Command (m for help): n
 2 Command action
 3    e   extended
 4    p   primary partition (1-4)
 5 p
 6 Partition number (1-4): 1
 7 First cylinder (1-2610, default 1): 
 8 Using default value 1
 9 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G
10 
11 Command (m for help): p
12 
13 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
14 255 heads, 63 sectors/track, 2610 cylinders
15 Units = cylinders of 16065 * 512 = 8225280 bytes
16 Sector size (logical/physical): 512 bytes / 512 bytes
17 I/O size (minimum/optimal): 512 bytes / 512 bytes
18 Disk identifier: 0x9422e586
19 
20    Device Boot      Start         End      Blocks   Id  System
21 /dev/sdb1               1         654     5253223+  83  Linux
View Code

(4)创建扩展分区和逻辑驱动器

技术分享
 1 Command (m for help): n
 2 Command action
 3    e   extended
 4    p   primary partition (1-4)
 5 e
 6 Partition number (1-4): 2
 7 First cylinder (655-2610, default 655): 
 8 Using default value 655
 9 Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): 
10 Using default value 2610
11 
12 Command (m for help): p
13 
14 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
15 255 heads, 63 sectors/track, 2610 cylinders
16 Units = cylinders of 16065 * 512 = 8225280 bytes
17 Sector size (logical/physical): 512 bytes / 512 bytes
18 I/O size (minimum/optimal): 512 bytes / 512 bytes
19 Disk identifier: 0x9422e586
20 
21    Device Boot      Start         End      Blocks   Id  System
22 /dev/sdb1               1         654     5253223+  83  Linux
23 /dev/sdb2             655        2610    15711570    5  Extended
24 
25 Command (m for help): n
26 Command action
27    l   logical (5 or over)
28    p   primary partition (1-4)
29 l
30 First cylinder (655-2610, default 655): 
31 Using default value 655
32 Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G
33 
34 Command (m for help): n
35 Command action
36    l   logical (5 or over)
37    p   primary partition (1-4)
38 l
39 First cylinder (1309-2610, default 1309): 
40 Using default value 1309
41 Last cylinder, +cylinders or +size{K,M,G} (1309-2610, default 2610): 
42 Using default value 2610
43 
44 Command (m for help): p
45 
46 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
47 255 heads, 63 sectors/track, 2610 cylinders
48 Units = cylinders of 16065 * 512 = 8225280 bytes
49 Sector size (logical/physical): 512 bytes / 512 bytes
50 I/O size (minimum/optimal): 512 bytes / 512 bytes
51 Disk identifier: 0x9422e586
52 
53    Device Boot      Start         End      Blocks   Id  System
54 /dev/sdb1               1         654     5253223+  83  Linux
55 /dev/sdb2             655        2610    15711570    5  Extended
56 /dev/sdb5             655        1308     5253223+  83  Linux
57 /dev/sdb6            1309        2610    10458283+  83  Linux
View Code

(5)查看并转换分区类型

技术分享

技术分享
 1 Command (m for help): t
 2 Partition number (1-6): 5
 3 Hex code (type L to list codes): 8e
 4 Changed system type of partition 5 to 8e (Linux LVM)
 5 
 6 Command (m for help): p
 7 
 8 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
 9 255 heads, 63 sectors/track, 2610 cylinders
10 Units = cylinders of 16065 * 512 = 8225280 bytes
11 Sector size (logical/physical): 512 bytes / 512 bytes
12 I/O size (minimum/optimal): 512 bytes / 512 bytes
13 Disk identifier: 0x9422e586
14 
15    Device Boot      Start         End      Blocks   Id  System
16 /dev/sdb1               1         654     5253223+  83  Linux
17 /dev/sdb2             655        2610    15711570    5  Extended
18 /dev/sdb5             655        1308     5253223+  8e  Linux LVM
19 /dev/sdb6            1309        2610    10458283+  83  Linux
View Code

(6)保存分区设置信息,并退出fdisk

技术分享
1 Command (m for help): w
2 The partition table has been altered!
3 
4 Calling ioctl() to re-read partition table.
5 Syncing disks.
View Code

(7)重启计算机系统并查看分区

 

技术分享
1 [root@test ~]# ls /dev/sdb*
2 /dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdb5  /dev/sdb6
View Code

 

Linux磁盘分区和格式化