blob: 03872db6fc5221c6dd68b888a8e6baadf6709005 [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>
30#endif
31
Colin Cross96529862013-01-23 15:38:57 -080032#ifndef USE_MINGW
33#include <selinux/selinux.h>
34#include <selinux/label.h>
Jeff Vander Stoepc28ca562015-05-13 14:29:15 -070035#if !defined(HOST)
Colin Cross96529862013-01-23 15:38:57 -080036#include <selinux/android.h>
Jeff Vander Stoepc28ca562015-05-13 14:29:15 -070037#endif
Colin Cross96529862013-01-23 15:38:57 -080038#else
39struct selabel_handle;
40#endif
41
Doug Zongker263eefd2010-06-29 17:23:14 -070042#include "make_ext4fs.h"
Colin Cross96529862013-01-23 15:38:57 -080043#include "ext4_utils.h"
Doug Zongkeraad1acc2014-06-16 09:07:44 -070044#include "canned_fs_config.h"
Doug Zongker263eefd2010-06-29 17:23:14 -070045
Anatol Pomazau0349bd92012-01-11 15:12:27 -080046#ifndef USE_MINGW /* O_BINARY is windows-specific flag */
47#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");
Doug Zongkerbec598e2014-08-12 11:35:37 -070059 fprintf(stderr, " [ -z | -s ] [ -w ] [ -c ] [ -J ] [ -v ] [ -B <block_list_file> ]\n");
Thierry Strudelb89e81d2015-07-09 16:31:39 -070060 fprintf(stderr, " <filename> [[<directory>] <target_out_directory>]\n");
Doug Zongker263eefd2010-06-29 17:23:14 -070061}
62
63int main(int argc, char **argv)
64{
Anatol Pomazau00740cc2012-02-06 17:13:51 -080065 int opt;
66 const char *filename = NULL;
67 const char *directory = NULL;
Thierry Strudelb89e81d2015-07-09 16:31:39 -070068 const char *target_out_directory = NULL;
Colin Crossa4460142012-12-20 01:00:33 -080069 char *mountpoint = NULL;
Kenny Root68e3dfd2012-03-29 14:43:22 -070070 fs_config_func_t fs_config_func = NULL;
Doug Zongkeraad1acc2014-06-16 09:07:44 -070071 const char *fs_config_file = NULL;
Anatol Pomazau00740cc2012-02-06 17:13:51 -080072 int gzip = 0;
73 int sparse = 0;
74 int crc = 0;
75 int wipe = 0;
Jeff Sharkey7538cc92015-04-06 22:29:04 -070076 int real_uuid = 0;
Anatol Pomazau0349bd92012-01-11 15:12:27 -080077 int fd;
78 int exitcode;
William Roberts20573702013-01-17 13:24:27 -080079 int verbose = 0;
Doug Zongker95266802013-12-05 15:51:28 -080080 time_t fixed_time = -1;
Kenny Root2e5c5232012-03-30 20:38:32 -070081 struct selabel_handle *sehnd = NULL;
Doug Zongkerbec598e2014-08-12 11:35:37 -070082 FILE* block_list_file = NULL;
Kenny Root723f1c72012-10-10 11:13:13 -070083#ifndef USE_MINGW
Kenny Root2e5c5232012-03-30 20:38:32 -070084 struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, "" } };
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -050085#endif
Doug Zongker263eefd2010-06-29 17:23:14 -070086
Jeff Sharkey7538cc92015-04-06 22:29:04 -070087 while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:S:T:C:B:fwzJsctvu")) != -1) {
Anatol Pomazau00740cc2012-02-06 17:13:51 -080088 switch (opt) {
89 case 'l':
90 info.len = parse_num(optarg);
91 break;
92 case 'j':
93 info.journal_blocks = parse_num(optarg);
94 break;
95 case 'b':
96 info.block_size = parse_num(optarg);
97 break;
98 case 'g':
99 info.blocks_per_group = parse_num(optarg);
100 break;
101 case 'i':
102 info.inodes = parse_num(optarg);
103 break;
104 case 'I':
105 info.inode_size = parse_num(optarg);
106 break;
107 case 'L':
108 info.label = optarg;
109 break;
110 case 'f':
111 force = 1;
112 break;
113 case 'a':
Kenny Root68e3dfd2012-03-29 14:43:22 -0700114#ifdef ANDROID
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800115 mountpoint = optarg;
Kenny Root68e3dfd2012-03-29 14:43:22 -0700116#else
117 fprintf(stderr, "can't set android permissions - built without android support\n");
118 usage(argv[0]);
119 exit(EXIT_FAILURE);
120#endif
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800121 break;
122 case 'w':
123 wipe = 1;
124 break;
Jeff Sharkey7538cc92015-04-06 22:29:04 -0700125 case 'u':
126 real_uuid = 1;
127 break;
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800128 case 'z':
129 gzip = 1;
130 break;
Colin Crosse4b5ae82010-08-03 14:10:07 -0700131 case 'J':
132 info.no_journal = 1;
133 break;
Colin Cross757ace52010-12-29 13:57:01 -0800134 case 'c':
135 crc = 1;
136 break;
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800137 case 's':
138 sparse = 1;
139 break;
140 case 't':
Colin Cross56497f22013-02-04 00:44:55 -0800141 fprintf(stderr, "Warning: -t (initialize inode tables) is deprecated\n");
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800142 break;
Kenny Root2e5c5232012-03-30 20:38:32 -0700143 case 'S':
Kenny Root723f1c72012-10-10 11:13:13 -0700144#ifndef USE_MINGW
Kenny Root2e5c5232012-03-30 20:38:32 -0700145 seopts[0].value = optarg;
146 sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
147 if (!sehnd) {
148 perror(optarg);
149 exit(EXIT_FAILURE);
150 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500151#endif
Kenny Root723f1c72012-10-10 11:13:13 -0700152 break;
William Roberts20573702013-01-17 13:24:27 -0800153 case 'v':
154 verbose = 1;
155 break;
Doug Zongker95266802013-12-05 15:51:28 -0800156 case 'T':
157 fixed_time = strtoll(optarg, NULL, 0);
158 break;
Doug Zongkeraad1acc2014-06-16 09:07:44 -0700159 case 'C':
160 fs_config_file = optarg;
161 break;
Doug Zongkerbec598e2014-08-12 11:35:37 -0700162 case 'B':
163 block_list_file = fopen(optarg, "w");
164 if (block_list_file == NULL) {
165 fprintf(stderr, "failed to open block_list_file: %s\n", strerror(errno));
166 exit(EXIT_FAILURE);
167 }
168 break;
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800169 default: /* '?' */
170 usage(argv[0]);
171 exit(EXIT_FAILURE);
172 }
Ken Sumrall75249ed2010-08-13 16:04:49 -0700173 }
174
rpcraig5da2f362012-09-21 11:46:20 -0400175#if !defined(HOST)
176 // Use only if -S option not requested
Colin Crossa4460142012-12-20 01:00:33 -0800177 if (!sehnd && mountpoint) {
rpcraig5da2f362012-09-21 11:46:20 -0400178 sehnd = selinux_android_file_context_handle();
179
180 if (!sehnd) {
181 perror(optarg);
182 exit(EXIT_FAILURE);
183 }
184 }
185#endif
186
Doug Zongkeraad1acc2014-06-16 09:07:44 -0700187 if (fs_config_file) {
188 if (load_canned_fs_config(fs_config_file) < 0) {
189 fprintf(stderr, "failed to load %s\n", fs_config_file);
190 exit(EXIT_FAILURE);
191 }
192 fs_config_func = canned_fs_config;
193 } else if (mountpoint) {
194 fs_config_func = fs_config;
195 }
196
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800197 if (wipe && sparse) {
198 fprintf(stderr, "Cannot specifiy both wipe and sparse\n");
199 usage(argv[0]);
200 exit(EXIT_FAILURE);
201 }
Colin Crossc2470652011-01-26 16:39:46 -0800202
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800203 if (wipe && gzip) {
204 fprintf(stderr, "Cannot specifiy both wipe and gzip\n");
205 usage(argv[0]);
206 exit(EXIT_FAILURE);
207 }
Doug Zongker263eefd2010-06-29 17:23:14 -0700208
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800209 if (optind >= argc) {
210 fprintf(stderr, "Expected filename after options\n");
211 usage(argv[0]);
212 exit(EXIT_FAILURE);
213 }
Doug Zongker263eefd2010-06-29 17:23:14 -0700214
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800215 filename = argv[optind++];
Doug Zongker263eefd2010-06-29 17:23:14 -0700216
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800217 if (optind < argc)
218 directory = argv[optind++];
Doug Zongker263eefd2010-06-29 17:23:14 -0700219
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700220 if (optind < argc)
221 target_out_directory = argv[optind++];
222
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800223 if (optind < argc) {
224 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
225 usage(argv[0]);
226 exit(EXIT_FAILURE);
227 }
228
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800229 if (strcmp(filename, "-")) {
230 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
231 if (fd < 0) {
Colin Cross27f830f2012-12-20 12:24:52 -0800232 perror("open");
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800233 return EXIT_FAILURE;
234 }
235 } else {
236 fd = STDOUT_FILENO;
237 }
238
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700239 exitcode = make_ext4fs_internal(fd, directory, target_out_directory, mountpoint, fs_config_func, gzip,
Jeff Sharkey7538cc92015-04-06 22:29:04 -0700240 sparse, crc, wipe, real_uuid, sehnd, verbose, fixed_time, block_list_file);
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800241 close(fd);
Doug Zongkerbec598e2014-08-12 11:35:37 -0700242 if (block_list_file)
243 fclose(block_list_file);
JP Abgrall39ef30b2014-02-14 17:31:40 -0800244 if (exitcode && strcmp(filename, "-"))
245 unlink(filename);
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800246 return exitcode;
Doug Zongker263eefd2010-06-29 17:23:14 -0700247}