fdisk

查看磁盘使用情况和磁盘分区

补充说明

fdisk命令 用于观察硬盘实体使用情况,也可对硬盘分区。它采用传统的问答式界面,而非类似DOS fdisk的cfdisk互动式操作界面,因此在使用上较为不便,但功能却丝毫不打折扣。

语法

1fdisk [选项] <磁盘>           更改分区表
2fdisk [选项] -l [<磁盘>...]   列出分区表

选项

 1
 2选项:
 3 -b, --sectors-size <大小>     显示扇区计数和大小
 4 -B, --protect-boot            创建新标签时不要擦除 bootbits
 5 -c, --compatibility[=<模式>]  模式,为“dos”或“nondos”(默认)
 6 -L, --color[=<时机>]          彩色输出(auto, always 或 never)默认启用颜色
 7 -l, --list                    显示分区并退出
 8 -x, --list-details            类似 --list 但提供更多细节
 9 -n, --noauto-pt               不要在空设备上创建默认分区表
10 -o, --output <列表>           输出列
11 -t, --type <类型>             只识别指定的分区表类型
12 -u, --units[=<单位>]          显示单位,“cylinders”柱面或“sectors”扇区(默认)
13 -s, --getsz                   以 512-字节扇区显示设备大小[已废弃]
14      -b, --bytes                   以字节为单位而非易读的格式来打印 SIZE
15      --lock[=<模式>]           使用独占设备锁(yes、no 或 nonblock)
16 -w, --wipe <模式>             擦除签名(auto, always 或 never)
17 -W, --wipe-partitions <模式>  擦除新分区的签名(auto, always 或 never)
18
19 -C, --cylinders <数字>        指定柱面数
20 -H, --heads <数字>            指定磁头数
21 -S, --sectors <数字>          指定每条磁道的扇区数
22
23 -h, --help                    显示此帮助
24 -V, --version                 显示版本

参数

设备文件:指定要进行分区或者显示分区的硬盘设备文件。

实例

首先选择要进行操作的磁盘:

1[root@localhost ~]# fdisk /dev/sdb

输入m列出可以执行的命令:

 1command (m for help): m
 2Command action
 3   a   toggle a bootable flag
 4   b   edit bsd disklabel
 5   c   toggle the dos compatibility flag
 6   d   delete a partition
 7   l   list known partition types
 8   m   print this menu
 9   n   add a new partition
10   o   create a new empty DOS partition table
11   p   print the partition table
12   q   quit without saving changes
13   s   create a new empty Sun disklabel
14   t   change a partition's system id
15   u   change display/entry units
16   v   verify the partition table
17   w   write table to disk and exit
18   x   extra functionality (experts only)

输入p列出磁盘目前的分区情况:

1Command (m for help): p
2
3Disk /dev/sdb: 3221 MB, 3221225472 bytes
4255 heads, 63 sectors/track, 391 cylinders
5Units = cylinders of 16065 * 512 = 8225280 bytes
6
7   Device Boot      Start         End      Blocks   Id  System
8/dev/sdb1               1           1        8001   8e  Linux LVM
9/dev/sdb2               2          26      200812+  83  Linux

输入d然后选择分区,删除现有分区:

1Command (m for help): d
2Partition number (1-4): 1
3
4Command (m for help): d
5Selected partition 2

查看分区情况,确认分区已经删除:

1Command (m for help): print
2
3Disk /dev/sdb: 3221 MB, 3221225472 bytes
4255 heads, 63 sectors/track, 391 cylinders
5Units = cylinders of 16065 * 512 = 8225280 bytes
6
7   Device Boot      Start         End      Blocks   Id  System
8
9Command (m for help):

输入n建立新的磁盘分区,首先建立两个主磁盘分区:

 1Command (m for help): n
 2Command action
 3   e   extended
 4   p   primary partition (1-4)
 5p    //建立主分区
 6Partition number (1-4): 1  //分区号
 7First cylinder (1-391, default 1):  //分区起始位置
 8Using default value 1
 9last cylinder or +size or +sizeM or +sizeK (1-391, default 391): 100  //分区结束位置,单位为扇区
10
11Command (m for help): n  //再建立一个分区
12Command action
13   e   extended
14   p   primary partition (1-4)
15p 
16Partition number (1-4): 2  //分区号为2
17First cylinder (101-391, default 101):
18Using default value 101
19Last cylinder or +size or +sizeM or +sizeK (101-391, default 391): +200M  //分区结束位置,单位为M

确认分区建立成功:

1Command (m for help): p
2
3Disk /dev/sdb: 3221 MB, 3221225472 bytes
4255 heads, 63 sectors/track, 391 cylinders
5Units = cylinders of 16065 * 512 = 8225280 bytes
6
7   Device Boot      Start         End      Blocks   Id  System
8/dev/sdb1               1         100      803218+  83  Linux
9/dev/sdb2             101         125      200812+  83  Linux

再建立一个逻辑分区:

 1Command (m for help): n
 2Command action
 3   e   extended
 4   p   primary partition (1-4)
 5e  //选择扩展分区
 6Partition number (1-4): 3
 7First cylinder (126-391, default 126):
 8Using default value 126
 9Last cylinder or +size or +sizeM or +sizeK (126-391, default 391):
10Using default value 391

