From 1364e6e30d3b579de8b410c219d305ec5c7c21ef Mon Sep 17 00:00:00 2001 From: William Manley Date: Mon, 18 Jul 2016 14:42:40 +0100 Subject: [PATCH] ostree-prepare-root: Refactor code to resolve deploy_path I'll reuse this for a new ostree-init. Closes: #403 Approved by: cgwalters --- src/switchroot/ostree-prepare-root.c | 61 ++++++++++++++++------------ 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/src/switchroot/ostree-prepare-root.c b/src/switchroot/ostree-prepare-root.c index 375867b1..df3e3219 100644 --- a/src/switchroot/ostree-prepare-root.c +++ b/src/switchroot/ostree-prepare-root.c @@ -108,25 +108,12 @@ touch_run_ostree (void) (void) close (fd); } -int -main(int argc, char *argv[]) +static char* +resolve_deploy_path (const char * root_mountpoint) { - const char *root_mountpoint = NULL; - char *ostree_target = NULL; - char *deploy_path = NULL; - char srcpath[PATH_MAX]; char destpath[PATH_MAX]; - char newroot[PATH_MAX]; struct stat stbuf; - int orig_cwd_dfd; - - if (argc < 2) - { - fprintf (stderr, "usage: ostree-prepare-root SYSROOT\n"); - exit (EXIT_FAILURE); - } - - root_mountpoint = argv[1]; + char *ostree_target, *deploy_path; ostree_target = parse_ostree_cmdline (); if (!ostree_target) @@ -135,16 +122,6 @@ main(int argc, char *argv[]) exit (EXIT_FAILURE); } - /* Create a temporary target for our mounts in the initramfs; this will - * be moved to the new system root below. - */ - snprintf (newroot, sizeof(newroot), "%s.tmp", root_mountpoint); - if (mkdir (newroot, 0755) < 0) - { - perrorv ("Couldn't create temporary sysroot '%s': ", newroot); - exit (EXIT_FAILURE); - } - snprintf (destpath, sizeof(destpath), "%s/%s", root_mountpoint, ostree_target); printf ("Examining %s\n", destpath); if (lstat (destpath, &stbuf) < 0) @@ -164,6 +141,38 @@ main(int argc, char *argv[]) exit (EXIT_FAILURE); } printf ("Resolved OSTree target to: %s\n", deploy_path); + return deploy_path; +} + +int +main(int argc, char *argv[]) +{ + const char *root_mountpoint = NULL; + char *deploy_path = NULL; + char srcpath[PATH_MAX]; + char destpath[PATH_MAX]; + char newroot[PATH_MAX]; + struct stat stbuf; + int orig_cwd_dfd; + + if (argc < 2) + { + fprintf (stderr, "usage: ostree-prepare-root SYSROOT\n"); + exit (EXIT_FAILURE); + } + + root_mountpoint = argv[1]; + deploy_path = resolve_deploy_path (root_mountpoint); + + /* Create a temporary target for our mounts in the initramfs; this will + * be moved to the new system root below. + */ + snprintf (newroot, sizeof(newroot), "%s.tmp", root_mountpoint); + if (mkdir (newroot, 0755) < 0) + { + perrorv ("Couldn't create temporary sysroot '%s': ", newroot); + exit (EXIT_FAILURE); + } /* Work-around for a kernel bug: for some reason the kernel * refuses switching root if any file systems are mounted