Linux文件系统管理fdisk分区命令
概述
我们在安装操作系统的过程中已经对系统硬盘进行了分区,但是如果我新添加了一块硬盘,想要正常使用时,在Linux中有专门的分区命令 fdisk 和 parted。其中 fdisk 命令较为常用,但不支持大于 2TB 的分区;如果需要支持大于 2TB 的分区,则需要使用 parted 命令,当然 parted 命令也能分配较小的分区。我们先来看看如何使用 fdisk 命令进行分区。
fdisk命令 fdisk 命令的格式如下:
#列出系统分区 [root@localhost ~]# fdisk -l #给硬盘分区 [root@localhost ~]# fdisk 设备文件名
通过fdisk -l 命令可以得到以下信息
#查询本机可以识别的硬盘和分区 [root@localhost ~]# fdisk -l #硬盘文件名和硬盘大小 Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors #每个柱面的大小 Units = sectors of 1 * 512 = 512 bytes #每个扇区的大小 Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0007be9f Device Boot Start End Blocks ld System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 41943039 20765696 8e Linux LVM
使用"fdisk -l"査看分区信息,能够看到我们添加的硬盘(/dev/sda)的信息。信息的上半部分态是硬盘的整体状态,/dev/sda 硬盘的总大小是 21.5 GB
信息的下半部分是分区的信息,共 7 列,含义如下: Device Boot Start End Blocks id System 分区的设备文件名 是否为启动引导分区,在这里/dev/sda1为启动引导分区 起始柱面,代表分区从哪里开始 终止柱面,代表分区到哪里结束 分区的大小,单位是 KB 分区内文件系统的 ID。在fdisk 命令中,可以使用"i"查看 分区内安装的系统 如果这个分区并没有占满整块硬盘,就会提示 "Partition 1 does not end on cyl inder boundary",表示第一个分区没有到硬盘的结束柱面。 假如有一个新的硬盘 /dev/sdb,使用命令给硬盘 /dev/sdb分区,命令如下:
#给/dev/sdb分区 [root@localhost ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xed7e8bc7. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won\'t be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated.it\'s strongly recommended to switch off the mode (command \'c\') and change display units to sectors (command \'u\'). Command (m for help):m #交互界面的等待输入指令的位置,输入 m 得到帮助 Command action #可用指令 a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition I list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition\'s system id u change display/entry units v verity the partition table w write table to disk and exit x extra functionality (experts only)
注意这里的分区命令是"fdisk/dev/sdb",这是因为我们的硬盘现在并没有分区,使用 fdisk 命令的目的就是建立分区,所以"1~59"这些数字还不存在。
在 fdisk 交互界面中输入m可以得到帮助,帮助里列出了 fdisk 可以识别的交互命令,我们来解释一下这些命令,如下所示。
命令说 明 a 设置可引导标记 b 编辑 bsd 磁盘标签 c 设置 DOS 操作系统兼容标记 d 删除一个分区 1 显示已知的文件系统类型。82 为 Linux swap 分区,83 为 Linux 分区 m 显示帮助菜单 n 新建分区 0 建立空白 DOS 分区表 P 显示分区列表 q 不保存退出 s 新建空白 SUN 磁盘标签 t 改变一个分区的系统 ID u 改变显示记录单位 V 验证分区表 w 保存退出 X 附加功能(仅专家) fdisk命令创建主分区
建立主分区的过程是:"fdisk 硬盘名 -> n(新建)->p(建立主分区) -> 1(指定分区号) -> 回车(默认从 1 柱面开始建立分区)-> +5G(指定分区大小)"。此时我们的分区还没有格式化和挂载,所以还不能使用。
[root@localhost ~]# fdisk /dev/sdb …省略部分输出… Command (m for help): p #显示当前硬盘的分区列表 Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 *512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xb4b0720c Device Boot Start End Blocks id System #目前一个分区都没有 Command (m for help): n #那么我们新建一个分区 Command action #指定分区类型 e extended #扩展分区 p primary partition (1-4) #主分区 p #这里选择p,建立一个主分区 Partition number (1-4): 1 #选择分区号,范围为1~4,这里选择1 First cylinder (1 -2610, default 1): #分区的起始柱面,默认从1开始。因为要从硬盘头开始分区,所以直接回车 Using default value 1 #提示使用的是默认值1 Last cylinder, +cylinders or +size{K, M, G}(1-2610, default 2610): +5G #指定硬盘大小。可以按照柱面指定(1-2610)。我们对柱面不熟悉,那么可以使用size{K, M, G}的方式指定硬盘大小。这里指定+5G,建立一个5GB大小的分区 Command (m for help): #主分区就建立了,又回到了fdisk交互界面的提示符 Command (m for help): p #查询一下新建立的分区 Disk /dev/sdb: 21.5GB, 21474836480 bytes 255 heads,63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes 1512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xb4b0720c Device Boot Start End Blocks id System /dev/sdb1 1 654 5253223+ 83 Linux #dev/sdb1已经建立
fdisk命令创建扩展分区
把 /dev/sdb 硬盘的所有剩余空间都建立为扩展分区,也就是建立一个主分区,剩余空间都建立成扩展分区,再由扩展分区中建立逻辑分区。
扩展分区的建立命令如下:
[root@localhost ~]# fdisk /dev/sdb …省略部分输出… Command (m for help): n #新建立分区 Command action e extended p primary partition (1-4) e #这次建立扩展分区 Partition number (1-4): 2 #给扩展分区指定分区号2 First cylinder (655-2610, default 655): #扩展分区的起始柱面。上节建立的主分区1已经占用了1~654个柱面,所以我们从655开始建立,注意:如果没有特殊要求,则不要跳开柱面建立分区,应该紧挨着建立分区 Using default value 655 提示使用的是默认值655 Last cylinder, +cylinders or +size{K, M, G} (655-2610, default 2610): #这里把整块硬盘的剩余空间都建立为扩展分区 Using default value 2610 #提示使用的是默认值2610
fdisk命令创建逻辑分区
扩展分区是不能被格式化和直接使用的,所以还要在扩展分区内部再建立逻辑分区。
逻辑分区的建立过程,命令如下:
[root@localhost ~]# fdisk /dev/sdb …省略部分输出… Command (m for help): n #建立新分区 Command action l logical (5 or over) #由于在前面章节中,扩展分区已经建立,所以这里变成了l(logic) p primary partition (1-4) l #建立逻辑分区 First cylinder (655-2610, default 655): #不用指定分区号,默认会从5开始分配,所以直接选择起始柱面 #注意:逻辑分区是在扩展分区内部再划分的,所以柱面是和扩展分区重叠的 Using default value 655 Last cylinder, +cylinders or +size{K, M, G} (655-2610, default 2610):+2G #分配2GB大小 Command (m for help): n #再建立一个逻辑分区 Command action l logical (5 or over) p primary partition (1-4) l First cylinder (917-2610, default 917): Using default value 917 Last cylinder, +cylinders or +size{K, M, G} (917-2610, default 2610):+2G Command (m for help): p #查看一下已经建立的分区 Disk /dev/sdb: 21.5GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 *512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xb4b0720c Device Boot Start End Blocks id System /dev/sdb1 1 654 5253223+ 83 Linux #主分区 /dev/sdb2 655 2610 15711570 5 Extended #扩展分区 /dev/sdb5 655 916 2104483+ 83 Linux #逻辑分区 1 /dev/sdb6 917 1178 2104483+ 83 Linux #逻辑分区2 Command (m for help): w #保存并退出 The partition table has been altered! Calling ioctl。to re-read partition table. Syncing disks. [root@localhost -]# #退回到提示符界面
所有的分区立过程中如果不保存并退出是不会生效的,所以建立错了也没有关系,使用 q 命令不保存退出即可。如果使用了 w 命令,就会保存退出。有时因为系统的分区表正忙,所以需要重新启动系统才能使新的分区表生效。命令如下:
Command (m for help): w #保存并退出 The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot. #要求重新启动,才能格式化 Syncing disks.
必须重新启动!
mkfs命令格式化分区
分区完成后,如果不格式化写入文件系统,则是不能正常使用的。所以我们需要使用 mkfs 命令进行格式化。
mkfs 命令格式如下:
[root@localhost ~]# mkfs [选项] 分区设备文件名
选项:-t 文件系统:指定格式化的文件系统,如ext3、ext4;
在Linux分区中 /dev/sdb1(主分区)、/dev/sdb2(扩展分区)、/dev/sdb5(逻辑分区)和 /dev/sdb6(逻辑分区)这几个分区,其中 /dev/sdb2 不能被格式化。剩余的三个分区都需要格式化之后使用。命令如下:
[root@localhost ~]# mkfs -t ext4 /dev/sdb6 mke2fs 1.41.12 (17-May-2010) 文件系统标签= 这里指的是卷标名,我们没有设置卷标 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131648 inodes, 526120 blocks 26306 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=541065216 17 block groups 32768 blocks per group, 32768 fragments per group 7744 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 正在写入inodes表:完成 Creating journal (16384 blocks):完成 Writing superblocks and filesystem accounting information:完成 This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# mkfs -t ext4 /dev/sdb5 #把dev/sdb5也格式化
mkfs 命令非常简单易用,不过是不能调整分区的默认参数的(比如块大小是 4096 Bytes),这些默认参数除非特殊清况,否则不需要调整。如果想要调整,就需要使用 mke2fs 命令重新格式化。命令格式如下:
[root@localhost ~]# mke2fs [选项] 分区设备文件名
选项:
选项 选项说明 -t 文件系统:指定格式化成哪个文件系统, 如 ext2、ext3、ext4 -b 字节:指定 block 的大小 -i 字节:指定"字节 inode "的比例,也就是多少字节分配一个 inode -j 建立带有 ext3 日志功能的文件系统 -L 卷标名:给文件系统设置卷标名,就不使用 e2label 命令设定了
格式化过程如下:
[root@localhost ~]# mke2fs -t ext4 -b 2048 /dev/sdb6 #格式化分区,并指定block的大小为2048 Bytes mke2fe 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=2048 (log=1) #block的大小就不再是4096Bytes 了 分块大小=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 131560 inodes,1052240 blocks 52612 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=538968064 65 block groups 16384 blocks per group, 16384 fragments per group 2024 inodes per group Superblock backups stored on blocks: 16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816 正在写入inode表:完成 Creating journal (32768 blocks):完成 Writing superblocks and filesystem accounting information:完成 This filesystem will be automatically checked every 38 mounts or 180 days, whichever comes first. Use tune2fs -c or-i to override.