1 /* febootstrap-supermin-helper reimplementation in C.
2 * Copyright (C) 2009-2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include "xvasprintf.h"
36 #include "ext2internal.h"
40 /* The ext2 image that we build always has a fixed size, and we 'hope'
41 * that the files fit in (otherwise we'll get an error). Note that
42 * the file is sparsely allocated.
44 * The downside of allocating a very large initial disk is that the
45 * fixed overhead of ext2 is larger (since ext2 calculates it based on
46 * the size of the disk). For a 1GB disk the overhead is
49 * In future, make this configurable, or determine it from the input
52 #define APPLIANCE_SIZE (1024*1024*1024)
55 ext2_start (const char *hostcpu, const char *appliance,
56 const char *modpath, const char *initrd)
58 initialize_ext2_error_table ();
60 /* Make the initrd. */
61 ext2_make_initrd (modpath, initrd);
63 /* Make the appliance sparse image. */
64 int fd = open (appliance, O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY, 0644);
66 error (EXIT_FAILURE, errno, "open: %s", appliance);
68 if (lseek (fd, APPLIANCE_SIZE - 1, SEEK_SET) == -1)
69 error (EXIT_FAILURE, errno, "lseek");
72 if (write (fd, &c, 1) != 1)
73 error (EXIT_FAILURE, errno, "write");
76 error (EXIT_FAILURE, errno, "close");
78 /* Run mke2fs on the file.
79 * XXX Quoting, but this string doesn't come from an untrusted source.
81 char *cmd = xasprintf ("%s -t ext2 -F%s '%s'",
83 verbose >= 2 ? "" : "q",
86 if (r == -1 || WEXITSTATUS (r) != 0)
87 error (EXIT_FAILURE, 0, "%s: failed", cmd);
91 print_timestamped_message ("finished mke2fs");
93 /* Open the filesystem. */
95 ext2fs_open (appliance, EXT2_FLAG_RW, 0, 0, unix_io_manager, &fs);
97 error (EXIT_FAILURE, 0, "ext2fs_open: %s", error_message (err));
99 /* Bitmaps are not loaded by default, so load them. ext2fs_close will
100 * write out any changes.
102 err = ext2fs_read_bitmaps (fs);
104 error (EXIT_FAILURE, 0, "ext2fs_read_bitmaps: %s", error_message (err));
110 /* Write out changes and close. */
111 errcode_t err = ext2fs_close (fs);
113 error (EXIT_FAILURE, 0, "ext2fs_close: %s", error_message (err));
117 ext2_mkdir (ext2_ino_t dir_ino, const char *dirname, const char *basename,
118 mode_t mode, uid_t uid, gid_t gid,
119 time_t ctime, time_t atime, time_t mtime)
123 mode = LINUX_S_IFDIR | (mode & 0777);
125 /* Does the directory exist? This is legitimate: we just skip
129 err = ext2fs_namei (fs, EXT2_ROOT_INO, dir_ino, basename, &ino);
133 /* Otherwise, create it. */
134 err = ext2fs_new_inode (fs, dir_ino, mode, 0, &ino);
136 error (EXIT_FAILURE, 0, "ext2fs_new_inode: %s", error_message (err));
139 err = ext2fs_mkdir (fs, dir_ino, ino, basename);
141 /* See: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=217892 */
142 if (err == EXT2_ET_DIR_NO_SPACE) {
143 err = ext2fs_expand_dir (fs, dir_ino);
145 error (EXIT_FAILURE, 0, "ext2fs_expand_dir: %s/%s: %s",
146 dirname, basename, error_message (err));
149 error (EXIT_FAILURE, 0, "ext2fs_mkdir: %s/%s: %s",
150 dirname, basename, error_message (err));
153 /* Copy the final permissions, UID etc. to the inode. */
154 struct ext2_inode inode;
155 err = ext2fs_read_inode (fs, ino, &inode);
157 error (EXIT_FAILURE, 0, "ext2fs_read_inode: %s", error_message (err));
161 inode.i_ctime = ctime;
162 inode.i_atime = atime;
163 inode.i_mtime = mtime;
164 err = ext2fs_write_inode (fs, ino, &inode);
166 error (EXIT_FAILURE, 0, "ext2fs_write_inode: %s", error_message (err));
170 ext2_empty_inode (ext2_ino_t dir_ino, const char *dirname, const char *basename,
171 mode_t mode, uid_t uid, gid_t gid,
172 time_t ctime, time_t atime, time_t mtime,
173 int major, int minor, int dir_ft, ext2_ino_t *ino_ret)
176 struct ext2_inode inode;
179 err = ext2fs_new_inode (fs, dir_ino, mode, 0, &ino);
181 error (EXIT_FAILURE, 0, "ext2fs_new_inode: %s", error_message (err));
183 memset (&inode, 0, sizeof inode);
188 inode.i_links_count = 1;
189 inode.i_ctime = ctime;
190 inode.i_atime = atime;
191 inode.i_mtime = mtime;
193 inode.i_block[0] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
195 err = ext2fs_write_new_inode (fs, ino, &inode);
197 error (EXIT_FAILURE, 0, "ext2fs_write_inode: %s", error_message (err));
199 ext2_link (dir_ino, basename, ino, dir_ft);
201 ext2fs_inode_alloc_stats2 (fs, ino, 1, 0);
207 /* You must create the file first with ext2_empty_inode. */
209 ext2_write_file (ext2_ino_t ino, const char *buf, size_t size)
213 err = ext2fs_file_open2 (fs, ino, NULL, EXT2_FILE_WRITE, &file);
215 error (EXIT_FAILURE, 0, "ext2fs_file_open2: %s", error_message (err));
217 /* ext2fs_file_write cannot deal with partial writes. You have
218 * to write the entire file in a single call.
220 unsigned int written;
221 err = ext2fs_file_write (file, buf, size, &written);
223 error (EXIT_FAILURE, 0, "ext2fs_file_write: %s", error_message (err));
224 if ((size_t) written != size)
225 error (EXIT_FAILURE, 0,
226 "ext2fs_file_write: size = %zu != written = %u\n",
229 err = ext2fs_file_flush (file);
231 error (EXIT_FAILURE, 0, "ext2fs_file_flush: %s", error_message (err));
232 err = ext2fs_file_close (file);
234 error (EXIT_FAILURE, 0, "ext2fs_file_close: %s", error_message (err));
236 /* Update the true size in the inode. */
237 struct ext2_inode inode;
238 err = ext2fs_read_inode (fs, ino, &inode);
240 error (EXIT_FAILURE, 0, "ext2fs_read_inode: %s", error_message (err));
242 err = ext2fs_write_inode (fs, ino, &inode);
244 error (EXIT_FAILURE, 0, "ext2fs_write_inode: %s", error_message (err));
247 /* This is just a wrapper around ext2fs_link which calls
248 * ext2fs_expand_dir as necessary if the directory fills up. See
249 * definition of expand_dir in the sources of debugfs.
252 ext2_link (ext2_ino_t dir_ino, const char *basename, ext2_ino_t ino, int dir_ft)
257 err = ext2fs_link (fs, dir_ino, basename, ino, dir_ft);
259 if (err == EXT2_ET_DIR_NO_SPACE) {
260 err = ext2fs_expand_dir (fs, dir_ino);
262 error (EXIT_FAILURE, 0, "ext2_link: ext2fs_expand_dir: %s: %s",
263 basename, error_message (err));
268 error (EXIT_FAILURE, 0, "ext2fs_link: %s: %s",
269 basename, error_message (err));
273 release_block (ext2_filsys fs, blk_t *blocknr,
274 int blockcnt, void *private)
279 ext2fs_block_alloc_stats (fs, block, -1);
283 /* unlink or rmdir path, if it exists. */
285 ext2_clean_path (ext2_ino_t dir_ino,
286 const char *dirname, const char *basename,
292 err = ext2fs_lookup (fs, dir_ino, basename, strlen (basename),
294 if (err == EXT2_ET_FILE_NOT_FOUND)
298 struct ext2_inode inode;
299 err = ext2fs_read_inode (fs, ino, &inode);
301 error (EXIT_FAILURE, 0, "ext2fs_read_inode: %s", error_message (err));
302 inode.i_links_count--;
303 err = ext2fs_write_inode (fs, ino, &inode);
305 error (EXIT_FAILURE, 0, "ext2fs_write_inode: %s", error_message (err));
307 err = ext2fs_unlink (fs, dir_ino, basename, 0, 0);
309 error (EXIT_FAILURE, 0, "ext2fs_unlink_inode: %s", error_message (err));
311 if (inode.i_links_count == 0) {
312 inode.i_dtime = time (NULL);
313 err = ext2fs_write_inode (fs, ino, &inode);
315 error (EXIT_FAILURE, 0, "ext2fs_write_inode: %s", error_message (err));
317 if (ext2fs_inode_has_valid_blocks (&inode)) {
319 /* From the docs: "BLOCK_FLAG_READ_ONLY is a promise by the
320 * caller that it will not modify returned block number."
321 * RHEL 5 does not have this flag, so just omit it if it is
324 #ifdef BLOCK_FLAG_READ_ONLY
325 flags |= BLOCK_FLAG_READ_ONLY;
327 ext2fs_block_iterate (fs, ino, flags, NULL,
328 release_block, NULL);
331 ext2fs_inode_alloc_stats2 (fs, ino, -1, isdir);
334 /* else it's a directory, what to do? XXX */
337 /* Read in the whole file into memory. Check the size is still 'size'. */
339 read_whole_file (const char *filename, size_t size)
341 char *buf = malloc (size);
343 error (EXIT_FAILURE, errno, "malloc");
345 int fd = open (filename, O_RDONLY);
347 error (EXIT_FAILURE, errno, "open: %s", filename);
353 ssize_t r = read (fd, p, size - n);
355 error (EXIT_FAILURE, errno, "read: %s", filename);
357 error (EXIT_FAILURE, 0,
358 "error: file has changed size unexpectedly: %s", filename);
363 if (close (fd) == -1)
364 error (EXIT_FAILURE, errno, "close: %s", filename);
369 /* Add a file (or directory etc) from the host. */
371 ext2_file_stat (const char *orig_filename, const struct stat *statbuf)
376 fprintf (stderr, "ext2_file_stat %s 0%o\n",
377 orig_filename, statbuf->st_mode);
379 /* Sanity check the path. These rules are always true for the paths
380 * passed to us here from the appliance layer. The assertions just
381 * verify that the rules haven't changed.
383 size_t n = strlen (orig_filename);
384 assert (n <= PATH_MAX);
386 assert (orig_filename[0] == '/'); /* always absolute path */
387 assert (n == 1 || orig_filename[n-1] != '/'); /* no trailing slash */
389 /* Don't make the root directory, it always exists. This simplifies
390 * the code that follows.
394 const char *dirname, *basename;
395 const char *p = strrchr (orig_filename, '/');
397 if (orig_filename == p) { /* "/foo" */
399 basename = orig_filename+1;
400 dir_ino = EXT2_ROOT_INO;
401 } else { /* "/foo/bar" */
402 dirname = strndup (orig_filename, p-orig_filename);
405 /* Look up the parent directory. */
406 err = ext2fs_namei (fs, EXT2_ROOT_INO, EXT2_ROOT_INO, dirname, &dir_ino);
408 error (EXIT_FAILURE, 0, "ext2: parent directory not found: %s: %s",
409 dirname, error_message (err));
412 ext2_clean_path (dir_ino, dirname, basename, S_ISDIR (statbuf->st_mode));
416 /* Create regular file. */
417 if (S_ISREG (statbuf->st_mode)) {
418 /* XXX Hard links get duplicated here. */
420 ext2_empty_inode (dir_ino, dirname, basename,
421 statbuf->st_mode, statbuf->st_uid, statbuf->st_gid,
422 statbuf->st_ctime, statbuf->st_atime, statbuf->st_mtime,
423 0, 0, EXT2_FT_REG_FILE, &ino);
425 if (statbuf->st_size > 0) {
426 char *buf = read_whole_file (orig_filename, statbuf->st_size);
427 ext2_write_file (ino, buf, statbuf->st_size);
431 /* Create a symlink. */
432 else if (S_ISLNK (statbuf->st_mode)) {
434 ext2_empty_inode (dir_ino, dirname, basename,
435 statbuf->st_mode, statbuf->st_uid, statbuf->st_gid,
436 statbuf->st_ctime, statbuf->st_atime, statbuf->st_mtime,
437 0, 0, EXT2_FT_SYMLINK, &ino);
439 char buf[PATH_MAX+1];
440 ssize_t r = readlink (orig_filename, buf, sizeof buf);
442 error (EXIT_FAILURE, errno, "readlink: %s", orig_filename);
443 ext2_write_file (ino, buf, r);
445 /* Create directory. */
446 else if (S_ISDIR (statbuf->st_mode))
447 ext2_mkdir (dir_ino, dirname, basename,
448 statbuf->st_mode, statbuf->st_uid, statbuf->st_gid,
449 statbuf->st_ctime, statbuf->st_atime, statbuf->st_mtime);
450 /* Create a special file. */
451 else if (S_ISBLK (statbuf->st_mode)) {
452 dir_ft = EXT2_FT_BLKDEV;
455 else if (S_ISCHR (statbuf->st_mode)) {
456 dir_ft = EXT2_FT_CHRDEV;
458 } else if (S_ISFIFO (statbuf->st_mode)) {
459 dir_ft = EXT2_FT_FIFO;
461 } else if (S_ISSOCK (statbuf->st_mode)) {
462 dir_ft = EXT2_FT_SOCK;
464 ext2_empty_inode (dir_ino, dirname, basename,
465 statbuf->st_mode, statbuf->st_uid, statbuf->st_gid,
466 statbuf->st_ctime, statbuf->st_atime, statbuf->st_mtime,
467 major (statbuf->st_rdev), minor (statbuf->st_rdev),
473 ext2_file (const char *filename)
477 if (lstat (filename, &statbuf) == -1)
478 error (EXIT_FAILURE, errno, "lstat: %s", filename);
479 ext2_file_stat (filename, &statbuf);
482 /* In theory this could be optimized to avoid a namei lookup, but
483 * it probably wouldn't make much difference.
486 ext2_fts_entry (FTSENT *entry)
488 if (entry->fts_info & FTS_NS || entry->fts_info & FTS_NSOK)
489 ext2_file (entry->fts_path);
491 ext2_file_stat (entry->fts_path, entry->fts_statp);
494 struct writer ext2_writer = {
495 .wr_start = ext2_start,
497 .wr_file = ext2_file,
498 .wr_file_stat = ext2_file_stat,
499 .wr_fts_entry = ext2_fts_entry,
500 .wr_cpio_file = ext2_cpio_file,