简介
一般说到磁盘分区及格式化有部分人还是会比较陌生的,特别是在Linux下没有windows中的各种大师,将会显得比较无助,让人感觉难以触摸。那么本文将对磁盘的分区以及格式化进行分步讲解,让你逐步了解将一块初始化的磁盘给你,你要如何变成你自己能够使用的文件分区。
(PS: 本文运行环境是美团云的机器,系统是CentOS 6.5,挂载1T的云硬盘,本文将针对一块全新的未格式化10G的云硬盘进行分区格式化处理。)
命令了解
首先我们来对将要用到的一些命令进行熟悉
命令:
fdisk
功能:
查看磁盘使用情况和分割磁盘
使用方法:
一、在 console 上输入 fdisk -l /dev/sda ,观察硬盘使用情况
二、在 console 上输入 fdisk /dev/sda,可进入分割硬盘
1. 输入 m 打印各命令的菜单。
2. 输入 p 打印硬盘分区列表。
3. 输入 a 设定硬盘启动区。
4. 输入 n 创建一个新的分区。
4.1. 输入 e 硬盘为[延伸]分割区(extend)。
4.2. 输入 p 硬盘为[主要]分割区(primary)。
5. 输入 t 改变硬盘分割区属性。
6. 输入 d 删除硬盘分割区属性。
7. 输入 q 不保存退出。
8. 输入 w 保存退出。
命令:
mkfs
功能:
格式化硬盘
格式:
mkfs –t 文件系统类型 分区设备
参数:
-t : 给定档案系统的型式,Linux 的预设值为 ext2
-c : 在制做档案系统前,检查该partition 是否有坏轨
-V : 详细显示模式
开始分区
首先我们执行一下fdisk -l来查看目前已有磁盘的情况
[root@jx-web:~]# fdisk -l
Disk /dev/vda: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 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: 0x000bce79
Device Boot Start End Blocks Id System
/dev/vda1 * 1 3264 26213376 83 Linux
Disk /dev/vdb: 34.4 GB, 34359738368 bytes
255 heads, 63 sectors/track, 4177 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: 0x00062e8c
Device Boot Start End Blocks Id System
/dev/vdb1 1 4177 33551721 82 Linux swap / Solaris
Disk /dev/vdc: 1073.7 GB, 1073741824000 bytes
16 heads, 63 sectors/track, 2080507 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0902eabd
Device Boot Start End Blocks Id System
/dev/vdc1 1 2080507 1048575496+ 83 Linux
Disk /dev/vdd: 10.7 GB, 10737418240 bytes
16 heads, 63 sectors/track, 20805 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
其中各个项的具体含义如下:
- Device:分区的设备文件名称。
- Boot:是否是引导分区,是,则带有“*”标识。
- Start:该分区在硬盘中的起始位置(柱面数)。
- End:该分区在硬盘中的结束位置(柱面数)。
- Blocks:分区的大小,以Blocks(块)为单位,默认的块大小为1024字节。
- Id:分区类型的ID标记号,对于EXT4分区为83,LVM分区为8e。
- System:分区类型。
然后我们可以看到我的机器系统分区26.8G,然后swap是34.4G,还有1T的硬盘,我们可以看到这些磁盘分别都有挂载点的,所以这些是已经分好区了的,可以看到最后一块10G的硬盘(/dev/vdd)就是我们今天的目标硬盘了,接下来开始动刀子了~~
[root@jx-web ~]# fdisk /dev/vdd
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x2e355a48.
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):
在操作界面中的“Command (m for help):”提示符后,用户可以输入特定的分区操作指令,完成各项分区管理任务。例如输入“m”指令后,可以查看各种操作指令的帮助信息。具体帮助信息就不在展示了哈,常用的指令也在开头为大家介绍了。
接下来输入“n”开始分区
Command (m for help): n
Command action
e extended
p primary partition (1-4)
这里是选择扩展分区或者主分区,我们直接选择p主分区(当选择e扩展分区之后,该分区是不能直接格式化的,只能格式化主分区和逻辑分区,所以这个时候我们只能再次进行逻辑分区,当再次对扩展分区进行分区的时候是可以选择逻辑分区的,下面我们将会为大家讲解扩展分区的创建)
选择p主分区之后我们继续~~
Partition number (1-4): 1
First cylinder (1-20805, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-20805, default 20805): +5G
Command (m for help):
选择分区号时,主分区和扩展分区的序号只能在1~4之间。分区起始位置一般由fdisk默认识别即可,结束位置或大小可以使用“+size(K、M、G)”的形式,如“+size5G”表示将该分区的容量设置为5GB。cylinder是指定分区的开始柱面,建议默认1开始。
分区结束之后,可以输入p查看创建好的分区/dev/vdd1
Command (m for help): p
Disk /dev/vdd: 10.7 GB, 10737418240 bytes
16 heads, 63 sectors/track, 20805 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x19652849
Device Boot Start End Blocks Id System
/dev/vdd1 1 10404 5243584+ 83 Linux
下面为大家继续演示逻辑分区的创建,创建逻辑分区之前首先需要创建扩展分区,而且必须把所有剩余空间全部分给扩展分区。
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 4
First cylinder (10405-20805, default 10405):
Using default value 10405
Last cylinder, +cylinders or +size{K,M,G} (10405-20805, default 20805):
Using default value 20805
Command (m for help): p
Disk /dev/vdd: 10.7 GB, 10737418240 bytes
16 heads, 63 sectors/track, 20805 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x19652849
Device Boot Start End Blocks Id System
/dev/vdd1 1 10404 5243584+ 83 Linux
/dev/vdd4 10405 20805 5242104 5 Extended
扩展分区创建好之后,接着就可以创建逻辑分区。在创建逻辑分区的时候就不需要指定分区编号了,系统将会自动从5开始顺序编号。
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (10405-20805, default 10405):
Using default value 10405
Last cylinder, +cylinders or +size{K,M,G} (10405-20805, default 20805): +2G
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (14567-20805, default 14567):
Using default value 14567
Last cylinder, +cylinders or +size{K,M,G} (14567-20805, default 20805):
Using default value 20805
Command (m for help): p
Disk /dev/vdd: 10.7 GB, 10737418240 bytes
16 heads, 63 sectors/track, 20805 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x19652849
Device Boot Start End Blocks Id System
/dev/vdd1 1 10404 5243584+ 83 Linux
/dev/vdd4 10405 20805 5242104 5 Extended
/dev/vdd5 10405 14566 2097616+ 83 Linux
/dev/vdd6 14567 20805 3144424+ 83 Linux
到目前为止我们完成了这块硬盘的分区,我们分了3区,一个主分区,2个逻辑分区,接下来我们输入w进行保存并退出,或者输入q则不会记录本次分区的记录。
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
硬盘分区设置完成以后,一般需要将系统重启以使设置生效,如果不想重启系统,可以使用“partprobe”命令使操作系统获知新的分区表情况。(如果没有partprobe的话,CentOS用户可以尝试“yum install parted”进行安装)。
如果需要删除已创建好的分区,可以在fdisk命令操作界面中使用“d”指令将指定的分区删除,根据提示输入需要删除的分区序号即可。在删除时建议从最后一个分区开始进行删除,以免fdisk识别的分区序号发生紊乱。另外,如果扩展分区被删除,则扩展分区之下的逻辑分区也将同时被删除。
格式化分区
分区创建好之后,还必须要经过格式化才能使用,格式化分区的主要目的是在分区中创建文件系统。Linux专用的文件系统是ext,包含ext2、ext3、ext4等诸多版本,在RHEL6中默认使用的是ext4。另外,Linux也支持Windows中的FAT32文件系统,在Linux中表示为vfat。
下面我们将对/dev/vdd1格式化为ext4文件系统,同时检查是否有坏轨存在,并且将过程详细列出来
[root@jx-web ~]# mkfs -V -t ext4 -c /dev/vdd1
mkfs (util-linux-ng 2.17.2)
mkfs.ext4 -c /dev/vdd1
mke2fs 1.41.12 (17-May-2010)
警告: 176 块未使用.
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
328320 inodes, 1310720 blocks
65544 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8208 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Checking for bad blocks (read-only test): 完成
正在写入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.
将/dev/vdd5格式化为fat32文件系统。
[root@jx-web ~]# mkfs -t vfat /dev/vdd5
mkfs (util-linux-ng 2.17.2)
mkfs.vfat /dev/vdd5
mkfs.vfat 3.0.9 (31 Jan 2010)
(如果执行过程打印
mkfs (util-linux-ng 2.17.2)
mkfs.vfat /dev/vdd5
mkfs.vfat: 没有那个文件或目录
那么是你的环境没有安装dosfstools,运行一下“yum install dosfstools”再次格式化就好了)
[root@jx-web ~]# mkfs.ext4 /dev/vdd6
具体打印就不显示了哈~~
到现在为止,我们格式化完成,再来看一下分好的区
Disk /dev/vdd: 10.7 GB, 10737418240 bytes
16 heads, 63 sectors/track, 20805 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x19652849
Device Boot Start End Blocks Id System
/dev/vdd1 1 10404 5243584+ 83 Linux
/dev/vdd4 10405 20805 5242104 5 Extended
/dev/vdd5 10405 14566 2097616+ 83 Linux
/dev/vdd6 14567 20805 3144424+ 83 Linux
文件系统的挂载
在安装Linux系统的过程中,自动建立或识别的分区通常会由系统自动完成挂载,如“/”分区、“boot”分区等,对于后来新增加的硬盘分区、优盘、光盘等设备,就必须由管理员手动进行挂载。挂载一个分区时,必须为其指定一个目录作为挂载点,用户通过这个目录就可以访问设备中的文件、目录数据。
磁盘分好区格式化好了,接下来就是要使用了~~
临时挂载:
[root@jx-web ~]# mkdir /data1 /data2 /data3
[root@jx-web ~]# mount /dev/vdd1 /data1
[root@jx-web ~]# mount /dev/vdd5 /data2
[root@jx-web ~]# mount /dev/vdd6 /data3
[root@jx-web ~]# df -hT //查看挂载情况
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 25G 4.3G 20G 19% /
tmpfs tmpfs 16G 72K 16G 1% /dev/shm
/dev/vdc1 ext4 985G 23G 912G 3% /opt
/dev/vdd1 ext4 4.8G 10M 4.6G 1% /data1
/dev/vdd5 vfat 2.0G 4.0K 2.0G 1% /data2
/dev/vdd6 ext4 2.9G 4.5M 2.8G 1% /data3
永久挂载:
[root@jx-web ~]# vim /etc/fstab
/dev/vdd1 /data1 ext4 defaults 0 0
/dev/vdd5 /data2 vfat defaults 0 0
/dev/vdd6 /data3 ext4 defaults 0 0
到此为止本文为大家介绍了一块磁盘从原始状态到一块在文件系统中可以使用的磁盘,今天就为大家介绍到这里。
(PS: 本文运行环境是美团云的机器,系统是CentOS 6.5,挂载1T的云硬盘,本文将针对一块全新的未格式化10G的云硬盘进行分区格式化处理。)