第一篇:创建文件系统(格式化)
============================================================================
概述:
============================================================================
创建文件系统(格式化)
1.简介:
★相关概念
文件系统是操作系统用于明确存储设备或分区上的文件的方法和数据结构;即在存储设备上组织文件的方法。操作系统中负责管理和存储文件信息的软件结构称为文件管理系统,简称文件系统。
从系统角度来看,文件系统是对文件存储设备的空间进行组织和分配,负责文件存储并对存入的文件进行保护和检索的系统。具体地说,它负责为用户建立文件,存入、读出、修改、转储文件,控制文件的存取,安全控制,日志,压缩,加密等。
★格式化
低级格式化:(分区之前进行,划分磁道);
高级格式化:(分区之后进行,创建文件系统)
★磁盘分区划分为元数据区和数据区
☉元数据区:
◆文件元数据:inode(index node):
包含文件的大小、权限、属主属组、时间戳、数据块指针;
◆符号链接文件(软链接):
存储数据指针的空间当中存储的是真实文件的访问路径;
◆设备文件:
存储数据指针的空间当中存储的是设备号(major,minor)
2.文件系统组织结构中的常见术语:
☉block groups(块组)
块组内分为元数据区(inode table中又存放indoe)和数据区(block)
☉bitmap index
位图索引 (了解哪些inode已经使用,哪些还没使用)
☉block bitmap
块位图索引:了解哪些磁盘块已经使用,哪些还没有使用;
☉superblock:
超级快记录当前整个分区上有哪些块组,以及块组的划分
附图:
3.Linux的文件系统:
★Linux的文件系统:
VFS:虚拟文件系统接口,当两个层次之间不衔接时,所加的中间层。因此linux可以引入众多的文件系统类型。
附图:
★文件系统分类:
常用的Linux文件系统:ext2(Extended file system), ext3, ext4, xfs(SGI),btrfs(Oracle),reiserfs, jfs(AIX), swap(用于存放虚拟内存)
交换文件系统:swap;
光盘:iso9660;
Windows:fat32, ntfs;
Unix:FFS(fast), UFS(unix), JFS2;
网络文件系统:NFS, CIFS;
集群文件系统:GFS2, OCFS2(oracle);
分布式文件系统:ceph, moosefs, mogilefs, glusterfs, Lustre
RAW:未经处理或者未经格式化产生的文件系
☉根据其是否支持"journal"功能:
日志型文件系统:ext3, ext4, xfs, ...
非日志型文件系统:ext2, vfat
★文件系统的组成部分:
内核中文件系统的驱动(lsmod命令可查看):由内核提供;ext4,xfs,vfat;
用户空间的管理工具:由用户空间的应用程序提供;mkfs.ext4, mkfs.xfs,mkfs.vfat
★查看当前支持的文件系统:
cat /proc/filesystems
演示:
[root@centos7 ~]# cat /proc/filesystems nodev sysfs nodev rootfs nodev bdev nodev proc nodev cgroup nodev cpuset nodev tmpfs nodev devtmpfs nodev debugfs nodev securityfs nodev sockfs nodev pipefs nodev anon_inodefs nodev configfs nodev devpts nodev ramfs nodev hugetlbfs nodev autofs nodev pstore nodev mqueue xfs nodev rpc_pipefs nodev nfsd
文件系统管理工具
1.文件系统管理工具:
★分类:
☉创建文件系统的工具:
◆mkfs
mkfs.ext2,mkfs.ext3,mkfs.ext4,mkfs.xfs,mkfs.vfat
☉检测及修复文件系统的工具:
◆fsck
fsck.ext2,fsck.ext3,fsck.ext4...
☉查看及属性的工具
dumpe2fs,tune2fs
☉调整文件系统特性:
tune2fs
注意:
扩展分区是不能格式化的,因为它存放的是逻辑分区,可对逻辑分区格式化
2.创建ext系列文件系统的工具:
★mkfs命令:(按两次Tab键)
☉工具:
mkfs.ext2,mkfs.ext3,mkfs.ext4
☉语法:# mkfs.FS_TYPE /dev/device
◆FS_TYPE:
ext4,
xfs,
btrfs,
vfat
☉mkfs -t FS_TYPE /dev/DEVICE
-L 'LABEL':设定卷标
mkfs -t ext2=mkfs.ext2
演示:(以CentOS 6 为例)
1.创建ext2文件系统
# 1.首先创建分区,如下: # 查看当前分区情况,可以看见仅有两个分区,说明还可以创建一个主分区和扩展分区 [root@CentOS6 ~]# fdisk -l /dev/sda Disk /dev/sda: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 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: 0x00093c28 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 7859 62914560 8e Linux LVM # 创建分区 [root@CentOS6 ~]# fdisk /dev/sda 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): p Disk /dev/sda: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 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: 0x00093c28 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 7859 62914560 8e Linux LVM Command (m for help): n Command action e extended p primary partition (1-4) p # 创建主分区 Partition number (1-4): 3 First cylinder (7859-10443, default 7859): Using default value 7859 Last cylinder, +cylinders or +size{K,M,G} (7859-10443, default 10443): +5G Command (m for help): n Command action e extended p primary partition (1-4) e # 创建扩展分区 Selected partition 4 First cylinder (8513-10443, default 8513): Using default value 8513 Last cylinder, +cylinders or +size{K,M,G} (8513-10443, default 10443): Using default value 10443 Command (m for help): n # 创建扩展分区上的逻辑分区 First cylinder (8513-10443, default 8513): Using default value 8513 Last cylinder, +cylinders or +size{K,M,G} (8513-10443, default 10443): +3G Command (m for help): p Disk /dev/sda: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 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: 0x00093c28 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 7859 62914560 8e Linux LVM /dev/sda3 7859 8512 5252256 83 Linux /dev/sda4 8513 10443 15510757+ 5 Extended # 扩展分区 /dev/sda5 8513 8905 3156741 83 Linux # 逻辑分区 # 创建一个逻辑分区,并修改其类型为交换分区 swap Command (m for help): n First cylinder (8906-10443, default 8906): Using default value 8906 Last cylinder, +cylinders or +size{K,M,G} (8906-10443, default 10443): +2G Command (m for help): t # 修改其分区类型 Partition number (1-6): 6 Hex code (type L to list codes): 82 # swap 交换分区的 ID为82 Changed system type of partition 6 to 82 (Linux swap / Solaris) Command (m for help): p Disk /dev/sda: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 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: 0x00093c28 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 7859 62914560 8e Linux LVM /dev/sda3 7859 8512 5252256 83 Linux /dev/sda4 8513 10443 15510757+ 5 Extended /dev/sda5 8513 8905 3156741 83 Linux /dev/sda6 8906 9167 2104483+ 82 Linux swap / Solaris # 交换分区 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 or after you run partprobe(8) or kpartx(8) Syncing disks. # 让内核重读分区 [root@CentOS6 ~]# partx -a /dev/sda BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 BLKPG: Device or resource busy error adding partition 4 BLKPG: Device or resource busy error adding partition 5 [root@CentOS6 ~]# partx -a /dev/sda BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 BLKPG: Device or resource busy error adding partition 4 BLKPG: Device or resource busy error adding partition 5 BLKPG: Device or resource busy error adding partition 6 # 分区创建完成 [root@CentOS6 ~]# cat /proc/partitions major minor #blocks name 8 0 83886080 sda 8 1 204800 sda1 8 2 62914560 sda2 8 3 5252256 sda3 8 4 31 sda4 8 5 3156741 sda5 8 6 2104483 sda6 253 0 20971520 dm-0 253 1 2097152 dm-1 253 2 10485760 dm-2 253 3 20971520 dm-3 #=================================================================================== # 对 sda3 分区格式化,创建ext2 文件系统;格式化会删除原有数据,慎重操作 [root@CentOS6 ~]# mkfs.ext2 /dev/sda3 mke2fs 1.41.12 (17-May-2010) Filesystem label= # 文件系统的卷标 OS type: Linux # 操作系统类型 Block size=4096 (log=2) # 块大小 4K Fragment size=4096 (log=2) # 分块大小 Stride=0 blocks, Stripe width=0 blocks 328656 inodes, 1313064 blocks # 一共创建了多少个indoe节点和块 65653 blocks (5.00%) reserved for the super user # 预留给超级管理员的可用空间 First data block=0 Maximum filesystem blocks=1346371584 41 block groups # 一共多少块组 32768 blocks per group, 32768 fragments per group # 每个区块组的块数目; 8016 inodes per group # 每个组内有多少indoe Superblock backups stored on blocks: # 超级快备份所在的数据块 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done # 提示正在写入indoe表:完成 Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 26 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. # 查看创建好的文件系统 [root@CentOS6 ~]# blkid /dev/sda3 /dev/sda3: UUID="de71aae0-914e-4a5f-b00d-eae7f1b53574" TYPE="ext2"
2.在ext2基础上创建ext3文件类型(区别:多了一个创建日志的功能)
[root@CentOS6 ~]# mkfs.ext3 /dev/sda3 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 328656 inodes, 1313064 blocks 65653 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1346371584 41 block groups 32768 blocks per group, 32768 fragments per group 8016 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Creating journal (32768 blocks): done # 多了一个支持日志的功能 Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 26 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. # UUID:为全局唯一标识符 [root@CentOS6 ~]# blkid /dev/sda3 /dev/sda3: UUID="e369a246-bf9d-415c-bf96-70865f933113" SEC_TYPE="ext2" TYPE="ext3"
★mke2fs:ext系列文件系统专用管理工具
☉语法:
mke2fs [OPTIONS] /dev/sd... 默认为ext2文件系统
☉选项:
-t {ext2|ext3|ext4}:指明要创建的文件系统类型;
-b {1024|2048|4096}:指明块大小,默认为4K,最大不超过4K;
-L 'LABEL' :指明卷标;
-j:创建有日志功能的文件系统ext3;(mkfs.ext3 = mkfs -t ext3 = mke2fs -j = mke2fs -t ext3)
-i#:指明indoe与字节的比率;即每多少个字节创建一个inode;此大小不应该小于block的大小;
-N #:直接指明要给此文件系统创建的inode的数量;
-I :一个inode记录大小128---4096;
-m #: 默认5%,为管理人员指定预留空间占总空间的百分比;
-O FEATURE[,...]:以指定特性创建目标文件系统;
如:mke2fs -O has_journal /dev/sda3 (同样为创建ext3 文件系统)
-O ^FEATURE:关闭指定特性;
演示:
# 为磁盘分区/dev/sda3 创建 ext3 文件系统,卷标为"MYDATA" ,块大小为1024K [root@CentOS6 ~]# mke2fs -t ext3 -L MYDATA -b 1024 /dev/sda3 mke2fs 1.41.12 (17-May-2010) Filesystem label=MYDATA # 文件系统标签改变 OS type: Linux Block size=1024 (log=0) # 块大小改变 Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 328704 inodes, 5252256 blocks 262612 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=72613888 642 block groups 8192 blocks per group, 8192 fragments per group 512 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553, 1024001, 1990657, 2809857, 5120001 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 36 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@CentOS6 ~]# blkid /dev/sda3 /dev/sda3: LABEL="MYDATA" UUID="a884f5b1-daa2-47ea-99be-aa8d1891fa9c" SEC_TYPE="ext2" TYPE="ext3"
3.显示及调整文件系统特性的工具:
★e2label命令
☉作用:卷标的查看,预设定
☉用法:
查看:e2label device
设定:e2label device LABEL
★tune2fs 命令
☉作用:
查看或修改ext系列文件系统的某些属性;
☉语法:
tune2fs [options] device
☉选项:
-l:查看指定文件系统超级块信息;super block;
-L 'LABEL':修改卷标;
-m #:调整预留给管理员的空间百分比;
-j:将ext2升级为ext3;
-O[^]FEATHER:文件系统属性启用或禁用,如:–O ^has_journal;
-o[^]mount_options:开启或关闭文件系统的默认挂载选项,如:–o ^acl
-U UUID:修改UUID号
注意:
块大小创建后不可修改!!!
★dumpe2fs 命令
☉作用:
显示ext系列文件系统的属性信息
☉语法:
dumpe2fs [-h] device
☉选项:
-h:查看超级块信息(分组信息),分区用分组管理
演示:
1.-l:查看指定文件系统超级块信息;super block
[root@CentOS6 ~]# tune2fs -l /dev/sda3 tune2fs 1.41.12 (17-May-2010) Filesystem volume name: TAOTAO # 卷标 Last mounted on:# 上次一次挂载于 Filesystem UUID: a884f5b1-daa2-47ea-99be-aa8d1891fa9c # 文件系统UUID Filesystem magic number: 0xEF53 Filesystem revision #: 1 (dynamic) Filesystem features: has_journal ext_attr resize_inode dir_index filetype sparse_super # 文件系统已启用特性 Filesystem flags: signed_directory_hash Default mount options: (none) # 默认的挂载选项 Filesystem state: clean # 文爱你系统状态(干净状态) Errors behavior: Continue # 错误处理方案(继续) Filesystem OS type: Linux # 操作系统类型 Inode count: 328704 # Inode数目 Block count: 5252256 # 块数目 Reserved block count: 262612 # 保留的块数目 Free blocks: 5131992 # 空闲块数目 Free inodes: 328693 # 空闲indoe数 First block: 1 # 第一个块的编号起始位置 Block size: 1024 # 块大小 Fragment size: 1024 # 片段大小 Reserved GDT blocks: 256 Blocks per group: 8192 # 每组多少块 Fragments per group: 8192 Inodes per group: 512 # 每组多少个indoe Inode blocks per group: 128 # 每组多少个块用来存放indoe信息 Filesystem created: Tue Feb 21 03:01:51 2017 Last mount time: n/a Last write time: Tue Feb 21 03:17:36 2017 Mount count: 0 Maximum mount count: 36 Last checked: Tue Feb 21 03:01:51 2017 Check interval: 15552000 (6 months) Next check after: Sun Aug 20 03:01:51 2017 Reserved blocks uid: 0 (user root) Reserved blocks gid: 0 (group root) First inode: 11 Inode size: 256 Required extra isize: 28 Desired extra isize: 28 Journal inode: 8 Default directory hash: half_md4 Directory Hash Seed: dab414a7-f816-482f-9ab9-1fe3061dbc58 Journal backup: inode blocks
2.-m #:调整预留给管理员的空间百分比
[root@CentOS6 ~]# tune2fs -m 2 /dev/sda3 tune2fs 1.41.12 (17-May-2010) Setting reserved blocks percentage to 2% (105045 blocks)
3.-j: 将ext2升级为ext3
[root@CentOS6 ~]# blkid /dev/sda3 /dev/sda3: UUID="ddd258cd-c7bc-48a2-8783-3bb629e21f81" TYPE="ext2" [root@CentOS6 ~]# tune2fs -j /dev/sda3 tune2fs 1.41.12 (17-May-2010) Creating journal inode: done This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@CentOS6 ~]# blkid /dev/sda3 /dev/sda3: UUID="ddd258cd-c7bc-48a2-8783-3bb629e21f81" SEC_TYPE="ext2" TYPE="ext3"
4.-O[^]FEATHER: 文件系统属性启用或禁用,–O ^has_journal
[root@CentOS6 ~]# blkid /dev/sda3 /dev/sda3: UUID="ddd258cd-c7bc-48a2-8783-3bb629e21f81" SEC_TYPE="ext2" TYPE="ext3" # 关闭日志功能,发现文件系统由原来的 ext3 变为 ext2 [root@CentOS6 ~]# tune2fs -O ^has_journal /dev/sda3 tune2fs 1.41.12 (17-May-2010) [root@CentOS6 ~]# blkid /dev/sda3 /dev/sda3: UUID="ddd258cd-c7bc-48a2-8783-3bb629e21f81" TYPE="ext2" # 启用日志功能,发现文件系统由原来的 ext2 变为 ext3 [root@CentOS6 ~]# tune2fs -O has_journal /dev/sda3 tune2fs 1.41.12 (17-May-2010) Creating journal inode: done This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@CentOS6 ~]# blkid /dev/sda3 /dev/sda3: UUID="ddd258cd-c7bc-48a2-8783-3bb629e21f81" SEC_TYPE="ext2" TYPE="ext3"
5.-o: 开启或关闭文件系统的默认挂载选项
由于篇幅限制,后续内容详见博客http://1992tao.blog.51cto.com/11606804/1900034
将会为大家继续介绍用于系统检测的工具、blkid命令,及如何创建swap文件系统,最重要的是文件系统创建后如何使用,挂载的内容!