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 05:30:11 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.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 <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
#include "genimage.h"
|
|
|
|
|
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;
|
2017-11-19 17:19:26 +08:00
|
|
|
const char *extraargs = cfg_getstr(image->imagesec, "extraargs");
|
|
|
|
const char *features = cfg_getstr(image->imagesec, "features");
|
|
|
|
const char *label = cfg_getstr(image->imagesec, "label");
|
2011-06-27 14:39:51 +08:00
|
|
|
|
2018-02-15 21:03:24 +08:00
|
|
|
ret = systemp(image, "%s -d '%s' --size-in-blocks=%lld -i 16384 '%s' %s",
|
2011-06-27 14:39:51 +08:00
|
|
|
get_opt("genext2fs"),
|
2011-10-10 22:38:14 +08:00
|
|
|
mountpath(image), image->size / 1024, imageoutfile(image),
|
2011-06-27 14:39:51 +08:00
|
|
|
extraargs);
|
|
|
|
|
2011-10-11 17:05:01 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2012-09-21 16:00:20 +08:00
|
|
|
if (features && features[0] != '\0') {
|
2018-02-15 21:03:24 +08:00
|
|
|
ret = systemp(image, "%s -O '%s' '%s'", get_opt("tune2fs"),
|
2011-10-11 17:05:01 +08:00
|
|
|
features, imageoutfile(image));
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char *extraargs = cfg_getstr(image->imagesec, "extraargs");
|
|
|
|
char *label = cfg_getstr(image->imagesec, "label");
|
|
|
|
char *conf = cfg_getstr(image->imagesec, "mke2fs_conf");
|
|
|
|
char *conf_env = "";
|
|
|
|
|
|
|
|
if (label && label[0] == '\0')
|
|
|
|
label = NULL;
|
|
|
|
|
|
|
|
if (conf) {
|
|
|
|
int fd;
|
|
|
|
/* mke2fs ignores a missing config file, so make sure it exists. */
|
|
|
|
fd = open(conf, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
int ret = errno;
|
|
|
|
image_error(image, "Failed to open mke2fs_conf '%s': %s\n",
|
|
|
|
conf, strerror(ret));
|
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
xasprintf(&conf_env, "MKE2FS_CONFIG='%s' ", conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return systemp(image, "%s%s -t %s -E root_owner=0:0 "
|
|
|
|
"-E lazy_itable_init=0,lazy_journal_init=0 "
|
|
|
|
"%s -d '%s' %s %s%s '%s' %lld",
|
|
|
|
conf_env, get_opt("mke2fs"), image->handler->type,
|
|
|
|
image->size < 0x20000000000 ? "-O ^huge_file" : "",
|
|
|
|
mountpath(image), extraargs,
|
|
|
|
label ? "-L " : "", label ? label : "",
|
|
|
|
imageoutfile(image), image->size / 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ext2_generate(struct image *image)
|
|
|
|
{
|
|
|
|
const char *fs_timestamp = cfg_getstr(image->imagesec, "fs-timestamp");
|
|
|
|
const char *tool = cfg_getstr(image->imagesec, "tool");
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (strcmp(tool, "mke2fs") == 0)
|
|
|
|
ret = ext2_generate_mke2fs(image);
|
|
|
|
else if (strcmp(tool, "genext2fs") == 0)
|
|
|
|
ret = ext2_generate_genext2fs(image);
|
|
|
|
else {
|
|
|
|
image_error(image, "unknown tool to create %s images: %s",
|
|
|
|
image->handler->type, tool);
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!image->size) {
|
|
|
|
image_error(image, "no size given or must not be zero\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
static cfg_opt_t ext2_opts[] = {
|
|
|
|
CFG_STR("extraargs", "", CFGF_NONE),
|
2011-10-11 17:05:01 +08:00
|
|
|
CFG_STR("features", 0, CFGF_NONE),
|
2012-09-21 16:00:45 +08:00
|
|
|
CFG_STR("label", 0, CFGF_NONE),
|
2017-11-19 17:00:13 +08:00
|
|
|
CFG_STR("fs-timestamp", NULL, CFGF_NONE),
|
2018-03-29 05:30:11 +08:00
|
|
|
CFG_STR("tool", "genext2fs", CFGF_NONE),
|
|
|
|
CFG_STR("mke2fs_conf", 0, 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,
|
2011-06-27 14:39:51 +08:00
|
|
|
.opts = ext2_opts,
|
|
|
|
};
|
|
|
|
|
2011-10-11 17:05:01 +08:00
|
|
|
static cfg_opt_t ext3_opts[] = {
|
|
|
|
CFG_STR("extraargs", "", CFGF_NONE),
|
|
|
|
CFG_STR("features", "has_journal", CFGF_NONE),
|
2012-09-21 16:00:45 +08:00
|
|
|
CFG_STR("label", 0, CFGF_NONE),
|
2017-11-19 17:00:13 +08:00
|
|
|
CFG_STR("fs-timestamp", NULL, CFGF_NONE),
|
2018-03-29 05:30:11 +08:00
|
|
|
CFG_STR("tool", "genext2fs", CFGF_NONE),
|
|
|
|
CFG_STR("mke2fs_conf", 0, CFGF_NONE),
|
2011-10-11 17:05:01 +08:00
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
struct image_handler ext3_handler = {
|
|
|
|
.type = "ext3",
|
|
|
|
.generate = ext2_generate,
|
2016-11-22 01:37:35 +08:00
|
|
|
.setup = ext2_setup,
|
2011-10-11 17:05:01 +08:00
|
|
|
.opts = ext3_opts,
|
|
|
|
};
|
|
|
|
|
|
|
|
static cfg_opt_t ext4_opts[] = {
|
|
|
|
CFG_STR("extraargs", "", CFGF_NONE),
|
|
|
|
CFG_STR("features", "extents,uninit_bg,dir_index,has_journal", CFGF_NONE),
|
2012-09-21 16:00:45 +08:00
|
|
|
CFG_STR("label", 0, CFGF_NONE),
|
2017-11-19 17:00:13 +08:00
|
|
|
CFG_STR("fs-timestamp", NULL, CFGF_NONE),
|
2018-03-29 05:30:11 +08:00
|
|
|
CFG_STR("tool", "genext2fs", CFGF_NONE),
|
|
|
|
CFG_STR("mke2fs_conf", 0, CFGF_NONE),
|
2011-10-11 17:05:01 +08:00
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
struct image_handler ext4_handler = {
|
|
|
|
.type = "ext4",
|
|
|
|
.generate = ext2_generate,
|
2016-11-22 01:37:35 +08:00
|
|
|
.setup = ext2_setup,
|
2011-10-11 17:05:01 +08:00
|
|
|
.opts = ext4_opts,
|
|
|
|
};
|
|
|
|
|