8.5 mkfs:创建Linux文件系统
mkfs命令用于在指定的设备(或硬盘分区等)上创建格式化并创建文件系统,fdisk和parted等分区工具相当于建房的人,把房子(硬盘),分成几居室(分区),mkfs就相当于对不同的居室装修(创建文件系统)了,只有装修好的房子(有文件系统)才能入住,分区也是一样,只有格式化创建文件系统(存取数据的机制)后,才能用来存取数据。 mkfs [选项] [设备名] -t 指定要创建的文件系统类型 -c 创建文件系统时检查磁盘坏块 -v 显示详细信息 mkfs只是一个前端命令,它通过-t参数指定文件系统类型后会调用相应的命令mkfs.fstype。因此可以直接使用mkfs.ext4这个命令创建ext4文件系统。[root@cs6 ~]# ls /sbin/mkfs* /sbin/mkfs /sbin/mkfs.ext2 /sbin/mkfs.ext4 /sbin/mkfs.xfs /sbin/mkfs.cramfs /sbin/mkfs.ext3 /sbin/mkfs.ext4dev
通过mkfs 命令创建文件系统(-t参数)的例子
[root@cs6 ~]# mkfs -t ext4 -v /dev/sdb mke2fs 1.41.12 (17-May-2010) /dev/sdb is entire device, not just one partition! Proceed anyway? (y,n) y fs_types for mke2fs.conf resolution: 'ext4', 'default' Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
通过mkfs.ext4创建文件系统
[root@cs6 ~]# mkfs.ext4 /dev/sdb mke2fs 1.41.12 (17-May-2010) /dev/sdb is entire device, not just one partition! Proceed anyway? (y,n) y Filesystem label= OS type: Linux Block size=4096 (log=2) .... Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 25 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
8.6 dumpe2fs:导出ext2/ext3/ext4文件系统信息
dumpe2fs命令用于导出ext2/ext3/ext4文件系统内部的相关信息,例如:文件系统的组成包含超级快、块组、inode、block等信息。 dumpe2fs命令的参数选项及说明 -b 打印文件系统中预留的块信息 -h 仅显示超级块信息 -i 从指定的文件系统映像文件中读取文件的系统信息 -x 以16进制格式打印信息块成员查看系统的inode信息
[root@cs6 ~]# dumpe2fs /dev/sda1|egrep -i "inode size|inode count" dumpe2fs 1.41.12 (17-May-2010) Inode count: 128016 #<==单位;个。 Inode size: 128 #<== /boot 分区默认128字节。 [root@cs6 ~]# dumpe2fs /dev/mapper/vg_cs6-lv_root |egrep -i "inode size|inode count" dumpe2fs 1.41.12 (17-May-2010) Inode count: 1738080 Inode size: 256 #<==普通分区默认256字节。 [root@cs6 ~]# df -i Filesystem Inodes IUsed IFree IUse% Mounted on /dev/mapper/vg_cs6-lv_root 1738080 23534 1714546 2% / tmpfs 238145 1 238144 1% /dev/shm /dev/sda1 128016 38 127978 1% /boot [root@cs6 ~]# dumpe2fs /dev/sda1|egrep -i "block size|block count" dumpe2fs 1.41.12 (17-May-2010) Block count: 512000 #<==分区的block总量。 Reserved block count: 25600 Block size: 1024 #<==/boot分区默认1024字节即1K [root@cs6 ~]# dumpe2fs /dev/mapper/vg_cs6-lv_root |egrep -i "block size|block count" dumpe2fs 1.41.12 (17-May-2010) Block count: 6948864 Reserved block count: 347443 Block size: 4096 #<==普通分区4K=4096字节。
8.7 resize2fs:调整ext2/ext3/ext4文件系统大小
resize2fs命令用于扩容或收缩未挂载的ext2/ext3/ext4文件系统。在Linux2.6或更高 版本的内核中,该命令还支持在线扩容已经挂载的文件系统,该命令常用来针对LVM扩容 后的分区使用。 -p 打印完成任务的进度条 -f 强制执行操作动态修改分区大小的例子
出现这种情况一般是当初对系统分区的时候没有规划好,现在想要重新调整分区的大小。假设是要对/dev/sdb上的分区进行操作,将/mnt/data1扩容。[root@cs6 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_cs6-lv_root 26G 834M 24G 4% / tmpfs 931M 0 931M 0% /dev/shm /dev/sda1 477M 28M 424M 7% /boot /dev/sdb1 194M 1.8M 182M 1% /mnt/data1 /dev/sdb2 194M 1.8M 182M 1% /mnt/data2 [root@cs6 ~]# touch /mnt/data1/{1..5}.html [root@cs6 ~]# touch /mnt/data2/{1..5}.html
提示:/dev/sdb是一块1GB的磁盘,现在分了两个主分区sdb1、sdb2,分别是200MB,剩余800MB未分区。现在需要把sdb2分区和sdb1分区合并,以实现对sdb1的扩容,注意,此种情况不要在生产场景操作,仅作为演示用,生产场景一般是事先规划好不会出现扩容需求,非I/O密集应用可以采用LVM实现规范动态扩容。 以下是扩容实战步骤。 1)记录分区的柱面起始信息:
[root@cs6 ~]# fdisk -l /dev/sdb Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 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: 0x1bc7d413 Device Boot Start End Blocks Id System /dev/sdb1 1 26 208813+ 83 Linux /dev/sdb2 27 52 208845 83 Linux2)卸载分区:
[root@cs6 ~]# umount /mnt/data1 [root@cs6 ~]# umount /mnt/data2 [root@cs6 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_cs6-lv_root 26G 834M 24G 4% / tmpfs 931M 0 931M 0% /dev/shm /dev/sda1 477M 28M 424M 7% /boot
3)重新分区:
[root@cs6 ~]# fdisk /dev/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): d Partition number (1-4): 1 Command (m for help): d Selected partition 2 Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-130, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): 52 Command (m for help): p Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 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: 0x1bc7d413 Device Boot Start End Blocks Id System /dev/sdb1 1 52 417658+ 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@cs6 ~]# partprobe /dev/sdb [root@cs6 ~]# mount /dev/sdb1 /mnt/data1 [root@cs6 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_cs6-lv_root 26G 834M 24G 4% / tmpfs 931M 0 931M 0% /dev/shm /dev/sda1 477M 28M 424M 7% /boot /dev/sdb1 194M 1.8M 182M 1% /mnt/data1 [root@cs6 ~]# ls /mnt/data1 1.html 2.html 3.html 4.html 5.html lost+found [root@cs6 ~]# resize2fs /dev/sdb1 #<==在线调整磁盘大小, resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/sdb1 is mounted on /mnt/data1; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/sdb1 to 417656 (1k) blocks. The filesystem on /dev/sdb1 is now 417656 blocks long. [root@cs6 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_cs6-lv_root 26G 834M 24G 4% / tmpfs 931M 0 931M 0% /dev/shm /dev/sda1 477M 28M 424M 7% /boot /dev/sdb1 392M 2.3M 369M 1% /mnt/data1 [root@cs6 ~]# ls /mnt/data1 1.html 2.html 3.html 4.html 5.html lost+found
#<==但是数据还是只有sdb1里的了,sdb2的数据丢失了。此种方法不适合生产场景扩容,比较规范方法是通过LVM逻辑卷管理进行扩容,扩容后也需要resize2fs进行最终实现扩容。