2012-06-14 02:04:15 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2015-02-09 00:26:29 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-06-14 02:04:15 +08:00
|
|
|
*/
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
#include <confuse.h>
|
2018-03-29 19:49:50 +08:00
|
|
|
|
2018-03-29 05:30:11 +08:00
|
|
|
#include <errno.h>
|
2011-06-27 14:39:51 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-03-29 05:30:11 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
#include "genimage.h"
|
|
|
|
|
2018-03-29 19:49:50 +08:00
|
|
|
struct ext {
|
|
|
|
int use_mke2fs;
|
|
|
|
const char *features;
|
|
|
|
char *usage_type_args;
|
|
|
|
char *conf_env;
|
|
|
|
char *size_features;
|
|
|
|
};
|
|
|
|
|
2018-03-29 05:30:11 +08:00
|
|
|
static int ext2_generate_genext2fs(struct image *image)
|
2011-06-27 14:39:51 +08:00
|
|
|
{
|
|
|
|
int ret;
|
2018-03-29 19:49:50 +08:00
|
|
|
struct ext *ext = image->handler_priv;
|
2017-11-19 17:19:26 +08:00
|
|
|
const char *extraargs = cfg_getstr(image->imagesec, "extraargs");
|
|
|
|
const char *label = cfg_getstr(image->imagesec, "label");
|
2011-06-27 14:39:51 +08:00
|
|
|
|
2019-09-21 18:20:03 +08:00
|
|
|
ret = systemp(image, "%s %s%s%s --size-in-blocks=%lld -i 16384 '%s' %s",
|
2011-06-27 14:39:51 +08:00
|
|
|
get_opt("genext2fs"),
|
2019-09-21 18:20:03 +08:00
|
|
|
image->empty ? "" : "-d '",
|
|
|
|
image->empty ? "" : mountpath(image),
|
|
|
|
image->empty ? "" : "'",
|
|
|
|
image->size / 1024, imageoutfile(image), extraargs);
|
2011-06-27 14:39:51 +08:00
|
|
|
|
2011-10-11 17:05:01 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-03-29 19:49:50 +08:00
|
|
|
if (ext->features && ext->features[0] != '\0') {
|
2018-02-15 21:03:24 +08:00
|
|
|
ret = systemp(image, "%s -O '%s' '%s'", get_opt("tune2fs"),
|
2018-03-29 19:49:50 +08:00
|
|
|
ext->features, imageoutfile(image));
|
2011-10-11 17:05:01 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2012-09-21 16:00:45 +08:00
|
|
|
if (label && label[0] != '\0') {
|
2018-02-15 21:03:24 +08:00
|
|
|
ret = systemp(image, "%s -L '%s' '%s'", get_opt("tune2fs"),
|
2012-09-21 16:00:45 +08:00
|
|
|
label, imageoutfile(image));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-29 05:30:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ext2_generate_mke2fs(struct image *image)
|
|
|
|
{
|
2018-03-29 19:49:50 +08:00
|
|
|
struct ext *ext = image->handler_priv;
|
|
|
|
const char *extraargs = cfg_getstr(image->imagesec, "extraargs");
|
|
|
|
const char *label = cfg_getstr(image->imagesec, "label");
|
2018-11-09 00:56:48 +08:00
|
|
|
const char *root_owner = cfg_getstr(image->imagesec, "root-owner");
|
|
|
|
const char *options = "lazy_itable_init=0,lazy_journal_init=0";
|
2019-11-15 15:21:18 +08:00
|
|
|
const char *features = ext->features;
|
2022-03-25 17:22:03 +08:00
|
|
|
int ret;
|
2018-03-29 05:30:11 +08:00
|
|
|
|
2019-11-15 15:21:18 +08:00
|
|
|
if (features && features[0] == '\0')
|
|
|
|
features = NULL;
|
2018-03-29 05:30:11 +08:00
|
|
|
if (label && label[0] == '\0')
|
|
|
|
label = NULL;
|
|
|
|
|
2022-03-25 17:22:03 +08:00
|
|
|
ret = prepare_image(image, image->size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2018-12-17 21:58:54 +08:00
|
|
|
|
2024-06-07 00:05:52 +08:00
|
|
|
return systemp(image, "%s%s -t %s%s -I 256 -E 'root_owner=%s,%s'%s %s%s%s %s %s%s%s %s%s%s '%s' %lldk",
|
2018-03-29 19:49:50 +08:00
|
|
|
ext->conf_env, get_opt("mke2fs"), image->handler->type,
|
2018-11-09 00:56:48 +08:00
|
|
|
ext->usage_type_args, root_owner, options, ext->size_features,
|
2019-09-21 18:20:03 +08:00
|
|
|
image->empty ? "" : "-d '",
|
|
|
|
image->empty ? "" : mountpath(image),
|
|
|
|
image->empty ? "" : "'",
|
2024-06-07 00:05:52 +08:00
|
|
|
extraargs,
|
|
|
|
label ? "-L '" : "",
|
|
|
|
label ? label : "",
|
|
|
|
label ? "'" : "",
|
2019-11-15 15:21:18 +08:00
|
|
|
features ? "-O '" : "",
|
|
|
|
features ? features : "",
|
|
|
|
features ? "'" : "",
|
2018-03-29 05:30:11 +08:00
|
|
|
imageoutfile(image), image->size / 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ext2_generate(struct image *image)
|
|
|
|
{
|
2018-03-29 19:49:50 +08:00
|
|
|
struct ext *ext = image->handler_priv;
|
2018-03-29 05:30:11 +08:00
|
|
|
const char *fs_timestamp = cfg_getstr(image->imagesec, "fs-timestamp");
|
|
|
|
int ret;
|
|
|
|
|
2018-03-29 19:49:50 +08:00
|
|
|
if (ext->use_mke2fs)
|
2018-03-29 05:30:11 +08:00
|
|
|
ret = ext2_generate_mke2fs(image);
|
2018-03-29 19:49:50 +08:00
|
|
|
else
|
2018-03-29 05:30:11 +08:00
|
|
|
ret = ext2_generate_genext2fs(image);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2011-10-11 17:05:01 +08:00
|
|
|
|
2018-02-15 21:03:24 +08:00
|
|
|
ret = systemp(image, "%s -pvfD '%s'", get_opt("e2fsck"),
|
2011-10-11 17:05:01 +08:00
|
|
|
imageoutfile(image));
|
|
|
|
|
|
|
|
/* e2fsck return 1 when the filesystem was successfully modified */
|
2017-11-19 17:00:13 +08:00
|
|
|
if (ret > 2)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (fs_timestamp) {
|
|
|
|
ret = systemp(image, "echo '"
|
|
|
|
"set_current_time %s\n"
|
|
|
|
"set_super_value mkfs_time %s\n"
|
|
|
|
"set_super_value lastcheck %s\n"
|
2018-02-14 22:58:21 +08:00
|
|
|
"set_super_value mtime 00000000' | %s -w '%s'",
|
2017-11-19 17:00:13 +08:00
|
|
|
fs_timestamp, fs_timestamp, fs_timestamp,
|
|
|
|
get_opt("debugfs"), imageoutfile(image));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
|
2016-11-22 01:37:35 +08:00
|
|
|
static int ext2_setup(struct image *image, cfg_t *cfg)
|
|
|
|
{
|
2018-03-29 19:49:50 +08:00
|
|
|
struct ext *ext = xzalloc(sizeof(*ext));
|
2018-12-17 18:23:59 +08:00
|
|
|
const char *conf = cfg_getstr(image->imagesec, "mke2fs-conf");
|
2018-03-29 19:49:50 +08:00
|
|
|
const char *usage_type = cfg_getstr(image->imagesec, "usage-type");
|
|
|
|
|
2018-12-17 18:23:59 +08:00
|
|
|
if (!conf) {
|
|
|
|
conf = cfg_getstr(image->imagesec, "mke2fs_conf");
|
|
|
|
if (conf)
|
|
|
|
image_info(image, "option 'mke2fs_conf' is deprecated, use mke2fs-conf instead.\n");
|
|
|
|
}
|
|
|
|
|
2016-11-22 01:37:35 +08:00
|
|
|
if (!image->size) {
|
|
|
|
image_error(image, "no size given or must not be zero\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-03-29 19:49:50 +08:00
|
|
|
ext->use_mke2fs = cfg_getbool(cfg, "use-mke2fs");
|
|
|
|
|
|
|
|
ext->features = cfg_getstr(image->imagesec, "features");
|
|
|
|
if (!ext->features) {
|
|
|
|
if (!ext->use_mke2fs) {
|
|
|
|
if (!strcmp(image->handler->type, "ext3"))
|
|
|
|
ext->features = "has_journal";
|
|
|
|
else if (!strcmp(image->handler->type, "ext4"))
|
|
|
|
ext->features = "extents,uninit_bg,dir_index,has_journal";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext->use_mke2fs) {
|
|
|
|
int is_large = image->size >= 4ll * 1024 * 1024 * 1024;
|
|
|
|
int is_huge = image->size >= 2048ll * 1024 * 1024 * 1024;
|
|
|
|
struct stat s;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (conf) {
|
|
|
|
/* mke2fs ignores a missing config file, so make sure it exists. */
|
|
|
|
ret = stat(conf, &s);
|
|
|
|
if (ret) {
|
|
|
|
image_error(image, "mke2fs.conf(%s) does not exist: %s\n",
|
|
|
|
conf, strerror(errno));
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
xasprintf(&ext->conf_env,"MKE2FS_CONFIG=\"%s\" ", conf);
|
|
|
|
} else
|
|
|
|
ext->conf_env = "";
|
|
|
|
|
|
|
|
if (usage_type)
|
|
|
|
xasprintf(&ext->usage_type_args, " -T '%s'", usage_type);
|
|
|
|
else
|
|
|
|
ext->usage_type_args = "";
|
|
|
|
|
|
|
|
xasprintf(&ext->size_features, "%s%s",
|
|
|
|
is_large ? "" : " -O '^large_file'",
|
|
|
|
is_huge ? "" : " -O '^huge_file'");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (conf) {
|
|
|
|
image_error(image, "'mke2fs.conf' is only used for 'mke2fs'\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (usage_type) {
|
|
|
|
image_error(image, "'usage_type' is only used for 'mke2fs'\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
image->handler_priv = ext;
|
|
|
|
|
2016-11-22 01:37:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-29 19:49:50 +08:00
|
|
|
static cfg_opt_t ext_opts[] = {
|
2018-11-09 00:56:48 +08:00
|
|
|
CFG_STR("root-owner", "0:0", CFGF_NONE),
|
2011-06-27 14:39:51 +08:00
|
|
|
CFG_STR("extraargs", "", CFGF_NONE),
|
2018-03-29 19:49:50 +08:00
|
|
|
CFG_STR("features", NULL, CFGF_NONE),
|
2020-10-07 16:09:22 +08:00
|
|
|
CFG_STR("label", NULL, CFGF_NONE),
|
2017-11-19 17:00:13 +08:00
|
|
|
CFG_STR("fs-timestamp", NULL, CFGF_NONE),
|
2024-02-24 17:21:34 +08:00
|
|
|
CFG_BOOL("use-mke2fs", cfg_true, CFGF_NONE),
|
2018-03-29 19:49:50 +08:00
|
|
|
CFG_STR("usage-type", NULL, CFGF_NONE),
|
2020-10-07 16:09:22 +08:00
|
|
|
CFG_STR("mke2fs-conf", NULL, CFGF_NONE),
|
|
|
|
CFG_STR("mke2fs_conf", NULL, CFGF_NONE),
|
2011-06-27 14:39:51 +08:00
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
struct image_handler ext2_handler = {
|
|
|
|
.type = "ext2",
|
|
|
|
.generate = ext2_generate,
|
2016-11-22 01:37:35 +08:00
|
|
|
.setup = ext2_setup,
|
2018-03-29 19:49:50 +08:00
|
|
|
.opts = ext_opts,
|
2011-10-11 17:05:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct image_handler ext3_handler = {
|
|
|
|
.type = "ext3",
|
|
|
|
.generate = ext2_generate,
|
2016-11-22 01:37:35 +08:00
|
|
|
.setup = ext2_setup,
|
2018-03-29 19:49:50 +08:00
|
|
|
.opts = ext_opts,
|
2011-10-11 17:05:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct image_handler ext4_handler = {
|
|
|
|
.type = "ext4",
|
|
|
|
.generate = ext2_generate,
|
2016-11-22 01:37:35 +08:00
|
|
|
.setup = ext2_setup,
|
2018-03-29 19:49:50 +08:00
|
|
|
.opts = ext_opts,
|
2011-10-11 17:05:01 +08:00
|
|
|
};
|
|
|
|
|