UNPKG

9.76 kBMarkdownView Raw
1fdisk
2===
3
4查看磁盘使用情况和磁盘分区
5
6## 补充说明
7
8**fdisk命令** 用于观察硬盘实体使用情况,也可对硬盘分区。它采用传统的问答式界面,而非类似DOS fdisk的cfdisk互动式操作界面,因此在使用上较为不便,但功能却丝毫不打折扣。
9
10### 语法
11
12```shell
13fdisk(选项)(参数)
14```
15
16### 选项
17
18```shell
19 -b <大小> 扇区大小(512、1024、2048或4096)
20 -c[=<模式>] 兼容模式:“dos”或“nondos”(默认)
21 -h 打印此帮助文本
22 -u[=<单位>] 显示单位:“cylinders”(柱面)或“sectors”(扇区,默认)
23 -v 打印程序版本
24 -C <数字> 指定柱面数
25 -H <数字> 指定磁头数
26 -S <数字> 指定每个磁道的扇区数
27```
28
29### 参数
30
31设备文件:指定要进行分区或者显示分区的硬盘设备文件。
32
33### 实例
34
35首先选择要进行操作的磁盘:
36
37```shell
38[root@localhost ~]# fdisk /dev/sdb
39```
40
41输入`m`列出可以执行的命令:
42
43```shell
44command (m for help): m
45Command action
46 a toggle a bootable flag
47 b edit bsd disklabel
48 c toggle the dos compatibility flag
49 d delete a partition
50 l list known partition types
51 m print this menu
52 n add a new partition
53 o create a new empty DOS partition table
54 p print the partition table
55 q quit without saving changes
56 s create a new empty Sun disklabel
57 t change a partition's system id
58 u change display/entry units
59 v verify the partition table
60 w write table to disk and exit
61 x extra functionality (experts only)
62```
63
64输入`p`列出磁盘目前的分区情况:
65
66```shell
67Command (m for help): p
68
69Disk /dev/sdb: 3221 MB, 3221225472 bytes
70255 heads, 63 sectors/track, 391 cylinders
71Units = cylinders of 16065 * 512 = 8225280 bytes
72
73 Device Boot Start End Blocks Id System
74/dev/sdb1 1 1 8001 8e Linux LVM
75/dev/sdb2 2 26 200812+ 83 Linux
76```
77
78输入`d`然后选择分区,删除现有分区:
79
80```shell
81Command (m for help): d
82Partition number (1-4): 1
83
84Command (m for help): d
85Selected partition 2
86```
87
88查看分区情况,确认分区已经删除:
89
90```shell
91Command (m for help): print
92
93Disk /dev/sdb: 3221 MB, 3221225472 bytes
94255 heads, 63 sectors/track, 391 cylinders
95Units = cylinders of 16065 * 512 = 8225280 bytes
96
97 Device Boot Start End Blocks Id System
98
99Command (m for help):
100```
101
102输入`n`建立新的磁盘分区,首先建立两个主磁盘分区:
103
104```shell
105Command (m for help): n
106Command action
107 e extended
108 p primary partition (1-4)
109p //建立主分区
110Partition number (1-4): 1 //分区号
111First cylinder (1-391, default 1): //分区起始位置
112Using default value 1
113last cylinder or +size or +sizeM or +sizeK (1-391, default 391): 100 //分区结束位置,单位为扇区
114
115Command (m for help): n //再建立一个分区
116Command action
117 e extended
118 p primary partition (1-4)
119p
120Partition number (1-4): 2 //分区号为2
121First cylinder (101-391, default 101):
122Using default value 101
123Last cylinder or +size or +sizeM or +sizeK (101-391, default 391): +200M //分区结束位置,单位为M
124```
125
126确认分区建立成功:
127
128```shell
129Command (m for help): p
130
131Disk /dev/sdb: 3221 MB, 3221225472 bytes
132255 heads, 63 sectors/track, 391 cylinders
133Units = cylinders of 16065 * 512 = 8225280 bytes
134
135 Device Boot Start End Blocks Id System
136/dev/sdb1 1 100 803218+ 83 Linux
137/dev/sdb2 101 125 200812+ 83 Linux
138```
139
140再建立一个逻辑分区:
141
142```shell
143Command (m for help): n
144Command action
145 e extended
146 p primary partition (1-4)
147e //选择扩展分区
148Partition number (1-4): 3
149First cylinder (126-391, default 126):
150Using default value 126
151Last cylinder or +size or +sizeM or +sizeK (126-391, default 391):
152Using default value 391
153```
154
155确认扩展分区建立成功:
156
157```shell
158Command (m for help): p
159
160Disk /dev/sdb: 3221 MB, 3221225472 bytes
161255 heads, 63 sectors/track, 391 cylinders
162Units = cylinders of 16065 * 512 = 8225280 bytes
163
164 Device Boot Start End Blocks Id System
165/dev/sdb1 1 100 803218+ 83 Linux
166/dev/sdb2 101 125 200812+ 83 Linux
167/dev/sdb3 126 391 2136645 5 Extended
168```
169
170在扩展分区上建立两个逻辑分区:
171
172```shell
173Command (m for help): n
174Command action
175 l logical (5 or over)
176 p primary partition (1-4)
177l //选择逻辑分区
178First cylinder (126-391, default 126):
179Using default value 126
180Last cylinder or +size or +sizeM or +sizeK (126-391, default 391): +400M
181
182Command (m for help): n
183Command action
184 l logical (5 or over)
185 p primary partition (1-4)
186l
187First cylinder (176-391, default 176):
188Using default value 176
189Last cylinder or +size or +sizeM or +sizeK (176-391, default 391):
190Using default value 391
191```
192
193确认逻辑分区建立成功:
194
195```shell
196Command (m for help): p
197
198Disk /dev/sdb: 3221 MB, 3221225472 bytes
199255 heads, 63 sectors/track, 391 cylinders
200Units = cylinders of 16065 * 512 = 8225280 bytes
201
202 Device Boot Start End Blocks Id System
203/dev/sdb1 1 100 803218+ 83 Linux
204/dev/sdb2 101 125 200812+ 83 Linux
205/dev/sdb3 126 391 2136645 5 Extended
206/dev/sdb5 126 175 401593+ 83 Linux
207/dev/sdb6 176 391 1734988+ 83 Linux
208
209Command (m for help):
210```
211
212从上面的结果我们可以看到,在硬盘sdb我们建立了2个主分区(sdb1,sdb2),1个扩展分区(sdb3),2个逻辑分区(sdb5,sdb6)
213
214注意:主分区和扩展分区的磁盘号位1-4,也就是说最多有4个主分区或者扩展分区,逻辑分区开始的磁盘号为5,因此在这个实验中试没有sdb4的。
215
216最后对分区操作进行保存:
217
218```shell
219Command (m for help): w
220The partition table has been altered!
221
222Calling ioctl() to re-read partition table.
223Syncing disks.
224```
225
226建立好分区之后我们还需要对分区进行格式化才能在系统中使用磁盘。
227
228在sdb1上建立ext2分区:
229
230```shell
231[root@localhost ~]# mkfs.ext2 /dev/sdb1
232mke2fs 1.39 (29-May-2006)
233Filesystem label=
234OS type: Linux
235Block size=4096 (log=2)
236Fragment size=4096 (log=2)
237100576 inodes, 200804 blocks
23810040 blocks (5.00%) reserved for the super user
239First data block=0
240Maximum filesystem blocks=209715200
2417 block groups
24232768 blocks per group, 32768 fragments per group
24314368 inodes per group
244Superblock backups stored on blocks:
245 32768, 98304, 163840
246
247Writing inode tables: done
248Writing superblocks and filesystem accounting information: done
249
250This filesystem will be automatically checked every 32 mounts or
251180 days, whichever comes first. Use tune2fs -c or -i to override.
252```
253
254在sdb6上建立ext3分区:
255
256```shell
257[root@localhost ~]# mkfs.ext3 /dev/sdb6
258mke2fs 1.39 (29-May-2006)
259Filesystem label=
260OS type: Linux
261Block size=4096 (log=2)
262Fragment size=4096 (log=2)
263217280 inodes, 433747 blocks
26421687 blocks (5.00%) reserved for the super user
265First data block=0
266Maximum filesystem blocks=444596224
26714 block groups
26832768 blocks per group, 32768 fragments per group
26915520 inodes per group
270Superblock backups stored on blocks:
271 32768, 98304, 163840, 229376, 294912
272
273Writing inode tables: done
274Creating journal (8192 blocks): done
275Writing superblocks and filesystem accounting information: done
276
277This filesystem will be automatically checked every 32 mounts or
278180 days, whichever comes first. Use tune2fs -c or -i to override.
279[root@localhost ~]#
280```
281
282建立两个目录`/oracle``/web`,将新建好的两个分区挂载到系统:
283
284```shell
285[root@localhost ~]# mkdir /oracle
286[root@localhost ~]# mkdir /web
287[root@localhost ~]# mount /dev/sdb1 /oracle
288[root@localhost ~]# mount /dev/sdb6 /web
289```
290
291查看分区挂载情况:
292
293```shell
294[root@localhost ~]# df -h
295文件系统 容量 已用 可用 已用% 挂载点
296/dev/mapper/VolGroup00-LogVol00
297 6.7G 2.8G 3.6G 44% /
298/dev/sda1 99M 12M 82M 13% /boot
299tmpfs 125M 0 125M 0% /dev/shm
300/dev/sdb1 773M 808K 733M 1% /oracle
301/dev/sdb6 1.7G 35M 1.6G 3% /web
302```
303
304如果需要每次开机自动挂载则需要修改`/etc/fstab`文件,加入两行配置:
305
306```shell
307[root@localhost ~]# vim /etc/fstab
308
309/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
310LABEL=/boot /boot ext3 defaults 1 2
311tmpfs /dev/shm tmpfs defaults 0 0
312devpts /dev/pts devpts gid=5,mode=620 0 0
313sysfs /sys sysfs defaults 0 0
314proc /proc proc defaults 0 0
315/dev/VolGroup00/LogVol01 swap swap defaults 0 0
316/dev/sdb1 /oracle ext2 defaults 0 0
317/dev/sdb6 /web ext3 defaults 0 0
318```
319
320
321<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ -->