前言:
本文耗费笔者大量时间编写,转载声明anyisalin.blog.51cto.com.
本文大多数为操作过程,不对理论知识进行过多阐述
1、文件系统的创建
在Linux创建文件系统有几个工具供我们使用:fdisk,parted...,这里我们只对fdisk进行相应讲解。
fdisk命令的语法fdisk [选项]... [设备文件]
fdisk命令的常用选项-l [设备文件]:不加设备文件路径显示系统上所有磁盘分区的信息,加上设备文件路径显示指定磁盘的分区信息
不加参数显示如下图显示了sda和sdb的分区信息
加参数显示如下图,只显示sda的分区信息
fdisk命令的子选项
fdisk命令的主要功能就是通过子选项来实现的fdisk /dev/sdb #通过这个指令进入fdisk的命令模式对sdb进行操作
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf92588db.
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
Command action
a toggle a bootable flag #设置引导扇区
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition #删指定分区
l list known partition types #列出分区ID列表
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 #改变分区的系统ID
u change display/entry units
v verify the partition table #检查分区表
w write table to disk and exit #将修改保存分区表并退出
x extra functionality (experts only)
##以上有一些操作由于笔者本人并不常用,所以不添加注释
Command (m for help):
光看不练假把式,和我一起在sdb硬盘上创建一个新的分区吧![root@server2 ~]# fdisk /dev/sdb #对sdb硬盘进行操作
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): n #创建一个新的分区
Command action
e extended #扩展分区
p primary partition (1-4) #主分区
p #指定创建分区为主分区
Partition number (1-4): 1 #指定分区编号
First cylinder (1-2610, default 1): #指定起始柱面
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G #创建大小为10G的分区
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 83 #修改分区系统ID为83,其实在Linux中使用fdisk创建完分区默认ID就为83,可不用修改
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: 0xdee177d5
Device Boot Start End Blocks Id System
/dev/sdb1 1 1306 10490413+ 83 Linux
Command (m for help): w #将修改写入分区表并退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
但是很多细心的同学会发现,我们刚创建完分区,但是内核却没有识别出分区
在Linux系统中,当分区进行修改操作时需手动通告给内核或重启才能识别到新建的分区partx -a /dev/sdb #使用partx命令手动通告内核重新读取分区表
partx的基本选项
-a [硬盘文件]:通告指定设备分区或所有分区的修改给内核
-n [m,n]:通告m到n范围内的设备的修改给内核
2、文件系统格式化cat /proc/filesystems #查看当前系统支持的文件系统
mkfs命令用法:mkfs.FStype /dev/partition #将指定分区格式化为指定文件系统 例如# mkfs.ext4 /dev/sdb1
如图:mkfs在centos6.7中支持的分区格式,centos7中支持btrfs,xfs等更加高级的文件系统
[root@server2 ~]# mkfs.ext4 /dev/sdb1 #格式化分区sdb1为ext4分区
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655776 inodes, 2622603 blocks
131130 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
mke2fs:ext系列文件系统专用管理工具常用选项:
-t {ext2|ext3|ext4}
-b {1024|2048|4096}块大小,默认块大小为4096
-L 'LABEL':指定卷标
-j 相当于 -t ext3
-i # 为数据空间每多少个字节创建一个inode, 此大小不应该小于block大小
-N # 为数据空间创建多少个inode
-m # 为管理人员预留的空间占据的百分比
-o FEATURE [...]:启动指定特性
-O ^FEATURE:关闭指定特性
mkswap:创建交换分区mkswap [OPTIONS] device
-L 'LABEL'
##使用前提,分区为格式化或调整分区ID为82
其他常用工具:
blkid:块设备属性信息查看
blkid [OPTION].. [DEVICE]
-U UUID:根据指定的UUID来查找设备
-L LABEL:根据指定指定卷标来查找对应的设备
e2label:管理ext系列文件系统的LABELe2label DEVICE [LABEL]
tune2fs:重新设定ext系列文件系统可调整参数的值-l:查看指定文件系统超级块信息, super block
-L 'LABEL':修改卷标
-m #:预留给管理员的空间百分比
-j:将ext2升级为ext3
-O:文件系统属性启用或禁用
-o:调整文件系统的默认挂在选项
-U UUID:修改UUID号
dump2fs:-h:查看超级块信息
3、文件系统检测
fsck:file system checkfsck的使用方法与mkfs类似
fsck.FStype /dev/partition ##例如 fsck.ext4 /dev/sdb1 检查/dev/sdb1的文件系统 注意:前面指定的文件系统必须要和指定的分区文件系统相同
-a:自动修复错误
-r:交互式修复错误
总结:本文只针对创建文件系统,格式化文件系统主要进行详细阐述,后面文件系统修改,检测由于没有实际遇到相应需要修改,检测文件系统的情况,所以并没有过多的写