-/* Write the server config files */
-int write_local_files(struct mkfs_opts *mop)
-{
- char mntpt[] = "/tmp/mntXXXXXX";
- char filepnm[128];
- char *dev;
- FILE *filep;
- int ret = 0;
-
- /* Mount this device temporarily in order to write these files */
- if (!mkdtemp(mntpt)) {
- fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
- progname, mntpt, strerror(errno));
- return errno;
- }
-
- dev = mop->mo_device;
- if (mop->mo_flags & MO_IS_LOOP)
- dev = mop->mo_loopdev;
-
- ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0, NULL);
- if (ret) {
- fprintf(stderr, "%s: Unable to mount %s: %s\n",
- progname, dev, strerror(errno));
- ret = errno;
- if (errno == ENODEV) {
- fprintf(stderr, "Is the %s module available?\n",
- MT_STR(&mop->mo_ldd));
- }
- goto out_rmdir;
- }
-
- /* Set up initial directories */
- sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
- ret = mkdir(filepnm, 0777);
- if ((ret != 0) && (errno != EEXIST)) {
- fprintf(stderr, "%s: Can't make configs dir %s (%s)\n",
- progname, filepnm, strerror(errno));
- goto out_umnt;
- } else if (errno == EEXIST) {
- ret = 0;
- }
-
- sprintf(filepnm, "%s/%s", mntpt, "ROOT");
- ret = mkdir(filepnm, 0777);
- if ((ret != 0) && (errno != EEXIST)) {
- fprintf(stderr, "%s: Can't make ROOT dir %s (%s)\n",
- progname, filepnm, strerror(errno));
- goto out_umnt;
- } else if (errno == EEXIST) {
- ret = 0;
- }
-
- /* Save the persistent mount data into a file. Lustre must pre-read
- this file to get the real mount options. */
- vprint("Writing %s\n", MOUNT_DATA_FILE);
- sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
- filep = fopen(filepnm, "w");
- if (!filep) {
- fprintf(stderr, "%s: Unable to create %s file: %s\n",
- progname, filepnm, strerror(errno));
- goto out_umnt;
- }
- fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
- fclose(filep);
-
- /* COMPAT_146 */
-#ifdef TUNEFS
- /* Check for upgrade */
- if ((mop->mo_ldd.ldd_flags & (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS))
- == (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) {
- char cmd[128];
- char *term;
- int cmdsz = sizeof(cmd);
- vprint("Copying old logs\n");
-
- /* Copy the old client log to fsname-client */
- sprintf(filepnm, "%s/%s/%s-client",
- mntpt, MOUNT_CONFIGS_DIR, mop->mo_ldd.ldd_fsname);
- snprintf(cmd, cmdsz, "cp %s/%s/client %s", mntpt, MDT_LOGS_DIR,
- filepnm);
- ret = run_command(cmd, cmdsz);
- if (ret) {
- fprintf(stderr, "%s: Can't copy 1.4 config %s/client "
- "(%d)\n", progname, MDT_LOGS_DIR, ret);
- fprintf(stderr, "mount -t ldiskfs %s somewhere, "
- "find the client log for fs %s and "
- "copy it manually into %s/%s-client, "
- "then umount.\n",
- mop->mo_device,
- mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
- mop->mo_ldd.ldd_fsname);
- goto out_umnt;
- }
-
- /* We need to use the old mdt log because otherwise mdt won't
- have complete lov if old clients connect before all
- servers upgrade. */
- /* Copy the old mdt log to fsname-MDT0000 (get old
- name from mdt_UUID) */
- ret = 1;
- strscpy(filepnm, (char *)mop->mo_ldd.ldd_uuid, sizeof(filepnm));
- term = strstr(filepnm, "_UUID");
- if (term) {
- *term = '\0';
- snprintf(cmd, cmdsz, "cp %s/%s/%s %s/%s/%s",
- mntpt, MDT_LOGS_DIR, filepnm,
- mntpt, MOUNT_CONFIGS_DIR,
- mop->mo_ldd.ldd_svname);
- ret = run_command(cmd, cmdsz);
- }
- if (ret) {
- fprintf(stderr, "%s: Can't copy 1.4 config %s/%s "
- "(%d)\n", progname, MDT_LOGS_DIR, filepnm, ret);
- fprintf(stderr, "mount -t ext3 %s somewhere, "
- "find the MDT log for fs %s and "
- "copy it manually into %s/%s, "
- "then umount.\n",
- mop->mo_device,
- mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
- mop->mo_ldd.ldd_svname);
- goto out_umnt;
- }
- }
-#endif
- /* end COMPAT_146 */
-
-
-out_umnt:
- umount(mntpt);
-out_rmdir:
- rmdir(mntpt);
- return ret;
-}
-
-int read_local_files(struct mkfs_opts *mop)
-{
- char tmpdir[] = "/tmp/dirXXXXXX";
- char cmd[PATH_MAX];
- char filepnm[128];
- char *dev;
- FILE *filep;
- int ret = 0;
- int cmdsz = sizeof(cmd);
-
- /* Make a temporary directory to hold Lustre data files. */
- if (!mkdtemp(tmpdir)) {
- fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
- progname, tmpdir, strerror(errno));
- return errno;
- }
-
- dev = mop->mo_device;
-
- /* Construct debugfs command line. */
- snprintf(cmd, cmdsz, "debugfs -c -R 'dump /%s %s/mountdata' %s",
- MOUNT_DATA_FILE, tmpdir, dev);
-
- ret = run_command(cmd, cmdsz);
- if (ret)
- verrprint("%s: Unable to dump %s dir (%d)\n",
- progname, MOUNT_CONFIGS_DIR, ret);
-
- sprintf(filepnm, "%s/mountdata", tmpdir);
- filep = fopen(filepnm, "r");
- if (filep) {
- vprint("Reading %s\n", MOUNT_DATA_FILE);
- fread(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
- } else {
- /* COMPAT_146 */
- /* Try to read pre-1.6 config from last_rcvd */
- struct lr_server_data lsd;
- verrprint("%s: Unable to read %s (%s).\n",
- progname, filepnm, strerror(errno));
-
- verrprint("Trying last_rcvd\n");
- sprintf(filepnm, "%s/%s", tmpdir, LAST_RCVD);
-
- /* Construct debugfs command line. */
- snprintf(cmd, cmdsz, "debugfs -c -R 'dump /%s %s' %s",
- LAST_RCVD, filepnm, dev);
-
- ret = run_command(cmd, cmdsz);
- if (ret) {
- fprintf(stderr, "%s: Unable to dump %s file (%d)\n",
- progname, LAST_RCVD, ret);
- goto out_rmdir;
- }
-
- filep = fopen(filepnm, "r");
- if (!filep) {
- fprintf(stderr, "%s: Unable to open %s: %s\n",
- progname, filepnm, strerror(errno));
- ret = errno;
- verrprint("Contents of %s:\n", tmpdir);
- verbose+=2;
- snprintf(cmd, cmdsz, "ls -l %s/", tmpdir);
- run_command(cmd, cmdsz);
- verrprint("Contents of disk:\n");
- snprintf(cmd, cmdsz, "debugfs -c -R 'ls -l /' %s", dev);
- run_command(cmd, cmdsz);
-
- goto out_rmdir;
- }
- vprint("Reading %s\n", LAST_RCVD);
- ret = fread(&lsd, 1, sizeof(lsd), filep);
- if (ret < sizeof(lsd)) {
- fprintf(stderr, "%s: Short read (%d of %d)\n",
- progname, ret, (int)sizeof(lsd));
- ret = ferror(filep);
- if (ret)
- goto out_close;
- }
- vprint("Feature compat=%x, incompat=%x\n",
- lsd.lsd_feature_compat, lsd.lsd_feature_incompat);
-
- if ((lsd.lsd_feature_compat & OBD_COMPAT_OST) ||
- (lsd.lsd_feature_incompat & OBD_INCOMPAT_OST)) {
- mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
- mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
- } else if ((lsd.lsd_feature_compat & OBD_COMPAT_MDT) ||
- (lsd.lsd_feature_incompat & OBD_INCOMPAT_MDT)) {
- /* We must co-locate so mgs can see old logs.
- If user doesn't want this, they can copy the old
- logs manually and re-tunefs. */
- mop->mo_ldd.ldd_flags =
- LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_MGS;
- mop->mo_ldd.ldd_svindex = lsd.lsd_mdt_index;
- } else {
- /* If neither is set, we're pre-1.4.6, make a guess. */
- /* Construct debugfs command line. */
- snprintf(cmd, cmdsz, "debugfs -c -R 'rdump /%s %s' %s",
- MDT_LOGS_DIR, tmpdir, dev);
- run_command(cmd, cmdsz);
-
- sprintf(filepnm, "%s/%s", tmpdir, MDT_LOGS_DIR);
- if (lsd.lsd_ost_index > 0) {
- mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
- mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
- } else {
- /* If there's a LOGS dir, it's an MDT */
- if ((ret = access(filepnm, F_OK)) == 0) {
- mop->mo_ldd.ldd_flags =
- LDD_F_SV_TYPE_MDT |
- LDD_F_SV_TYPE_MGS;
- /* Old MDT's are always index 0
- (pre CMD) */
- mop->mo_ldd.ldd_svindex = 0;
- } else {
- /* The index may not be correct */
- mop->mo_ldd.ldd_flags =
- LDD_F_SV_TYPE_OST | LDD_F_NEED_INDEX;
- verrprint("OST with unknown index\n");
- }
- }
- }
-
- ret = 0;
- memcpy(mop->mo_ldd.ldd_uuid, lsd.lsd_uuid,
- sizeof(mop->mo_ldd.ldd_uuid));
- mop->mo_ldd.ldd_flags |= LDD_F_UPGRADE14;
- }
- /* end COMPAT_146 */
-out_close:
- fclose(filep);
-
-out_rmdir:
- snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
- run_command(cmd, cmdsz);
- if (ret)
- verrprint("Failed to read old data (%d)\n", ret);
- return ret;
-}
-
-