mkfs.ext4 介绍
mkfs.ext4 是一个在 Linux 系统上用于创建 ext4 文件系统的工具。ext4 文件系统是扩展的第三代文件系统 (Fourth Extended Filesystem)
,是 ext3 的改进版,提供了更好的性能和功能。下面是对 mkfs.ext4 工具的详细讲解,包括其用途、选项和常见用法。
用途
mkfs.ext4 用于在分区或文件上创建一个新的 ext4 文件系统。它可以初始化一个分区或文件,使其可以被 Linux 系统识别和使用。
常见用法
mkfs.ext4 [options] device
device:要格式化的设备或文件,例如 /dev/sda1 或 imagefile.img。
常用选项
以下是 mkfs.ext4 的一些常用选项和它们的作用:
-L volume-label
:设置文件系统的卷标。卷标是一个人类可读的标识符。
mkfs.ext4 -L mylabel /dev/sda1
-b block-size
:设置文件系统的块大小。常见值为 1024, 2048, 4096。
mkfs.ext4 -b 4096 /dev/sda1
-m reserved-blocks-percentage
:设置保留块的百分比,默认值是 5%。这些块由超级用户保留,防止文件系统满了后影响系统操作。
mkfs.ext4 -m 1 /dev/sda1
-O feature[,...]
:启用指定的文件系统特性。特性列表可以通过 man mkfs.ext4 查看。
mkfs.ext4 -O ^has_journal /dev/sda1
-T fs-type
:根据文件系统的类型,优化参数。例如news, largefile, largefile4, small
。
mkfs.ext4 -T small /dev/sda1
-F
:强制操作。如果目标设备已经包含文件系统,使用此选项会强制覆盖。
mkfs.ext4 -F /dev/sda1
-N number-of-inodes
:指定文件系统中的 inode 数量。
mkfs.ext4 -N 100000 /dev/sda1
详细选项了解:
man mkfs.ext4
NAME
mke2fs - create an ext2/ext3/ext4 filesystem
SYNOPSIS
mke2fs [ -c | -l filename ] [ -b block-size ] [ -C cluster-size ] [ -d
root-directory ] [ -D ] [ -g blocks-per-group ] [ -G number-of-groups ]
[ -i bytes-per-inode ] [ -I inode-size ] [ -j ] [ -J journal-options ]
[ -N number-of-inodes ] [ -n ] [ -m reserved-blocks-percentage ] [ -o
creator-os ] [ -O [^]feature[,...] ] [ -q ] [ -r fs-revision-level ] [
-E extended-options ] [ -v ] [ -F ] [ -L volume-label ] [ -M last-
mounted-directory ] [ -S ] [ -t fs-type ] [ -T usage-type ] [ -U UUID ]
[ -V ] [ -e errors-behavior ] [ -z undo_file ] device [ fs-size ]
mke2fs -O journal_dev [ -b block-size ] [ -L volume-label ] [ -n ] [ -q
] [ -v ] external-journal [ fs-size ]
DESCRIPTION
mke2fs is used to create an ext2, ext3, or ext4 filesystem, usually in
a disk partition (or file) named by device.
The file system size is specified by fs-size. If fs-size does not have
a suffix, it is interpreted as power-of-two kilobytes, unless the -b
blocksize option is specified, in which case fs-size is interpreted as
the number of blocksize blocks. If the fs-size is suffixed by 'k',
'm', 'g', 't' (either upper-case or lower-case), then it is interpreted
in power-of-two kilobytes, megabytes, gigabytes, terabytes, etc. If
fs-size is omitted, mke2fs will create the file system based on the de‐
vice size.
If mke2fs is run as mkfs.XXX (i.e., mkfs.ext2, mkfs.ext3, or mkfs.ext4)
the option -t XXX is implied; so mkfs.ext3 will create a file system
for use with ext3, mkfs.ext4 will create a file system for use with
ext4, and so on.
The defaults of the parameters for the newly created filesystem, if not
overridden by the options listed below, are controlled by the
/etc/mke2fs.conf configuration file. See the mke2fs.conf(5) manual
page for more details.
Options:
-b block-size
Specify the size of blocks in bytes. Valid block-size values
are 1024, 2048 and 4096 bytes per block. If omitted, block-size
is heuristically determined by the filesystem size and the ex‐
pected usage of the filesystem (see the -T option). If block-
size is preceded by a negative sign ('-'), then mke2fs will use
heuristics to determine the appropriate block size, with the
constraint that the block size will be at least block-size
bytes. This is useful for certain hardware devices which re‐
quire that the blocksize be a multiple of 2k.
-c Check the device for bad blocks before creating the file system.
If this option is specified twice, then a slower read-write test
is used instead of a fast read-only test.
-C cluster-size
Specify the size of cluster in bytes for filesystems using the
bigalloc feature. Valid cluster-size values are from 2048 to
256M bytes per cluster. This can only be specified if the bi‐
galloc feature is enabled. (See the ext4 (5) man page for more
details about bigalloc.) The default cluster size if bigalloc
is enabled is 16 times the block size.
-d root-directory
Copy the contents of the given directory into the root directory
of the filesystem.
-D Use direct I/O when writing to the disk. This avoids mke2fs
dirtying a lot of buffer cache memory, which may impact other
applications running on a busy server. This option will cause
mke2fs to run much more slowly, however, so there is a tradeoff
to using direct I/O.
-e error-behavior
Change the behavior of the kernel code when errors are detected.
In all cases, a filesystem error will cause e2fsck(8) to check
the filesystem on the next boot. error-behavior can be one of
the following:
continue Continue normal execution.
remount-ro Remount filesystem read-only.
panic Cause a kernel panic.
-F Force mke2fs to create a filesystem, even if the specified de‐
vice is not a partition on a block special device, or if other
parameters do not make sense. In order to force mke2fs to cre‐
ate a filesystem even if the filesystem appears to be in use or
is mounted (a truly dangerous thing to do), this option must be
specified twice.
-g blocks-per-group
Specify the number of blocks in a block group. There is gener‐
ally no reason for the user to ever set this parameter, as the
default is optimal for the filesystem. (For administrators who
are creating filesystems on RAID arrays, it is preferable to use
the stride RAID parameter as part of the -E option rather than
manipulating the number of blocks per group.) This option is
generally used by developers who are developing test cases.
If the bigalloc feature is enabled, the -g option will specify
the number of clusters in a block group.
-G number-of-groups
Specify the number of block groups that will be packed together
to create a larger virtual block group (or "flex_bg group") in
an ext4 filesystem. This improves meta-data locality and per‐
formance on meta-data heavy workloads. The number of groups
must be a power of 2 and may only be specified if the flex_bg
filesystem feature is enabled.
-i bytes-per-inode
Specify the bytes/inode ratio. mke2fs creates an inode for ev‐
ery bytes-per-inode bytes of space on the disk. The larger the
bytes-per-inode ratio, the fewer inodes will be created. This
value generally shouldn't be smaller than the blocksize of the
filesystem, since in that case more inodes would be made than
can ever be used. Be warned that it is not possible to change
this ratio on a filesystem after it is created, so be careful
deciding the correct value for this parameter. Note that resiz‐
ing a filesystem changes the number of inodes to maintain this
ratio.
-I inode-size
Specify the size of each inode in bytes. The inode-size value
must be a power of 2 larger or equal to 128. The larger the in‐
ode-size the more space the inode table will consume, and this
reduces the usable space in the filesystem and can also nega‐
tively impact performance. It is not possible to change this
value after the filesystem is created.
In kernels after 2.6.10 and some earlier vendor kernels it is
possible to utilize inodes larger than 128 bytes to store ex‐
tended attributes for improved performance. Extended attributes
stored in large inodes are not visible with older kernels, and
such filesystems will not be mountable with 2.4 kernels at all.
The default inode size is controlled by the mke2fs.conf(5) file.
In the mke2fs.conf file shipped with e2fsprogs, the default in‐
ode size is 256 bytes for most file systems, except for small
file systems where the inode size will be 128 bytes.
-l filename
Read the bad blocks list from filename. Note that the block
numbers in the bad block list must be generated using the same
block size as used by mke2fs. As a result, the -c option to
mke2fs is a much simpler and less error-prone method of checking
a disk for bad blocks before formatting it, as mke2fs will auto‐
matically pass the correct parameters to the badblocks program.
-L new-volume-label
Set the volume label for the filesystem to new-volume-label.
The maximum length of the volume label is 16 bytes.
-m reserved-blocks-percentage
Specify the percentage of the filesystem blocks reserved for the
super-user. This avoids fragmentation, and allows root-owned
daemons, such as syslogd(8), to continue to function correctly
after non-privileged processes are prevented from writing to the
filesystem. The default percentage is 5%.
-M last-mounted-directory
Set the last mounted directory for the filesystem. This might
be useful for the sake of utilities that key off of the last
mounted directory to determine where the filesystem should be
mounted.
-n Causes mke2fs to not actually create a filesystem, but display
what it would do if it were to create a filesystem. This can be
used to determine the location of the backup superblocks for a
particular filesystem, so long as the mke2fs parameters that
were passed when the filesystem was originally created are used
again. (With the -n option added, of course!)
-N number-of-inodes
Overrides the default calculation of the number of inodes that
should be reserved for the filesystem (which is based on the
number of blocks and the bytes-per-inode ratio). This allows
the user to specify the number of desired inodes directly.
-o creator-os
Overrides the default value of the "creator operating system"
field of the filesystem. The creator field is set by default to
the name of the OS the mke2fs executable was compiled for.
-O [^]feature[,...]
Create a filesystem with the given features (filesystem op‐
tions), overriding the default filesystem options. The features
that are enabled by default are specified by the base_features
relation, either in the [defaults] section in the
/etc/mke2fs.conf configuration file, or in the [fs_types] sub‐
sections for the usage types as specified by the -T option, fur‐
ther modified by the features relation found in the [fs_types]
subsections for the filesystem and usage types. See the
mke2fs.conf(5) manual page for more details. The filesystem
type-specific configuration setting found in the [fs_types] sec‐
tion will override the global default found in [defaults].
The filesystem feature set will be further edited using either
the feature set specified by this option, or if this option is
not given, by the default_features relation for the filesystem
type being created, or in the [defaults] section of the configu‐
ration file.
The filesystem feature set is comprised of a list of features,
separated by commas, that are to be enabled. To disable a fea‐
ture, simply prefix the feature name with a caret ('^') charac‐
ter. Features with dependencies will not be removed success‐
fully. The pseudo-filesystem feature "none" will clear all
filesystem features.
For more information about the features which can be set, please see
the manual page ext4(5).
-q Quiet execution. Useful if mke2fs is run in a script.
-S Write superblock and group descriptors only. This is an extreme
measure to be taken only in the very unlikely case that all of
the superblock and backup superblocks are corrupted, and a last-
ditch recovery method is desired by experienced users. It
causes mke2fs to reinitialize the superblock and group descrip‐
tors, while not touching the inode table and the block and inode
bitmaps. The e2fsck program should be run immediately after
this option is used, and there is no guarantee that any data
will be salvageable. Due to the wide variety of possible op‐
tions to mke2fs that affect the on-disk layout, it is critical
to specify exactly the same format options, such as blocksize,
fs-type, feature flags, and other tunables when using this op‐
tion, or the filesystem will be further corrupted. In some
cases, such as filesystems that have been resized, or have had
features enabled after format time, it is impossible to over‐
write all of the superblocks correctly, and at least some
filesystem corruption will occur. It is best to run this on a
full copy of the filesystem so other options can be tried if
this doesn't work.
-t fs-type
Specify the filesystem type (i.e., ext2, ext3, ext4, etc.) that
is to be created. If this option is not specified, mke2fs will
pick a default either via how the command was run (for example,
using a name of the form mkfs.ext2, mkfs.ext3, etc.) or via a
default as defined by the /etc/mke2fs.conf file. This option
controls which filesystem options are used by default, based on
the fstypes configuration stanza in /etc/mke2fs.conf.
If the -O option is used to explicitly add or remove filesystem
options that should be set in the newly created filesystem, the
resulting filesystem may not be supported by the requested fs-
type. (e.g., "mke2fs -t ext3 -O extent /dev/sdXX" will create a
filesystem that is not supported by the ext3 implementation as
found in the Linux kernel; and "mke2fs -t ext3 -O ^has_journal
/dev/hdXX" will create a filesystem that does not have a journal
and hence will not be supported by the ext3 filesystem code in
the Linux kernel.)
-T usage-type[,...]
Specify how the filesystem is going to be used, so that mke2fs
can choose optimal filesystem parameters for that use. The us‐
age types that are supported are defined in the configuration
file /etc/mke2fs.conf. The user may specify one or more usage
types using a comma separated list.
If this option is is not specified, mke2fs will pick a single
default usage type based on the size of the filesystem to be
created. If the filesystem size is less than 3 megabytes,
mke2fs will use the filesystem type floppy. If the filesystem
size is greater than or equal to 3 but less than 512 megabytes,
mke2fs(8) will use the filesystem type small. If the filesystem
size is greater than or equal to 4 terabytes but less than 16
terabytes, mke2fs(8) will use the filesystem type big. If the
filesystem size is greater than or equal to 16 terabytes,
mke2fs(8) will use the filesystem type huge. Otherwise,
mke2fs(8) will use the default filesystem type default.
-U UUID
Set the universally unique identifier (UUID) of the filesystem
to UUID. The format of the UUID is a series of hex digits sepa‐
rated by hyphens, like this:
"c1b9d5a2-f162-11cf-9ece-0020afc76f16". The UUID parameter may
also be one of the following:
clear clear the filesystem UUID
random generate a new randomly-generated UUID
time generate a new time-based UUID
-v Verbose execution.
-V Print the version number of mke2fs and exit.
-z undo_file
Before overwriting a file system block, write the old contents
of the block to an undo file. This undo file can be used with
e2undo(8) to restore the old contents of the file system should
something go wrong. If the empty string is passed as the
undo_file argument, the undo file will be written to a file
named mke2fs-device.e2undo in the directory specified via the
E2FSPROGS_UNDO_DIR environment variable or the undo_dir direc‐
tive in the configuration file.
WARNING: The undo file cannot be used to recover from a power or
system crash.
示例
(1) 创建一个带卷标的 ext4 文件系统:
mkfs.ext4 -L myvolume /dev/sda1
(2) 创建一个带特定块大小的 ext4 文件系统:
mkfs.ext4 -b 2048 /dev/sda1
(3) 在一个文件中创建 ext4 文件系统:
dd if=/dev/zero of=imagefile.img bs=1M count=100
mkfs.ext4 -F imagefile.img
(4) 禁用日志功能:
mkfs.ext4 -O ^has_journal /dev/sda1
(5) 为大文件优化文件系统:
mkfs.ext4 -T largefile /dev/sda1
使用案例
#!/bin/bash
#############TARGET: Build Rootfs Images#############
dd if=/dev/zero of=$ROOTFS_IMAGE_NAME bs=1M count=300
mkfs.ext4 -F -L linuxroot $ROOTFS_IMAGE_NAME
指令解析:
创建空的文件
dd if=/dev/zero of=$ROOTFS_IMAGE_NAME bs=1M count=300
这条命令使用 dd 工具创建一个大小为 300 MB 的文件,并用零填充它。具体来说:
**dd**
:数据复制和转换工具,常用于低级别的数据处理和文件创建。**if=/dev/zero**
:输入文件 (input file) 是 /dev/zero,这是一个特殊的文件,读它会生成无限的零字节。这意味着将从 /dev/zero 读取数据。**of=$ROOTFS_IMAGE_NAME**
:输出文件 (output file) 是环境变量 $ROOTFS_IMAGE_NAME 指定的文件名。这是新创建的文件的名字。**bs=1M**
:设置块大小 (block size) 为 1 MB。这意味着每次读写 1 MB 数据。**count=300**
**:**设置块数 (block count) 为 300。这意味着总共要写 300 个 1 MB 的块,即总大小为 300 MB。
结合起来,整个命令的作用是创建一个大小为 300 MB 的文件,文件名由环境变量 $ROOTFS_IMAGE_NAME
指定,文件内容全部为零。这种文件常用于创建空白的磁盘镜像或虚拟磁盘,用于进一步的文件系统创建或其他操作。
创建 ext4 文件系统
mkfs.ext4 -F -L linuxroot $ROOTFS_IMAGE_NAME #在rootfs.ext4文件上创建ext4文件系统
这条命令使用 **mkfs.ext4**
工具在指定的文件上创建一个 ext4 文件系统。具体来说:
mkfs.ext4
:这是创建 ext4 文件系统的命令。-F
:强制 (force) 操作,即使目标文件看起来不像一个有效的磁盘分区,也强制格式化它。这通常用于格式化一个普通文件或已经包含文件系统的设备。-L linuxroot
:设置文件系统的卷标 (label) 为 linuxroot。卷标是一个人类可读的标识符,用于标记文件系统,可以使用命令如 e2label 或 blkid 查看和更改。$ROOTFS_IMAGE_NAME
:这是要创建文件系统的目标文件名。这个文件名由环境变量$ROOTFS_IMAGE_NAME
指定,应该是之前由 dd 命令创建的那个 300 MB 的文件。
结合起来,这条命令的作用是:
- 在
$ROOTFS_IMAGE_NAME
文件上创建一个 ext4 文件系统。 - 强制执行这个操作,即使文件可能不符合预期的格式。
- 将文件系统的卷标设置为
linuxroot
。
这通常用于准备一个文件作为根文件系统镜像,以便在嵌入式系统或虚拟机中使用。经过这一步,$ROOTFS_IMAGE_NAME
文件就包含了一个可以挂载和使用的 ext4 文件系统。
注意事项
- 使用 mkfs.ext4 会擦除目标设备上的所有数据。因此,在执行此命令之前,务必确认目标设备或文件是正确的。
- 有些选项可能会影响文件系统的兼容性或性能,使用前应查阅相关文档以确保合适的配置。