1.说明:fdisk如何来学习
fdisk,就是在linux中用来分区的一个命令了,至于详细的参数,可以man一下,这里给出实际应用过程中最常用的用法,因为这样比起单纯讲命令的语法功能更能掌握一个命令的实际用法。
下面的过程是在我自己的实验环境中搭建,先把简要的过程看一下:
(1)添加一块1GB的磁盘
(2)fdisk详细分区过程
(3)通知Linux内核分区信息的更改
(4)格式化分区
(5)挂载分区
OK!我相信这样一个过程下来,你是真正可以做到添加一块硬盘,并自己做分区格式化以及使用,这样的话,意义就真的比单纯讲fdisk的语法参数要好得多了!
2.实战演示:从添加硬盘到真正可以使用硬盘
(1)添加一块1GB的磁盘
我是在虚拟机环境下直接添加的,方法很简单,这里就不多说了,下面给出在linux下查看该硬盘的信息:
1
2
3
4
5
6
7
8
|
[root@xpleaf ~]# fdisk -l /dev/sdb
Disk /dev/sdb:
1073
MB, 1073741824bytes
255
heads,
63
sectors/track, 130cylinders
Units = cylinders of
16065
*
512
=
8225280
bytes
Sector size (logical/physical): 512bytes /
512
bytes
I/O size (minimum/optimal):
512
bytes/
512
bytes
Disk identifier:
0x00000000
|
(2)fdisk详细分区过程
下面直接给出详细过程,重要的一些说明在该过程中已经给出,注意看了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
[root@xpleaf ~]# fdisk /dev/sdb ===>直接接磁盘名就可以分区
Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabel
Building a
new
DOS disklabel
with
diskidentifier
0x7f3aa9cb
.
Changes will remain
in
memory only,until you decide to write them.
After that, of course, the previouscontent won't be recoverable.
Warning: invalid flag
0x0000
ofpartition table
4
will be corrected by w(rite)
WARNING: DOS-compatible mode isdeprecated. It's strongly recommended to
switch
off the mode (command
'c'
) andchange display units to
sectors (command
'u'
).
Command (m
for
help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibilityflag
d
delete
a partition ===>删除一个分区
l list known partition types ===>查看分区类型对应编号列表
m print
this
menu ===>打印此帮助菜单
n add a
new
partition ===>新建一个分区
o create a
new
empty DOSpartition table
p print the partition table ===>打印分区表
q quit without saving changes ===>退出但不保存更改
s create a
new
empty Sundisklabel
t change a partition's system id ===>更改分区类型(做LVM时需要)
u change display/entry units
v verify the partition table
w write table to disk and exit ===>将操作写入分区表并退出程序
x extra functionality (expertsonly)
Command (m
for
help): n
Command action
e extended
p primary partition (
1
-
4
)
p ===>新建一个主分区
Partition number (
1
-
4
):
1
===>指定主分区的分区号码
First cylinder (
1
-
130
,
default
1
): ===>指定分区开始的柱面号
Usingdefault value
1
Last cylinder, +cylinders or+size{K,M,G} (
1
-
130
,
default
130
): +100M
===>指定分区结束的柱面号,或者指定分区大小,让fdisk自动判断结束柱面号
Command (m
for
help): p
Disk /dev/sdb:
1073
MB, 1073741824bytes
255
heads,
63
sectors/track, 130cylinders
Units = cylinders of
16065
*
512
=
8225280
bytes
Sector size (logical/physical): 512bytes /
512
bytes
I/O size (minimum/optimal):
512
bytes/
512
bytes
Disk identifier:
0x7f3aa9cb
Device Boot Start End Blocks Id System
/dev/sdb1
1
14
112423
+
83
Linux
Command (m
for
help): n
Command action
e extended
p primary partition (
1
-
4
)
e ===>新建一个扩展分区
Partition number (
1
-
4
):
1
Partition
1
is
already defined. Delete it before re-adding it.
===>由于前面已经使用了分区号码
1
,所以这里会提示错误
Command (m
for
help): n
Command action
e extended
p primary partition (
1
-
4
)
e
Partition number (
1
-
4
):
2
First cylinder (
15
-
130
,
default
15
):
Usingdefault value
15
Last cylinder, +cylinders or+size{K,M,G} (
15
-
130
,
default
130
):
Usingdefault value
130
===>分区的开始柱面号和结束柱面号都使用默认值,即把剩余磁盘空间分配给该分区
Command (m
for
help): p
Disk /dev/sdb:
1073
MB, 1073741824bytes
255
heads,
63
sectors/track, 130cylinders
Units = cylinders of
16065
*
512
=
8225280
bytes
Sector size (logical/physical): 512bytes /
512
bytes
I/O size (minimum/optimal):
512
bytes/
512
bytes
Disk identifier:
0x7f3aa9cb
Device Boot Start End Blocks Id System
/dev/sdb1
1
14
112423
+
83
Linux
/dev/sdb2
15
130
931770
5
Extended
Command (m
for
help): n
Command action
l logical (
5
or over) ===>由于扩展分区只能有一个,上面新建扩展分区后
p primary partition (
1
-
4
) 这里就显示逻辑分区了,也可以看到fdisk命令,
p 要想新建逻辑分区,需要先新建扩展分区
Partition number (
1
-
4
):
3
No free sectors available
===>这里还想再新建一个主分区,但是提示已经没有磁盘空间剩余
Command (m
for
help): n
Command action
l logical (
5
or over)
p primary partition (
1
-
4
)
l ===>新建一个逻辑分区
First cylinder (
15
-
130
,
default
15
):
Usingdefault value
15
Last cylinder, +cylinders or+size{K,M,G} (
15
-
130
,
default
130
): +400M
Command (m
for
help): p
Disk /dev/sdb:
1073
MB, 1073741824bytes
255
heads,
63
sectors/track, 130cylinders
Units = cylinders of
16065
*
512
=
8225280
bytes
Sector size (logical/physical): 512bytes /
512
bytes
I/O size (minimum/optimal):
512
bytes/
512
bytes
Disk identifier:
0x7f3aa9cb
Device Boot Start End Blocks Id System
/dev/sdb1
1
14
112423
+
83
Linux
/dev/sdb2
15
130
931770
5
Extended
/dev/sdb5
15
66
417658
+
83
Linux
Command (m
for
help): n
Command action
l logical (
5
or over)
p primary partition (
1
-
4
)
l
First cylinder (
67
-
130
,
default
67
):
Usingdefault value
67
Last cylinder, +cylinders or+size{K,M,G} (
67
-
130
,
default
130
):
Usingdefault value
130
Command (m
for
help): p
Disk /dev/sdb:
1073
MB, 1073741824bytes
255
heads,
63
sectors/track, 130cylinders
Units = cylinders of
16065
*
512
=
8225280
bytes
Sector size (logical/physical): 512bytes /
512
bytes
I/O size (minimum/optimal):
512
bytes/
512
bytes
Disk identifier:
0x7f3aa9cb
Device Boot Start End Blocks Id System
/dev/sdb1
1
14
112423
+
83
Linux
/dev/sdb2
15
130
931770
5
Extended
/dev/sdb5
15
66
417658
+
83
Linux
/dev/sdb6
67
130
514048
+
83
Linux
Command (m
for
help): w ===>保存更改
The partition table has been altered!
Calling ioctl() to re-read partitiontable.
Syncing disks.
|
OK,可以看到,fdisk的分区方法是非常简单的,但是前提就是需要你得对硬盘的知识有一定的了解,比如:硬盘的内部结构、磁头、柱面、扇区等,这些知识都需要非常熟悉才好,不然的话,使用fdisk来分区的话也只是--“雾里看花,水中望月”,掌握原理才是最重要的,而使用命令工具只是为了实现我们的需求,同时也可以加深我们对原理的理解。
(3)通知Linux内核分区信息的更改
1
|
[root@xpleaf ~]# partprobe /dev/sdb
|
分区完成后,需要此步骤,否则去使用这些分区时会出现错误信息。当然,如果不这样做的话,也可以采取重新启动系统的方法。
(4)格式化分区
格式化分区是为了创建文件系统,分配Inode和Block
1
2
3
4
5
6
|
[root@xpleaf ~]# mkfs.ext4 /dev/sdb
sdb sdb1 sdb2 sdb5 sdb6
[root@xpleaf ~]# mkfs.ext4 /dev/sdb1
……
This filesystem will be automaticallychecked every
31
mounts or
180
days, whichever comes first. Use tune2fs -c or -i to
override
.
|
最后面提示说系统会定时检查文件系统,这里不想系统检查,则可以使用下面的命令:
1
2
3
|
[root@xpleaf ~]# tune2fs -c -
1
/dev/sdb1
tune2fs
1.41
.
12
(
17
-May-
2010
)
Setting maximal mount count to -
1
|
(5)挂载分区
1
2
3
4
5
6
7
|
[root@xpleaf ~]# mount /dev/sdb1 /mnt
[root@xpleaf ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3
9
.2G
1
.8G
7
.0G
21
% /
tmpfs 937M
0
937M
0
% /dev/shm
/dev/sda1 194M 34M 151M
19
% /boot
/dev/sdb1 107M
5
.6M 96M
6
% /mnt
|
这里的挂载是临时挂载的,如果需要永久挂载(即开机挂载),则需要修改/etc/fstab文件,这里就不提及了。
OK,通过上面的实战步骤,实际上已经可以掌握fdisk的用法了,这样的话以后需要添加硬盘、分区格式化的话,就不会不知道怎么做了吧?
3.拓展:fdisk如何非交互分区???
从上面的fdisk分区过程可以看到,fidisk使用的是交互式的过程,如果学过parted命令,应该知道parted是可以进行非交互式的分区的,那么这里的问题是,fdisk可以非交互式分区吗?
答案是肯定可以的,请参考下面:
(1)将你要在fdisk命令行输入的命令实现写入一个文本文件,比如叫做fdisk.txt
(2)然后fdisk.txt的内容类似于这样:
1
2
3
4
5
6
|
n
p
1
0
100M
w
|
实际就是我们在交互式时的分区过程(当然这里与我上面的分区并非一样)
(3)然后用类似如下命令实现自动分区:
1
|
fdisk /dev/hda < fdiskcmd.txt
|
OK!内容就这么多了,看是没有用,重在实践,希望可以帮到大家!
本文转自 xpleaf 51CTO博客,原文链接:http://blog.51cto.com/xpleaf/1683385,如需转载请自行联系原作者