+/* Add mgsnids from ldd params */
+static int add_mgsnids(struct mount_opts *mop, char *options,
+ const char *params)
+{
+ char *ptr = (char *)params;
+ char tmp, *sep;
+
+ while ((ptr = strstr(ptr, PARAM_MGSNODE)) != NULL) {
+ sep = strchr(ptr, ' ');
+ if (sep != NULL) {
+ tmp = *sep;
+ *sep = '\0';
+ }
+ append_option(options, ptr);
+ mop->mo_have_mgsnid++;
+ if (sep) {
+ *sep = tmp;
+ ptr = sep;
+ } else {
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static int parse_ldd(char *source, struct mount_opts *mop, char *options)
+{
+ struct lustre_disk_data *ldd = &mop->mo_ldd;
+ char *cur, *start;
+ int rc;
+
+ rc = osd_is_lustre(source, &ldd->ldd_mount_type);
+ if (rc == 0) {
+ fprintf(stderr, "%s: %s has not been formatted with mkfs.lustre"
+ " or the backend filesystem type is not supported by "
+ "this tool\n", progname, source);
+ return ENODEV;
+ }
+
+ rc = osd_read_ldd(source, ldd);
+ if (rc) {
+ fprintf(stderr, "%s: %s failed to read permanent mount"
+ " data: %s\n", progname, source, strerror(rc));
+ return rc;
+ }
+
+ if (ldd->ldd_flags & LDD_F_NEED_INDEX) {
+ fprintf(stderr, "%s: %s has no index assigned "
+ "(probably formatted with old mkfs)\n",
+ progname, source);
+ return EINVAL;
+ }
+
+ if (ldd->ldd_flags & LDD_F_UPGRADE14) {
+ fprintf(stderr, "%s: we cannot upgrade %s from this (very old) "
+ "Lustre version\n", progname, source);
+ return EINVAL;
+ }
+
+ /* Since we never rewrite ldd, ignore temp flags */
+ ldd->ldd_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE | LDD_F_WRITECONF);
+
+ /* svname of the form lustre:OST1234 means never registered */
+ rc = strlen(ldd->ldd_svname);
+ if (ldd->ldd_svname[rc - 8] == ':') {
+ ldd->ldd_svname[rc - 8] = '-';
+ ldd->ldd_flags |= LDD_F_VIRGIN;
+ } else if (ldd->ldd_svname[rc - 8] == '=') {
+ ldd->ldd_svname[rc - 8] = '-';
+ ldd->ldd_flags |= LDD_F_WRITECONF;
+ }
+
+ /* backend osd type */
+ append_option(options, "osd=");
+ strcat(options, mt_type(ldd->ldd_mount_type));
+
+ append_option(options, ldd->ldd_mount_opts);
+
+ if (!mop->mo_have_mgsnid) {
+ /* Only use disk data if mount -o mgsnode=nid wasn't
+ * specified */
+ if (ldd->ldd_flags & LDD_F_SV_TYPE_MGS) {
+ append_option(options, "mgs");
+ mop->mo_have_mgsnid++;
+ } else {
+ add_mgsnids(mop, options, ldd->ldd_params);
+ }
+ }
+ /* Better have an mgsnid by now */
+ if (!mop->mo_have_mgsnid) {
+ fprintf(stderr, "%s: missing option mgsnode=<nid>\n",
+ progname);
+ return EINVAL;
+ }
+
+ if (ldd->ldd_flags & LDD_F_VIRGIN)
+ append_option(options, "virgin");
+ if (ldd->ldd_flags & LDD_F_WRITECONF)
+ append_option(options, "writeconf");
+ if (ldd->ldd_flags & LDD_F_NO_PRIMNODE)
+ append_option(options, "noprimnode");
+
+ /* prefix every lustre parameter with param= so that in-kernel
+ * mount can recognize them properly and send to MGS at registration */
+ start = ldd->ldd_params;
+ while (start && *start != '\0') {
+ while (*start == ' ') start++;
+ if (*start == '\0')
+ break;
+ cur = start;
+ start = strchr(cur, ' ');
+ if (start) {
+ *start = '\0';
+ start++;
+ }
+ append_option(options, "param=");
+ strcat(options, cur);
+ }
+
+ /* svname must be last option */
+ append_option(options, "svname=");
+ strcat(options, ldd->ldd_svname);
+
+ return 0;
+}
+
+static void set_defaults(struct mount_opts *mop)
+{
+ memset(mop, 0, sizeof(*mop));
+ mop->mo_usource = NULL;
+ mop->mo_source = NULL;
+ mop->mo_nomtab = 0;
+ mop->mo_fake = 0;
+ mop->mo_force = 0;
+ mop->mo_retry = 0;
+ mop->mo_have_mgsnid = 0;
+ mop->mo_md_stripe_cache_size = 16384;
+ mop->mo_orig_options = "";
+ mop->mo_nosvc = 0;
+}
+
+static int parse_opts(int argc, char *const argv[], struct mount_opts *mop)
+{
+ static struct option long_opt[] = {
+ {"fake", 0, 0, 'f'},
+ {"force", 0, 0, 1},
+ {"help", 0, 0, 'h'},
+ {"nomtab", 0, 0, 'n'},
+ {"options", 1, 0, 'o'},
+ {"verbose", 0, 0, 'v'},
+ {0, 0, 0, 0}
+ };
+ char real_path[PATH_MAX] = {'\0'};
+ FILE *f;
+ char path[256], name[256];
+ size_t sz;
+ char *ptr;
+ int opt, rc;
+
+ while ((opt = getopt_long(argc, argv, "fhno:v",
+ long_opt, NULL)) != EOF){
+ switch (opt) {
+ case 1:
+ ++mop->mo_force;
+ printf("force: %d\n", mop->mo_force);
+ break;
+ case 'f':
+ ++mop->mo_fake;
+ printf("fake: %d\n", mop->mo_fake);
+ break;
+ case 'h':
+ usage(stdout);
+ break;
+ case 'n':
+ ++mop->mo_nomtab;
+ printf("nomtab: %d\n", mop->mo_nomtab);
+ break;
+ case 'o':
+ mop->mo_orig_options = optarg;
+ break;
+ case 'v':
+ ++verbose;
+ break;
+ default:
+ fprintf(stderr, "%s: unknown option '%c'\n",
+ progname, opt);
+ usage(stderr);
+ break;
+ }
+ }
+
+ if (optind + 2 > argc) {
+ fprintf(stderr, "%s: too few arguments\n", progname);
+ usage(stderr);
+ }
+
+ mop->mo_usource = argv[optind];
+ if (!mop->mo_usource) {
+ usage(stderr);
+ }
+
+ /**
+ * Try to get the real path to the device, in case it is a
+ * symbolic link for instance
+ */
+ if (realpath(mop->mo_usource, real_path) != NULL) {
+ ptr = strrchr(real_path, '/');
+ if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
+ snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
+ if ((f = fopen(path, "r"))) {
+ /* read "<name>\n" from sysfs */
+ if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
+ name[sz - 1] = '\0';
+ snprintf(real_path, sizeof(real_path), "/dev/mapper/%s", name);
+ }
+ fclose(f);
+ }
+ }
+ mop->mo_usource = strdup(real_path);
+ }
+
+ ptr = strstr(mop->mo_usource, ":/");
+ if (ptr != NULL) {
+ mop->mo_source = convert_hostnames(mop->mo_usource);
+ if (!mop->mo_source)
+ usage(stderr);
+ } else {
+ mop->mo_source = strdup(mop->mo_usource);
+ }
+
+ if (realpath(argv[optind + 1], mop->mo_target) == NULL) {
+ rc = errno;
+ fprintf(stderr, "warning: %s: cannot resolve: %s\n",
+ argv[optind + 1], strerror(errno));
+ return rc;
+ }
+
+ return 0;
+}