From 7d7c328016aca3e14aee6268a2e7881d3440e226 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Wed, 27 Jun 2007 11:02:38 +1000 Subject: [PATCH] Resolve device paths in kernel and initrd locations. Instead of mounting in random locations, create a fixed mapping between device and mountpoint. This allows the parsers to refer to files outside the current partition. Signed-off-by: Jeremy Kerr --- devices/kboot-parser.c | 9 +-- devices/parser-test.c | 43 +++++++++-- devices/parser.c | 4 +- devices/parser.h | 5 +- devices/udev-helper.c | 107 ++++++++++++++++++++++------ devices/yaboot-parser.c | 20 ++---- petitboot-paths.h | 2 +- utils/ps3-kboot-0.1-petitboot.patch | 4 +- 8 files changed, 144 insertions(+), 50 deletions(-) diff --git a/devices/kboot-parser.c b/devices/kboot-parser.c index d449ef6..b70d5fc 100644 --- a/devices/kboot-parser.c +++ b/devices/kboot-parser.c @@ -138,13 +138,13 @@ static int parse_option(struct boot_option *opt, char *config) /* if there's no space, it's only a kernel image with no params */ if (!pos) { - opt->boot_image_file = join_paths(mountpoint, config); + opt->boot_image_file = resolve_path(config, mountpoint); opt->description = strdup(config); return 1; } *pos = 0; - opt->boot_image_file = join_paths(mountpoint, config); + opt->boot_image_file = resolve_path(config, mountpoint); cmdline = malloc(buf_size); *cmdline = 0; @@ -179,7 +179,7 @@ static int parse_option(struct boot_option *opt, char *config) free(cmdline); cmdline = tmp; - opt->initrd_file = join_paths(mountpoint, initrd); + opt->initrd_file = resolve_path(initrd, mountpoint); } if (root) { @@ -197,7 +197,8 @@ static int parse_option(struct boot_option *opt, char *config) pb_log("kboot cmdline: %s\n", cmdline); opt->boot_args = cmdline; - asprintf(&opt->description, "%s %s", config, cmdline); + asprintf(&opt->description, "%s %s", + config, opt->boot_args); return 1; } diff --git a/devices/parser-test.c b/devices/parser-test.c index 4b134d0..9622d4d 100644 --- a/devices/parser-test.c +++ b/devices/parser-test.c @@ -1,8 +1,10 @@ +#define _GNU_SOURCE #include #include #include #include +#include #include "parser.h" @@ -16,10 +18,10 @@ void pb_log(const char *fmt, ...) } -int mount_device(const char *dev_path, char *mount_path) +int mount_device(const char *dev_path) { pb_log("attempt to mount device (%s) not supported\n", dev_path); - return -1; + return 0; } int add_device(const struct device *dev) @@ -48,16 +50,49 @@ enum generic_icon_type guess_device_type(void) return ICON_TYPE_UNKNOWN; } +static char *mountpoint; + +const char *mountpoint_for_device(const char *dev_path) +{ + char *tmp, *dev; + dev = strrchr(dev_path, '/'); + if (dev) + dev_path = dev + 1; + asprintf(&tmp, "%s/%s", mountpoint, dev_path); + return tmp; +} + +char *resolve_path(const char *path, const char *default_mountpoint) +{ + char *sep, *ret; + const char *devpath; + + sep = strchr(path, ':'); + if (!sep) { + devpath = default_mountpoint; + asprintf(&ret, "%s/%s", devpath, path); + } else { + char *tmp = strndup(path, sep - path); + devpath = mountpoint_for_device(path); + asprintf(&ret, "%s/%s", devpath, sep + 1); + free(tmp); + } + + return ret; +} + int main(int argc, char **argv) { - const char *dev = "/dev/null"; + const char *dev = "sda1"; if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); return EXIT_FAILURE; } - iterate_parsers(dev, argv[1]); + mountpoint = argv[1]; + + iterate_parsers(dev, mountpoint); return EXIT_SUCCESS; } diff --git a/devices/parser.c b/devices/parser.c index d1cdb43..00f3659 100644 --- a/devices/parser.c +++ b/devices/parser.c @@ -21,12 +21,12 @@ void iterate_parsers(const char *devpath, const char *mountpoint) { int i; - pb_log("trying parsers for %s@%s\n", devpath, mountpoint); + pb_log("trying parsers for %s\n", devpath); for (i = 0; parsers[i]; i++) { pb_log("\ttrying parser '%s'\n", parsers[i]->name); /* just use a dummy device path for now */ - if (parsers[i]->parse(devpath, mountpoint)) + if (parsers[i]->parse(devpath, mountpoint_for_device(devpath))) return; } pb_log("\tno boot_options found\n"); diff --git a/devices/parser.h b/devices/parser.h index 2034cf1..840ca1b 100644 --- a/devices/parser.h +++ b/devices/parser.h @@ -35,7 +35,10 @@ const char *generic_icon_file(enum generic_icon_type type); /* functions provided by udev-helper or the test wrapper */ void pb_log(const char *fmt, ...); -int mount_device(const char *dev_path, char *mount_path); +int mount_device(const char *dev_path); + +char *resolve_path(const char *path, const char *default_mountpoint); +const char *mountpoint_for_device(const char *dev_path); enum generic_icon_type guess_device_type(void); diff --git a/devices/udev-helper.c b/devices/udev-helper.c index 4ec054b..661fb08 100644 --- a/devices/udev-helper.c +++ b/devices/udev-helper.c @@ -1,4 +1,6 @@ +#define _GNU_SOURCE + #include #include #include @@ -181,28 +183,92 @@ int connect_to_socket() #endif } -int mount_device(const char *dev_path, char *mount_path) +struct device_map { + char *dev, *mnt; +}; + +#define DEVICE_MAP_SIZE 32 +static struct device_map device_map[DEVICE_MAP_SIZE]; + +const char *mountpoint_for_device(const char *dev_path) { - char *dir; + int i; const char *basename; - int pid, status, rc = -1; - basename = strrchr(dev_path, '/'); - if (basename) - basename++; - else + /* shorten '/dev/foo' to 'foo' */ + basename = strrchr(dev_path, '/'); + if (basename) + basename++; + else basename = dev_path; - - /* create a unique mountpoint */ - dir = malloc(strlen(TMP_DIR) + 13 + strlen(basename)); - sprintf(dir, "%s/mnt-%s-XXXXXX", TMP_DIR, basename); - - if (!mkdtemp(dir)) { - pb_log("failed to create temporary directory in %s: %s", - TMP_DIR, strerror(errno)); - goto out; + + /* check existing entries in the map */ + for (i = 0; (i < DEVICE_MAP_SIZE) && device_map[i].dev; i++) + if (!strcmp(device_map[i].dev, basename)) + return device_map[i].mnt; + + if (i == DEVICE_MAP_SIZE) + return NULL; + + device_map[i].dev = strdup(dev_path); + asprintf(&device_map[i].mnt, "%s/%s", TMP_DIR, basename); + return device_map[i].mnt; +} + +/** + * Resolve a path given in a config file, to a path in the local filesystem. + * Paths may be of the form: + * device:path (eg /dev/sda:/boot/vmlinux) + * + * or just a path: + * /boot/vmlinux + * - in this case, the default mountpoint is used. + * + * Returns a newly-allocated string containing a full path to the file in path + */ +char *resolve_path(const char *path, const char *default_mountpoint) +{ + char *ret; + const char *devpath, *sep; + + sep = strchr(path, ':'); + if (!sep) { + devpath = default_mountpoint; + asprintf(&ret, "%s/%s", devpath, path); + } else { + /* copy just the device name into tmp */ + char *dev = strndup(path, sep - path); + devpath = mountpoint_for_device(dev); + asprintf(&ret, "%s/%s", devpath, sep + 1); + free(dev); } + return ret; +} + +int mount_device(const char *dev_path) +{ + const char *dir; + int pid, status, rc = -1; + struct stat statbuf; + + dir = mountpoint_for_device(dev_path); + + if (stat(dir, &statbuf)) { + if (mkdir(dir, 0755)) { + pb_log("couldn't create directory %s: %s\n", + dir, strerror(errno)); + goto out; + } + } else { + if (!S_ISDIR(statbuf.st_mode)) { + pb_log("mountpoint %s exists, " + "but isn't a directory\n", dir); + goto out; + } + } + + pid = fork(); if (pid == -1) { pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno)); @@ -220,13 +286,10 @@ int mount_device(const char *dev_path, char *mount_path) goto out; } - if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { - strcpy(mount_path, dir); + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) rc = 0; - } out: - free(dir); return rc; } @@ -348,9 +411,9 @@ static int is_ignored_device(const char *devname) static int found_new_device(const char *dev_path) { - char mountpoint[PATH_MAX]; + const char *mountpoint = mountpoint_for_device(dev_path); - if (mount_device(dev_path, mountpoint)) { + if (mount_device(dev_path)) { pb_log("failed to mount %s\n", dev_path); return EXIT_FAILURE; } diff --git a/devices/yaboot-parser.c b/devices/yaboot-parser.c index 2324950..fc55047 100644 --- a/devices/yaboot-parser.c +++ b/devices/yaboot-parser.c @@ -15,7 +15,6 @@ static struct device *dev; static const char *mountpoint; -static char partition_mntpoint[PATH_MAX]; static char *defimage; char * @@ -103,13 +102,13 @@ void process_image(char *label) opt.name = label; cfgopt = cfg_get_strg(label, "image"); - opt.boot_image_file = join_paths(mountpoint, cfgopt); + opt.boot_image_file = resolve_path(cfgopt, mountpoint); if (cfgopt == defimage) pb_log("This one is default. What do we do about it?\n"); cfgopt = cfg_get_strg(label, "initrd"); if (cfgopt) - opt.initrd_file = join_paths(mountpoint, cfgopt); + opt.initrd_file = resolve_path(cfgopt, mountpoint); opt.boot_args = make_params(label, NULL); @@ -182,8 +181,8 @@ static int yaboot_parse(const char *devicepath, const char *_mountpoint) } dev->icon_file = strdup(generic_icon_file(guess_device_type())); - /* Mount the 'partition' which is what all the image filenames - are relative to */ + /* If we have a 'partiton=' directive, update the default mountpoint + * to use that instead of the current mountpoint */ tmpstr = cfg_get_strg(0, "partition"); if (tmpstr) { char *endp; @@ -203,16 +202,9 @@ static int yaboot_parse(const char *devicepath, const char *_mountpoint) /* and add our own... */ sprintf(endp+1, "%d", partnr); - /* FIXME: udev may not have created the device node - yet. And on removal, unmount_device() only unmounts - it once, while in fact it may be mounted twice. */ - if (mount_device(new_dev, partition_mntpoint)) { - pb_log("Error mounting image partition\n"); - return 0; - } - mountpoint = partition_mntpoint; + mountpoint = mountpoint_for_device(new_dev); dev->id = new_dev; - } + } } defimage = cfg_get_default(); diff --git a/petitboot-paths.h b/petitboot-paths.h index 114d0b2..f8c5964 100644 --- a/petitboot-paths.h +++ b/petitboot-paths.h @@ -10,7 +10,7 @@ #endif #ifndef TMP_DIR -#define TMP_DIR "/var/tmp" +#define TMP_DIR "/var/tmp/mnt/" #endif #define PBOOT_DEVICE_SOCKET "/var/tmp/petitboot-dev" diff --git a/utils/ps3-kboot-0.1-petitboot.patch b/utils/ps3-kboot-0.1-petitboot.patch index 651f548..a33fa24 100644 --- a/utils/ps3-kboot-0.1-petitboot.patch +++ b/utils/ps3-kboot-0.1-petitboot.patch @@ -50,7 +50,7 @@ More info at http://ozlabs.org/~jk/projects/petitboot/ + find root/ -name '*.a' -o -name '*.la' -exec rm {} \; mkdir -p root/etc root/tmp root/proc root/dev root/sys mkdir -p root/mnt/tmp root/mnt/root root/bin root/sbin -+ mkdir -p root/var/tmp ++ mkdir -p root/var/tmp/mnt + mkdir -p root/etc/udev/rules.d [ -e root/dev/console ] || mknod root/dev/console c 5 1 [ -e root/dev/null ] || mknod root/dev/null c 1 3 @@ -283,7 +283,7 @@ diff -urN ps3-kboot.orig/ps3-kboot-0.1/scripts/petitboot-init ps3-kboot/ps3-kboo + /sbin/petitboot -u + + # clean up after petitboot -+ for dir in /var/tmp/mnt-*; ++ for dir in /var/tmp/mnt/*; + do + umount "$dir" + rmdir "$dir" -- 2.39.2