lvm代码分析(六)——pvcreate

来源:互联网 发布:4glte是什么网络制式 编辑:程序博客网 时间:2024/06/05 08:03
 对于输入pvcreate /dev/sda0这个命令,如何通过lvm进入,然后经过转换到调用pvcreate的过程,前面算基本理清,更多的细节需要在日后多完善。pvcreate函数的代码如下:
  1. int pvcreate(struct cmd_context *cmd, int argc, char **argv)
  2. {
  3.     int i, r;
  4.     int ret = ECMD_PROCESSED;
  5.     struct pvcreate_params pp;
  6.     if (!argc) {
  7.         log_error("Please enter a physical volume path");
  8.         return EINVALID_CMD_LINE;
  9.     }
  10.     if (arg_count(cmd, restorefile_ARG) && !arg_count(cmd, uuidstr_ARG)) {
  11.         log_error("--uuid is required with --restorefile");
  12.         return EINVALID_CMD_LINE;
  13.     }
  14.     if (arg_count(cmd, uuidstr_ARG) && argc != 1) {
  15.         log_error("Can only set uuid on one volume at once");
  16.         return EINVALID_CMD_LINE;
  17.     }
  18.     if (arg_count(cmd, yes_ARG) && !arg_count(cmd, force_ARG)) {
  19.         log_error("Option y can only be given with option f");
  20.         return EINVALID_CMD_LINE;
  21.     }
  22.     if (arg_int_value(cmd, labelsector_ARG, 0) >= LABEL_SCAN_SECTORS) {
  23.         log_error("labelsector must be less than %lu",
  24.               LABEL_SCAN_SECTORS);
  25.         return EINVALID_CMD_LINE;
  26.     }
  27.     if (!(cmd->fmt->features & FMT_MDAS) &&
  28.         (arg_count(cmd, metadatacopies_ARG) ||
  29.          arg_count(cmd, metadatasize_ARG))) {
  30.         log_error("Metadata parameters only apply to text format");
  31.         return EINVALID_CMD_LINE;
  32.     }
  33.     if (arg_count(cmd, metadatacopies_ARG) &&
  34.         arg_int_value(cmd, metadatacopies_ARG, -1) > 2) {
  35.         log_error("Metadatacopies may only be 0, 1 or 2");
  36.         return EINVALID_CMD_LINE;
  37.     }
  38.     if (arg_count(cmd, zero_ARG))
  39.         pp.zero = strcmp(arg_str_value(cmd, zero_ARG, "y"), "n");
  40.     else if (arg_count(cmd, restorefile_ARG) || arg_count(cmd, uuidstr_ARG))
  41.         pp.zero = 0;
  42.     else
  43.         pp.zero = 1;
  44.     for (i = 0; i < argc; i++) {
  45.         r = pvcreate_single(cmd, argv[i], &pp);
  46.         if (r > ret)
  47.             ret = r;
  48.         if (sigint_caught())
  49.             return ret;
  50.     }
  51.     return ret;
  52. }
从开始到51行止,都是通过命令携带的参数进行判断处理,先不拘小节地跳过吧,等需要用到时在回来。53行开始的for循环是对每个设备都调用pvcreate_single函数分别对每个设备进行操作。

在pvcreate_single中,又看到一个郁闷的声明:
struct id id
和前面又有arg的结构,又有arg的宏定义一样,搞得我的se不能正确跳转。ft。

继续看下去:
  1. if (arg_count(cmd, uuidstr_ARG)) {
  2.         uuid = arg_str_value(cmd, uuidstr_ARG, "");
  3.         if (!id_read_format(&id, uuid))
  4.             return EINVALID_CMD_LINE;
  5.         if ((dev = device_from_pvid(cmd, &id)) &&
  6.             (dev != dev_cache_get(pv_name, cmd->filter))) {
  7.             log_error("uuid %s already in use on /"%s/"", uuid,
  8.                   dev_name(dev));
  9.             return ECMD_FAILED;
  10.         }
  11.         idp = &id;
  12.     }
此处应该是获取设备及判断设备是否已经呗使用,设备id赋予id变量,同时idp指向id。

  1.     if (arg_count(cmd, restorefile_ARG)) {
  2.         restorefile = arg_str_value(cmd, restorefile_ARG, "");
  3.         /* The uuid won't already exist */
  4.         init_partial(1);
  5.         if (!(vg = backup_read_vg(cmd, NULL, restorefile))) {
  6.             log_error("Unable to read volume group from %s",
  7.                   restorefile);
  8.             return ECMD_FAILED;
  9.         }
  10.         init_partial(0);
  11.         if (!(existing_pv = find_pv_in_vg_by_uuid(vg, idp))) {
  12.             log_error("Can't find uuid %s in backup file %s",
  13.                   uuid, restorefile);
  14.             return ECMD_FAILED;
  15.         }
  16.         pe_start = pv_pe_start(existing_pv);
  17.         extent_size = pv_pe_size(existing_pv);
  18.         extent_count = pv_pe_count(existing_pv);
  19.     }
接下来的这个代码快,应该是根据restorefile构建或者创建一个新的vg,然后获取已经存在的pv,获取pe_start,extent_size,extent_count。
阅读这种不连续的代码还真郁闷,要是能够debug看看传进参数是啥就好了。唉~


原创粉丝点击