确认扩展分区建立成功:

 1Command (m for help): p
 2
 3Disk /dev/sdb: 3221 MB, 3221225472 bytes
 4255 heads, 63 sectors/track, 391 cylinders
 5Units = cylinders of 16065 * 512 = 8225280 bytes
 6
 7   Device Boot      Start         End      Blocks   Id  System
 8/dev/sdb1               1         100      803218+  83  Linux
 9/dev/sdb2             101         125      200812+  83  Linux
10/dev/sdb3             126         391     2136645    5  Extended

在扩展分区上建立两个逻辑分区:

 1Command (m for help): n
 2Command action
 3   l   logical (5 or over)
 4   p   primary partition (1-4)
 5l //选择逻辑分区
 6First cylinder (126-391, default 126):
 7Using default value 126
 8Last cylinder or +size or +sizeM or +sizeK (126-391, default 391): +400M    
 9
10Command (m for help): n
11Command action
12   l   logical (5 or over)
13   p   primary partition (1-4)
14l
15First cylinder (176-391, default 176):
16Using default value 176
17Last cylinder or +size or +sizeM or +sizeK (176-391, default 391):
18Using default value 391

确认逻辑分区建立成功:

 1Command (m for help): p
 2
 3Disk /dev/sdb: 3221 MB, 3221225472 bytes
 4255 heads, 63 sectors/track, 391 cylinders
 5Units = cylinders of 16065 * 512 = 8225280 bytes
 6
 7   Device Boot      Start         End      Blocks   Id  System
 8/dev/sdb1               1         100      803218+  83  Linux
 9/dev/sdb2             101         125      200812+  83  Linux
10/dev/sdb3             126         391     2136645    5  Extended
11/dev/sdb5             126         175      401593+  83  Linux
12/dev/sdb6             176         391     1734988+  83  Linux
13
14Command (m for help):

从上面的结果我们可以看到,在硬盘sdb我们建立了2个主分区(sdb1,sdb2),1个扩展分区(sdb3),2个逻辑分区(sdb5,sdb6)

注意:主分区和扩展分区的磁盘号位1-4,也就是说最多有4个主分区或者扩展分区,逻辑分区开始的磁盘号为5,因此在这个实验中试没有sdb4的。

最后对分区操作进行保存:

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

建立好分区之后我们还需要对分区进行格式化才能在系统中使用磁盘。

在sdb1上建立ext2分区:

 1[root@localhost ~]# mkfs.ext2 /dev/sdb1
 2mke2fs 1.39 (29-May-2006)
 3Filesystem label=
 4OS type: Linux
 5Block size=4096 (log=2)
 6Fragment size=4096 (log=2)
 7100576 inodes, 200804 blocks
 810040 blocks (5.00%) reserved for the super user
 9First data block=0
10Maximum filesystem blocks=209715200
117 block groups
1232768 blocks per group, 32768 fragments per group
1314368 inodes per group
14Superblock backups stored on blocks:
15        32768, 98304, 163840
16
17Writing inode tables: done                           
18Writing superblocks and filesystem accounting information: done
19
20This filesystem will be automatically checked every 32 mounts or
21180 days, whichever comes first.  Use tune2fs -c or -i to override.

在sdb6上建立ext3分区:

 1[root@localhost ~]# mkfs.ext3 /dev/sdb6
 2mke2fs 1.39 (29-May-2006)
 3Filesystem label=
 4OS type: Linux
 5Block size=4096 (log=2)
 6Fragment size=4096 (log=2)
 7217280 inodes, 433747 blocks
 821687 blocks (5.00%) reserved for the super user
 9First data block=0
10Maximum filesystem blocks=444596224
1114 block groups
1232768 blocks per group, 32768 fragments per group
1315520 inodes per group
14Superblock backups stored on blocks:
15        32768, 98304, 163840, 229376, 294912
16
17Writing inode tables: done                           
18Creating journal (8192 blocks): done
19Writing superblocks and filesystem accounting information: done
20
21This filesystem will be automatically checked every 32 mounts or
22180 days, whichever comes first.  Use tune2fs -c or -i to override.
23[root@localhost ~]#

建立两个目录/oracle/web,将新建好的两个分区挂载到系统:

1[root@localhost ~]# mkdir /oracle
2[root@localhost ~]# mkdir /web
3[root@localhost ~]# mount /dev/sdb1 /oracle
4[root@localhost ~]# mount /dev/sdb6 /web

查看分区挂载情况:

1[root@localhost ~]# df -h
2文件系统              容量  已用 可用 已用% 挂载点
3/dev/mapper/VolGroup00-LogVol00
4                      6.7G  2.8G  3.6G  44% /
5/dev/sda1              99M   12M   82M  13% /boot
6tmpfs                 125M     0  125M   0% /dev/shm
7/dev/sdb1             773M  808K  733M   1% /oracle
8/dev/sdb6             1.7G   35M  1.6G   3% /web

如果需要每次开机自动挂载则需要修改/etc/fstab文件,加入两行配置:

 1[root@localhost ~]# vim /etc/fstab
 2
 3/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
 4LABEL=/boot             /boot                   ext3    defaults        1 2
 5tmpfs                   /dev/shm                tmpfs   defaults        0 0
 6devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
 7sysfs                   /sys                    sysfs   defaults        0 0
 8proc                    /proc                   proc    defaults        0 0
 9/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0
10/dev/sdb1               /oracle                 ext2    defaults        0 0
11/dev/sdb6               /web                    ext3    defaults        0 0

来源:https://github.com/jaywcjlove/linux-command

最后修改于: Wednesday, January 31, 2024
欢迎关注微信公众号,留言交流。

相关文章:

翻译: