forked from pengutronix/genimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-ext2.c
231 lines (197 loc) · 6.07 KB
/
image-ext2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (c) 2011 Sascha Hauer <[email protected]>, 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <confuse.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "genimage.h"
struct ext {
int use_mke2fs;
const char *features;
char *usage_type_args;
char *conf_env;
char *size_features;
};
static int ext2_generate_genext2fs(struct image *image)
{
int ret;
struct ext *ext = image->handler_priv;
const char *extraargs = cfg_getstr(image->imagesec, "extraargs");
const char *label = cfg_getstr(image->imagesec, "label");
ret = systemp(image, "%s %s%s%s --size-in-blocks=%lld -i 16384 '%s' %s",
get_opt("genext2fs"),
image->empty ? "" : "-d '",
image->empty ? "" : mountpath(image),
image->empty ? "" : "'",
image->size / 1024, imageoutfile(image), extraargs);
if (ret)
return ret;
if (ext->features && ext->features[0] != '\0') {
ret = systemp(image, "%s -O '%s' '%s'", get_opt("tune2fs"),
ext->features, imageoutfile(image));
if (ret)
return ret;
}
if (label && label[0] != '\0') {
ret = systemp(image, "%s -L '%s' '%s'", get_opt("tune2fs"),
label, imageoutfile(image));
if (ret)
return ret;
}
return 0;
}
static int ext2_generate_mke2fs(struct image *image)
{
struct ext *ext = image->handler_priv;
const char *extraargs = cfg_getstr(image->imagesec, "extraargs");
const char *label = cfg_getstr(image->imagesec, "label");
const char *root_owner = cfg_getstr(image->imagesec, "root-owner");
const char *options = "lazy_itable_init=0,lazy_journal_init=0";
if (label && label[0] == '\0')
label = NULL;
if (is_block_device(imageoutfile(image)))
pad_file(image, NULL, 2048, 0x0, MODE_OVERWRITE);
return systemp(image, "%s%s -t %s%s -E 'root_owner=%s,%s'%s %s%s%s %s %s%s '%s' %lld",
ext->conf_env, get_opt("mke2fs"), image->handler->type,
ext->usage_type_args, root_owner, options, ext->size_features,
image->empty ? "" : "-d '",
image->empty ? "" : mountpath(image),
image->empty ? "" : "'",
extraargs, label ? "-L " : "", label ? label : "",
imageoutfile(image), image->size / 1024);
}
static int ext2_generate(struct image *image)
{
struct ext *ext = image->handler_priv;
const char *fs_timestamp = cfg_getstr(image->imagesec, "fs-timestamp");
int ret;
if (ext->use_mke2fs)
ret = ext2_generate_mke2fs(image);
else
ret = ext2_generate_genext2fs(image);
if (ret)
return ret;
ret = systemp(image, "%s -pvfD '%s'", get_opt("e2fsck"),
imageoutfile(image));
/* e2fsck return 1 when the filesystem was successfully modified */
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"
"set_super_value mtime 00000000' | %s -w '%s'",
fs_timestamp, fs_timestamp, fs_timestamp,
get_opt("debugfs"), imageoutfile(image));
if (ret)
return ret;
}
return 0;
}
static int ext2_setup(struct image *image, cfg_t *cfg)
{
struct ext *ext = xzalloc(sizeof(*ext));
const char *conf = cfg_getstr(image->imagesec, "mke2fs-conf");
const char *usage_type = cfg_getstr(image->imagesec, "usage-type");
if (!conf) {
conf = cfg_getstr(image->imagesec, "mke2fs_conf");
if (conf)
image_info(image, "option 'mke2fs_conf' is deprecated, use mke2fs-conf instead.\n");
}
if (!image->size) {
image_error(image, "no size given or must not be zero\n");
return -EINVAL;
}
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;
return 0;
}
static cfg_opt_t ext_opts[] = {
CFG_STR("root-owner", "0:0", CFGF_NONE),
CFG_STR("extraargs", "", CFGF_NONE),
CFG_STR("features", NULL, CFGF_NONE),
CFG_STR("label", 0, CFGF_NONE),
CFG_STR("fs-timestamp", NULL, CFGF_NONE),
CFG_BOOL("use-mke2fs", cfg_false, CFGF_NONE),
CFG_STR("usage-type", NULL, CFGF_NONE),
CFG_STR("mke2fs-conf", 0, CFGF_NONE),
CFG_STR("mke2fs_conf", 0, CFGF_NONE),
CFG_END()
};
struct image_handler ext2_handler = {
.type = "ext2",
.generate = ext2_generate,
.setup = ext2_setup,
.opts = ext_opts,
};
struct image_handler ext3_handler = {
.type = "ext3",
.generate = ext2_generate,
.setup = ext2_setup,
.opts = ext_opts,
};
struct image_handler ext4_handler = {
.type = "ext4",
.generate = ext2_generate,
.setup = ext2_setup,
.opts = ext_opts,
};