1 | fdisk
|
2 | ===
|
3 |
|
4 | 查看磁盘使用情况和磁盘分区
|
5 |
|
6 | ## 补充说明
|
7 |
|
8 | **fdisk命令** 用于观察硬盘实体使用情况,也可对硬盘分区。它采用传统的问答式界面,而非类似DOS fdisk的cfdisk互动式操作界面,因此在使用上较为不便,但功能却丝毫不打折扣。
|
9 |
|
10 | ### 语法
|
11 |
|
12 | ```shell
|
13 | fdisk(选项)(参数)
|
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
|
44 | command (m for help): m
|
45 | Command 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
|
67 | Command (m for help): p
|
68 |
|
69 | Disk /dev/sdb: 3221 MB, 3221225472 bytes
|
70 | 255 heads, 63 sectors/track, 391 cylinders
|
71 | Units = 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
|
81 | Command (m for help): d
|
82 | Partition number (1-4): 1
|
83 |
|
84 | Command (m for help): d
|
85 | Selected partition 2
|
86 | ```
|
87 |
|
88 | 查看分区情况,确认分区已经删除:
|
89 |
|
90 | ```shell
|
91 | Command (m for help): print
|
92 |
|
93 | Disk /dev/sdb: 3221 MB, 3221225472 bytes
|
94 | 255 heads, 63 sectors/track, 391 cylinders
|
95 | Units = cylinders of 16065 * 512 = 8225280 bytes
|
96 |
|
97 | Device Boot Start End Blocks Id System
|
98 |
|
99 | Command (m for help):
|
100 | ```
|
101 |
|
102 | 输入`n`建立新的磁盘分区,首先建立两个主磁盘分区:
|
103 |
|
104 | ```shell
|
105 | Command (m for help): n
|
106 | Command action
|
107 | e extended
|
108 | p primary partition (1-4)
|
109 | p //建立主分区
|
110 | Partition number (1-4): 1 //分区号
|
111 | First cylinder (1-391, default 1): //分区起始位置
|
112 | Using default value 1
|
113 | last cylinder or +size or +sizeM or +sizeK (1-391, default 391): 100 //分区结束位置,单位为扇区
|
114 |
|
115 | Command (m for help): n //再建立一个分区
|
116 | Command action
|
117 | e extended
|
118 | p primary partition (1-4)
|
119 | p
|
120 | Partition number (1-4): 2 //分区号为2
|
121 | First cylinder (101-391, default 101):
|
122 | Using default value 101
|
123 | Last cylinder or +size or +sizeM or +sizeK (101-391, default 391): +200M //分区结束位置,单位为M
|
124 | ```
|
125 |
|
126 | 确认分区建立成功:
|
127 |
|
128 | ```shell
|
129 | Command (m for help): p
|
130 |
|
131 | Disk /dev/sdb: 3221 MB, 3221225472 bytes
|
132 | 255 heads, 63 sectors/track, 391 cylinders
|
133 | Units = 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
|
143 | Command (m for help): n
|
144 | Command action
|
145 | e extended
|
146 | p primary partition (1-4)
|
147 | e //选择扩展分区
|
148 | Partition number (1-4): 3
|
149 | First cylinder (126-391, default 126):
|
150 | Using default value 126
|
151 | Last cylinder or +size or +sizeM or +sizeK (126-391, default 391):
|
152 | Using default value 391
|
153 | ```
|
154 |
|
155 | 确认扩展分区建立成功:
|
156 |
|
157 | ```shell
|
158 | Command (m for help): p
|
159 |
|
160 | Disk /dev/sdb: 3221 MB, 3221225472 bytes
|
161 | 255 heads, 63 sectors/track, 391 cylinders
|
162 | Units = 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
|
173 | Command (m for help): n
|
174 | Command action
|
175 | l logical (5 or over)
|
176 | p primary partition (1-4)
|
177 | l //选择逻辑分区
|
178 | First cylinder (126-391, default 126):
|
179 | Using default value 126
|
180 | Last cylinder or +size or +sizeM or +sizeK (126-391, default 391): +400M
|
181 |
|
182 | Command (m for help): n
|
183 | Command action
|
184 | l logical (5 or over)
|
185 | p primary partition (1-4)
|
186 | l
|
187 | First cylinder (176-391, default 176):
|
188 | Using default value 176
|
189 | Last cylinder or +size or +sizeM or +sizeK (176-391, default 391):
|
190 | Using default value 391
|
191 | ```
|
192 |
|
193 | 确认逻辑分区建立成功:
|
194 |
|
195 | ```shell
|
196 | Command (m for help): p
|
197 |
|
198 | Disk /dev/sdb: 3221 MB, 3221225472 bytes
|
199 | 255 heads, 63 sectors/track, 391 cylinders
|
200 | Units = 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 |
|
209 | Command (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
|
219 | Command (m for help): w
|
220 | The partition table has been altered!
|
221 |
|
222 | Calling ioctl() to re-read partition table.
|
223 | Syncing disks.
|
224 | ```
|
225 |
|
226 | 建立好分区之后我们还需要对分区进行格式化才能在系统中使用磁盘。
|
227 |
|
228 | 在sdb1上建立ext2分区:
|
229 |
|
230 | ```shell
|
231 | [root@localhost ~]# mkfs.ext2 /dev/sdb1
|
232 | mke2fs 1.39 (29-May-2006)
|
233 | Filesystem label=
|
234 | OS type: Linux
|
235 | Block size=4096 (log=2)
|
236 | Fragment size=4096 (log=2)
|
237 | 100576 inodes, 200804 blocks
|
238 | 10040 blocks (5.00%) reserved for the super user
|
239 | First data block=0
|
240 | Maximum filesystem blocks=209715200
|
241 | 7 block groups
|
242 | 32768 blocks per group, 32768 fragments per group
|
243 | 14368 inodes per group
|
244 | Superblock backups stored on blocks:
|
245 | 32768, 98304, 163840
|
246 |
|
247 | Writing inode tables: done
|
248 | Writing superblocks and filesystem accounting information: done
|
249 |
|
250 | This filesystem will be automatically checked every 32 mounts or
|
251 | 180 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
|
258 | mke2fs 1.39 (29-May-2006)
|
259 | Filesystem label=
|
260 | OS type: Linux
|
261 | Block size=4096 (log=2)
|
262 | Fragment size=4096 (log=2)
|
263 | 217280 inodes, 433747 blocks
|
264 | 21687 blocks (5.00%) reserved for the super user
|
265 | First data block=0
|
266 | Maximum filesystem blocks=444596224
|
267 | 14 block groups
|
268 | 32768 blocks per group, 32768 fragments per group
|
269 | 15520 inodes per group
|
270 | Superblock backups stored on blocks:
|
271 | 32768, 98304, 163840, 229376, 294912
|
272 |
|
273 | Writing inode tables: done
|
274 | Creating journal (8192 blocks): done
|
275 | Writing superblocks and filesystem accounting information: done
|
276 |
|
277 | This filesystem will be automatically checked every 32 mounts or
|
278 | 180 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
|
299 | tmpfs 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
|
310 | LABEL=/boot /boot ext3 defaults 1 2
|
311 | tmpfs /dev/shm tmpfs defaults 0 0
|
312 | devpts /dev/pts devpts gid=5,mode=620 0 0
|
313 | sysfs /sys sysfs defaults 0 0
|
314 | proc /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 |
|