blob: 87cb53e63b1e927181cc27c8d0b284337ea81efb [file] [log] [blame]
Doug Zongker263eefd2010-06-29 17:23:14 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Anatol Pomazau0349bd92012-01-11 15:12:27 -080017#include <fcntl.h>
Anatol Pomazau354350e2012-02-03 18:20:07 -080018#include <libgen.h>
Colin Cross96529862013-01-23 15:38:57 -080019#include <stdio.h>
Anatol Pomazau0349bd92012-01-11 15:12:27 -080020#include <unistd.h>
Doug Zongker263eefd2010-06-29 17:23:14 -070021
22#if defined(__linux__)
23#include <linux/fs.h>
24#elif defined(__APPLE__) && defined(__MACH__)
25#include <sys/disk.h>
26#endif
27
Kenny Root68e3dfd2012-03-29 14:43:22 -070028#ifdef ANDROID
29#include <private/android_filesystem_config.h>
Mohamad Ayyashaa8b3552016-04-07 22:15:57 -070030#include <private/canned_fs_config.h>
Kenny Root68e3dfd2012-03-29 14:43:22 -070031#endif
32
Elliott Hughes1eb2f542016-10-05 09:44:48 -070033#ifndef _WIN32
Colin Cross96529862013-01-23 15:38:57 -080034#include <selinux/selinux.h>
35#include <selinux/label.h>
Jeff Vander Stoepc28ca562015-05-13 14:29:15 -070036#if !defined(HOST)
Colin Cross96529862013-01-23 15:38:57 -080037#include <selinux/android.h>
Jeff Vander Stoepc28ca562015-05-13 14:29:15 -070038#endif
Colin Cross96529862013-01-23 15:38:57 -080039#else
40struct selabel_handle;
41#endif
42
Tao Bao018ef1b2016-10-05 12:44:18 -070043#include "ext4_utils/ext4_utils.h"
44#include "ext4_utils/make_ext4fs.h"
Doug Zongker263eefd2010-06-29 17:23:14 -070045
Elliott Hughes1eb2f542016-10-05 09:44:48 -070046#ifndef _WIN32 /* O_BINARY is windows-specific flag */
Anatol Pomazau0349bd92012-01-11 15:12:27 -080047#define O_BINARY 0
48#endif
49
Doug Zongker263eefd2010-06-29 17:23:14 -070050extern struct fs_info info;
51
52
53static void usage(char *path)
54{
Anatol Pomazau00740cc2012-02-06 17:13:51 -080055 fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path));
56 fprintf(stderr, " [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n");
Jeff Sharkey7538cc92015-04-06 22:29:04 -070057 fprintf(stderr, " [ -L <label> ] [ -f ] [ -a <android mountpoint> ] [ -u ]\n");
Doug Zongkeraad1acc2014-06-16 09:07:44 -070058 fprintf(stderr, " [ -S file_contexts ] [ -C fs_config ] [ -T timestamp ]\n");
Mohamad Ayyash18785a82016-02-19 21:16:34 +000059 fprintf(stderr, " [ -z | -s ] [ -w ] [ -c ] [ -J ] [ -v ] [ -B <block_list_file> ]\n");
Mohamad Ayyash95791982016-02-20 03:46:00 +000060 fprintf(stderr, " [ -d <base_alloc_file_in> ] [ -D <base_alloc_file_out> ]\n");
Thierry Strudelb89e81d2015-07-09 16:31:39 -070061 fprintf(stderr, " <filename> [[<directory>] <target_out_directory>]\n");
Doug Zongker263eefd2010-06-29 17:23:14 -070062}
63
64int main(int argc, char **argv)
65{
Anatol Pomazau00740cc2012-02-06 17:13:51 -080066 int opt;
67 const char *filename = NULL;
68 const char *directory = NULL;
Thierry Strudelb89e81d2015-07-09 16:31:39 -070069 const char *target_out_directory = NULL;
Colin Crossa4460142012-12-20 01:00:33 -080070 char *mountpoint = NULL;
Kenny Root68e3dfd2012-03-29 14:43:22 -070071 fs_config_func_t fs_config_func = NULL;
Doug Zongkeraad1acc2014-06-16 09:07:44 -070072 const char *fs_config_file = NULL;
Anatol Pomazau00740cc2012-02-06 17:13:51 -080073 int gzip = 0;
74 int sparse = 0;
75 int crc = 0;
76 int wipe = 0;
Jeff Sharkey7538cc92015-04-06 22:29:04 -070077 int real_uuid = 0;
Anatol Pomazau0349bd92012-01-11 15:12:27 -080078 int fd;
79 int exitcode;
William Roberts20573702013-01-17 13:24:27 -080080 int verbose = 0;
Doug Zongker95266802013-12-05 15:51:28 -080081 time_t fixed_time = -1;
Kenny Root2e5c5232012-03-30 20:38:32 -070082 struct selabel_handle *sehnd = NULL;
Doug Zongkerbec598e2014-08-12 11:35:37 -070083 FILE* block_list_file = NULL;
Mohamad Ayyash95791982016-02-20 03:46:00 +000084 FILE* base_alloc_file_in = NULL;
85 FILE* base_alloc_file_out = NULL;
Elliott Hughes1eb2f542016-10-05 09:44:48 -070086#ifndef _WIN32
Kenny Root2e5c5232012-03-30 20:38:32 -070087 struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, "" } };
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -050088#endif
Doug Zongker263eefd2010-06-29 17:23:14 -070089
Mohamad Ayyash95791982016-02-20 03:46:00 +000090 while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:S:T:C:B:d:D:fwzJsctvu")) != -1) {
Anatol Pomazau00740cc2012-02-06 17:13:51 -080091 switch (opt) {
92 case 'l':
93 info.len = parse_num(optarg);
94 break;
95 case 'j':
96 info.journal_blocks = parse_num(optarg);
97 break;
98 case 'b':
99 info.block_size = parse_num(optarg);
100 break;
101 case 'g':
102 info.blocks_per_group = parse_num(optarg);
103 break;
104 case 'i':
105 info.inodes = parse_num(optarg);
106 break;
107 case 'I':
108 info.inode_size = parse_num(optarg);
109 break;
110 case 'L':
111 info.label = optarg;
112 break;
113 case 'f':
114 force = 1;
115 break;
116 case 'a':
Kenny Root68e3dfd2012-03-29 14:43:22 -0700117#ifdef ANDROID
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800118 mountpoint = optarg;
Kenny Root68e3dfd2012-03-29 14:43:22 -0700119#else
120 fprintf(stderr, "can't set android permissions - built without android support\n");
121 usage(argv[0]);
122 exit(EXIT_FAILURE);
123#endif
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800124 break;
125 case 'w':
126 wipe = 1;
127 break;
Jeff Sharkey7538cc92015-04-06 22:29:04 -0700128 case 'u':
129 real_uuid = 1;
130 break;
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800131 case 'z':
132 gzip = 1;
133 break;
Colin Crosse4b5ae82010-08-03 14:10:07 -0700134 case 'J':
135 info.no_journal = 1;
136 break;
Colin Cross757ace52010-12-29 13:57:01 -0800137 case 'c':
138 crc = 1;
139 break;
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800140 case 's':
141 sparse = 1;
142 break;
143 case 't':
Colin Cross56497f22013-02-04 00:44:55 -0800144 fprintf(stderr, "Warning: -t (initialize inode tables) is deprecated\n");
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800145 break;
Kenny Root2e5c5232012-03-30 20:38:32 -0700146 case 'S':
Elliott Hughes1eb2f542016-10-05 09:44:48 -0700147#ifndef _WIN32
Kenny Root2e5c5232012-03-30 20:38:32 -0700148 seopts[0].value = optarg;
149 sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
150 if (!sehnd) {
151 perror(optarg);
152 exit(EXIT_FAILURE);
153 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500154#endif
Kenny Root723f1c72012-10-10 11:13:13 -0700155 break;
William Roberts20573702013-01-17 13:24:27 -0800156 case 'v':
157 verbose = 1;
158 break;
Doug Zongker95266802013-12-05 15:51:28 -0800159 case 'T':
160 fixed_time = strtoll(optarg, NULL, 0);
161 break;
Doug Zongkeraad1acc2014-06-16 09:07:44 -0700162 case 'C':
163 fs_config_file = optarg;
164 break;
Doug Zongkerbec598e2014-08-12 11:35:37 -0700165 case 'B':
166 block_list_file = fopen(optarg, "w");
167 if (block_list_file == NULL) {
168 fprintf(stderr, "failed to open block_list_file: %s\n", strerror(errno));
169 exit(EXIT_FAILURE);
170 }
171 break;
Mohamad Ayyash95791982016-02-20 03:46:00 +0000172 case 'd':
173 base_alloc_file_in = fopen(optarg, "r");
174 if (base_alloc_file_in == NULL) {
175 fprintf(stderr, "failed to open base_alloc_file_in: %s\n", strerror(errno));
176 exit(EXIT_FAILURE);
177 }
178 break;
179 case 'D':
180 base_alloc_file_out = fopen(optarg, "w");
181 if (base_alloc_file_out == NULL) {
182 fprintf(stderr, "failed to open base_alloc_file_out: %s\n", strerror(errno));
183 exit(EXIT_FAILURE);
184 }
185 break;
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800186 default: /* '?' */
187 usage(argv[0]);
188 exit(EXIT_FAILURE);
189 }
Ken Sumrall75249ed2010-08-13 16:04:49 -0700190 }
191
rpcraig5da2f362012-09-21 11:46:20 -0400192#if !defined(HOST)
193 // Use only if -S option not requested
Colin Crossa4460142012-12-20 01:00:33 -0800194 if (!sehnd && mountpoint) {
rpcraig5da2f362012-09-21 11:46:20 -0400195 sehnd = selinux_android_file_context_handle();
196
197 if (!sehnd) {
198 perror(optarg);
199 exit(EXIT_FAILURE);
200 }
201 }
202#endif
203
Doug Zongkeraad1acc2014-06-16 09:07:44 -0700204 if (fs_config_file) {
205 if (load_canned_fs_config(fs_config_file) < 0) {
206 fprintf(stderr, "failed to load %s\n", fs_config_file);
207 exit(EXIT_FAILURE);
208 }
209 fs_config_func = canned_fs_config;
210 } else if (mountpoint) {
211 fs_config_func = fs_config;
212 }
213
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800214 if (wipe && sparse) {
215 fprintf(stderr, "Cannot specifiy both wipe and sparse\n");
216 usage(argv[0]);
217 exit(EXIT_FAILURE);
218 }
Colin Crossc2470652011-01-26 16:39:46 -0800219
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800220 if (wipe && gzip) {
221 fprintf(stderr, "Cannot specifiy both wipe and gzip\n");
222 usage(argv[0]);
223 exit(EXIT_FAILURE);
224 }
Doug Zongker263eefd2010-06-29 17:23:14 -0700225
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800226 if (optind >= argc) {
227 fprintf(stderr, "Expected filename after options\n");
228 usage(argv[0]);
229 exit(EXIT_FAILURE);
230 }
Doug Zongker263eefd2010-06-29 17:23:14 -0700231
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800232 filename = argv[optind++];
Doug Zongker263eefd2010-06-29 17:23:14 -0700233
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800234 if (optind < argc)
235 directory = argv[optind++];
Doug Zongker263eefd2010-06-29 17:23:14 -0700236
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700237 if (optind < argc)
238 target_out_directory = argv[optind++];
239
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800240 if (optind < argc) {
241 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
242 usage(argv[0]);
243 exit(EXIT_FAILURE);
244 }
245
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800246 if (strcmp(filename, "-")) {
247 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
248 if (fd < 0) {
Colin Cross27f830f2012-12-20 12:24:52 -0800249 perror("open");
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800250 return EXIT_FAILURE;
251 }
252 } else {
253 fd = STDOUT_FILENO;
254 }
255
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700256 exitcode = make_ext4fs_internal(fd, directory, target_out_directory, mountpoint, fs_config_func, gzip,
Mohamad Ayyash95791982016-02-20 03:46:00 +0000257 sparse, crc, wipe, real_uuid, sehnd, verbose, fixed_time,
258 block_list_file, base_alloc_file_in, base_alloc_file_out);
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800259 close(fd);
Doug Zongkerbec598e2014-08-12 11:35:37 -0700260 if (block_list_file)
261 fclose(block_list_file);
Mohamad Ayyash95791982016-02-20 03:46:00 +0000262 if (base_alloc_file_out)
263 fclose(base_alloc_file_out);
264 if (base_alloc_file_in)
265 fclose(base_alloc_file_in);
JP Abgrall39ef30b2014-02-14 17:31:40 -0800266 if (exitcode && strcmp(filename, "-"))
267 unlink(filename);
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800268 return exitcode;
Doug Zongker263eefd2010-06-29 17:23:14 -0700269}