UNPKG

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