lvm代码分析.doc_第1页
lvm代码分析.doc_第2页
lvm代码分析.doc_第3页
lvm代码分析.doc_第4页
lvm代码分析.doc_第5页
已阅读5页,还剩43页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

lvm代码分析其实挺不敢在csdn写blog的,特别是技术的东东,万一被高手路过,看到自己如此搓人在这造次,岂不是贻笑大方,不过发现这里写文章代码块很好用,用来做代码阅读的笔记挺好的。偷偷留痕,咔咔。在Linux安装完lvm2的工具集后,便可以使用pvcreate,lvcreate,pvscan等命令,其实这些命令都是lvm指出来的链接(害得偶曾经郁闷地找这些命令的main,!=.=)在lvm.c中,入口函数main很简单,调用lvm2_main函数。int main(int argc, char *argv) return lvm2_main(argc, argv, 0);分析int lvm2_main(int argc, char *argv, unsigned is_static) char *base; int ret, alias = 0; struct cmd_context *cmd;这里有个变态的结构cmd_context,发现Linux下很多结构都很变态,也许是自己底子太浅,加上以前都没有阅读过太多代码的缘故。/* FIXME Split into tool & library contexts */* command-instance-related variables needed by library */struct cmd_context struct dm_pool *libmem; /* For permanent config data */ struct dm_pool *mem; /* Transient: Cleared between each command */ const struct format_type *fmt; /* Current format to use by default */ struct format_type *fmt_backup; /* Format to use for backups */ struct list formats; /* Available formats */ struct list segtypes; /* Available segment types */ const char *hostname; const char *kernel_vsn; char *cmd_line; struct command *command; struct arg *args; char *argv; unsigned is_static; /* Static binary? */ unsigned is_long_lived; /* Optimises persistent_filter handling */ struct dev_filter *filter; int dump_filter; /* Dump filter when exiting? */ struct list config_files; int config_valid; struct config_tree *cft; struct config_tree *cft_override; struct config_info default_settings; struct config_info current_settings; struct archive_params *archive_params; struct backup_params *backup_params; /* List of defined tags */ struct list tags; int hosttags; char sys_dirPATH_MAX; char dev_dirPATH_MAX; char proc_dirPATH_MAX;一个变态的结构引出了几个更变态的结构,已然疯掉,libmen和men是通过dm的lib函数生成的内存池,format_type结构中的format_handle指向相应操作的函数指针,不同的format_type对应的操作函数不一样。struct format_type struct list list; struct cmd_context *cmd; struct format_handler *ops; struct labeller *labeller; const char *name; const char *alias; const char *orphan_vg_name; uint32_t features; void *library; void *private;还有一个command的结构,定义如下:struct command const char *name; const char *help; int min_args; int max_args; command_fn fn;继续分析lvm2_main: _close_stray_fds(); base = last_path_component(argv0); while (*base = /) base+; if (strcmp(base, lvm) & strcmp(base, lvm.static) & strcmp(base, initrd-lvm) alias = 1; if (is_static & strcmp(base, lvm.static) & path_exists(LVM_SHARED_PATH) & !getenv(LVM_DID_EXEC) setenv(LVM_DID_EXEC, base, 1); execvp(LVM_SHARED_PATH, argv); unsetenv(LVM_DID_EXEC); 第1行的函数_close_stray_fds(),没看懂,不过貌似和主干数据流没有多大的影响,暂且不管。希望以后多回头几次能读懂。咔咔。接下来的last_path_component(argv0)是将输入的命令进行一些处理,最后得到命令名,下面的两个if是判断做一些相关处理。就pvcreate /dev/sda0而言,两个if都没有执行。继续往下: if (!(cmd = init_lvm(is_static) return -1;此处调用init_lvm(is_static),is_static=0struct cmd_context *init_lvm(unsigned is_static) struct cmd_context *cmd; _cmdline.the_args = &_the_args0; if (!(cmd = create_toolcontext(_cmdline.the_args, is_static, 0) return_NULL; _init_rand(); _apply_settings(cmd); return cmd;此处的_cmdline是一个静态变量static struct cmdline_context _cmdline;而这个_the_arg0的赋值也着实让我见识到啥是c语言高级编程,把我最后一点自信给毁了。/* * Table of valid switches */static struct arg _the_argsARG_COUNT + 1 = #define arg(a, b, c, d, e) b, , - c, d, e, 0, NULL, 0, 0, INT64_C(0), UINT64_C(0), SIGN_NONE, PERCENT_NONE, NULL,#include args.h#undef arg;在代码中,我始终没找到ARG_COUNT这个变量的定义,可能是se的问题吧。arg.h:/* * Put all long args that dont have a * corresponding short option first . */* *INDENT-OFF* */arg(version_ARG, 0, version, NULL, 0)arg(quiet_ARG, 0, quiet, NULL, 0)arg(physicalvolumesize_ARG, 0, setphysicalvolumesize, size_mb_arg, 0)arg(ignorelockingfailure_ARG, 0, ignorelockingfailure, NULL, 0)arg(nolocking_ARG, 0, nolocking, NULL, 0)arg(metadatacopies_ARG, 0, metadatacopies, int_arg, 0)arg(metadatasize_ARG, 0, metadatasize, size_mb_arg, 0)arg(restorefile_ARG, 0, restorefile, string_arg, 0)arg(labelsector_ARG, 0, labelsector, int_arg, 0)arg(driverloaded_ARG, 0, driverloaded, yes_no_arg, 0)arg(aligned_ARG, 0, aligned, NULL, 0)arg(unbuffered_ARG, 0, unbuffered, NULL, 0)arg(noheadings_ARG, 0, noheadings, NULL, 0)arg(segments_ARG, 0, segments, NULL, 0)arg(units_ARG, 0, units, string_arg, 0)arg(nosuffix_ARG, 0, nosuffix, NULL, 0)arg(removemissing_ARG, 0, removemissing, NULL, 0)arg(abort_ARG, 0, abort, NULL, 0)arg(addtag_ARG, 0, addtag, tag_arg, 0)arg(deltag_ARG, 0, deltag, tag_arg, 0)arg(refresh_ARG, 0, refresh, NULL, 0)arg(mknodes_ARG, 0, mknodes, NULL, 0)arg(minor_ARG, 0, minor, minor_arg, 0)arg(type_ARG, 0, type, segtype_arg, 0)arg(alloc_ARG, 0, alloc, alloc_arg, 0)arg(separator_ARG, 0, separator, string_arg, 0)arg(mirrorsonly_ARG, 0, mirrorsonly, NULL, 0)arg(nosync_ARG, 0, nosync, NULL, 0)arg(resync_ARG, 0, resync, NULL, 0)arg(corelog_ARG, 0, corelog, NULL, 0)arg(mirrorlog_ARG, 0, mirrorlog, string_arg, 0)arg(monitor_ARG, 0, monitor, yes_no_arg, 0)arg(config_ARG, 0, config, string_arg, 0)arg(trustcache_ARG, 0, trustcache, NULL, 0)arg(ignoremonitoring_ARG, 0, ignoremonitoring, NULL, 0)arg(nameprefixes_ARG, 0, nameprefixes, NULL, 0)arg(unquoted_ARG, 0, unquoted, NULL, 0)arg(rows_ARG, 0, rows, NULL, 0)/* Allow some variations */arg(resizable_ARG, 0, resizable, yes_no_arg, 0)arg(allocation_ARG, 0, allocation, yes_no_arg, 0)/* * . and now the short args. */arg(available_ARG, a, available, yes_no_excl_arg, 0)arg(all_ARG, a, all, NULL, 0)arg(autobackup_ARG, A, autobackup, yes_no_arg, 0)arg(activevolumegroups_ARG, A, activevolumegroups, NULL, 0)arg(background_ARG, b, background, NULL, 0)arg(blockdevice_ARG, b, blockdevice, NULL, 0)arg(chunksize_ARG, c, chunksize, size_kb_arg, 0)arg(clustered_ARG, c, clustered, yes_no_arg, 0)arg(colon_ARG, c, colon, NULL, 0)arg(columns_ARG, C, columns, NULL, 0)arg(contiguous_ARG, C, contiguous, yes_no_arg, 0)arg(debug_ARG, d, debug, NULL, ARG_REPEATABLE)arg(disk_ARG, D, disk, NULL, 0)arg(exported_ARG, e, exported, NULL, 0)arg(physicalextent_ARG, E, physicalextent, NULL, 0)arg(file_ARG, f, file, string_arg, 0)arg(force_ARG, f, force, NULL, ARG_REPEATABLE)arg(full_ARG, f, full, NULL, 0)arg(help_ARG, h, help, NULL, 0)arg(help2_ARG, ?, , NULL, 0)arg(stripesize_ARG, I, stripesize, size_kb_arg, 0)arg(stripes_ARG, i, stripes, int_arg, 0)arg(interval_ARG, i, interval, int_arg, 0)arg(iop_version_ARG, i, iop_version, NULL, 0)arg(logicalvolume_ARG, l, logicalvolume, int_arg, 0)arg(maxlogicalvolumes_ARG, l, maxlogicalvolumes, int_arg, 0)arg(extents_ARG, l, extents, int_arg_with_sign_and_percent, 0)arg(lvmpartition_ARG, l, lvmpartition, NULL, 0)arg(list_ARG, l, list, NULL, 0)arg(size_ARG, L, size, size_mb_arg, 0)arg(logicalextent_ARG, L, logicalextent, int_arg_with_sign, 0)arg(persistent_ARG, M, persistent, yes_no_arg, 0)arg(major_ARG, j, major, major_arg, 0)arg(mirrors_ARG, m, mirrors, int_arg_with_sign, 0)arg(metadatatype_ARG, M, metadatatype, metadatatype_arg, 0)arg(maps_ARG, m, maps, NULL, 0)arg(name_ARG, n, name, string_arg, 0)arg(oldpath_ARG, n, oldpath, NULL, 0)arg(nofsck_ARG, n, nofsck, NULL, 0)arg(novolumegroup_ARG, n, novolumegroup, NULL, 0)arg(options_ARG, o, options, string_arg, 0)arg(sort_ARG, O, sort, string_arg, 0)arg(permission_ARG, p, permission, permission_arg, 0)arg(maxphysicalvolumes_ARG, p, maxphysicalvolumes, int_arg, 0)arg(partial_ARG, P, partial, NULL, 0)arg(physicalvolume_ARG, P, physicalvolume, NULL, 0)arg(readahead_ARG, r, readahead, readahead_arg, 0)arg(resizefs_ARG, r, resizefs, NULL, 0)arg(reset_ARG, R, reset, NULL, 0)arg(regionsize_ARG, R, regionsize, size_mb_arg, 0)arg(physicalextentsize_ARG, s, physicalextentsize, size_mb_arg, 0)arg(stdin_ARG, s, stdin, NULL, 0)arg(snapshot_ARG, s, snapshot, NULL, 0)arg(short_ARG, s, short, NULL, 0)arg(test_ARG, t, test, NULL, 0)arg(uuid_ARG, u, uuid, NULL, 0)arg(uuidstr_ARG, u, uuid, string_arg, 0)arg(uuidlist_ARG, U, uuidlist, NULL, 0)arg(verbose_ARG, v, verbose, NULL, ARG_REPEATABLE)arg(volumegroup_ARG, V, volumegroup, NULL, 0)arg(allocatable_ARG, x, allocatable, yes_no_arg, 0)arg(resizeable_ARG, x, resizeable, yes_no_arg, 0)arg(yes_ARG, y, yes, NULL, 0)arg(zero_ARG, Z, zero, yes_no_arg, 0)/* this should always be last */arg(ARG_COUNT, -, , NULL, 0)/* *INDENT-ON* */这个数组的初始化太有个性了。接着讲init_lvm:struct cmd_context *init_lvm(unsigned is_static) struct cmd_context *cmd; _cmdline.the_args = &_the_args0; if (!(cmd = create_toolcontext(_cmdline.the_args, is_static, 0) return_NULL; _init_rand(); _apply_settings(cmd); return cmd;将_the_args赋予_cmdline.the_args后,调用create_toolcontext,在该函数中,前面的语句:#ifdef M_MMAP_MAX mallopt(M_MMAP_MAX, 0);#endif if (!setlocale(LC_ALL, ) log_very_verbose(setlocale failed);#ifdef INTL_PACKAGE bindtextdomain(INTL_PACKAGE, LOCALEDIR);#endif init_syslog(DEFAULT_LOG_FACILITY);暂且不管,看似一些log相关的东西,接下来的if (!(cmd = dm_malloc(sizeof(*cmd) log_error(Failed to allocate command context); return NULL; memset(cmd, 0, sizeof(*cmd); cmd-args = the_args; cmd-is_static = is_static; cmd-is_long_lived = is_long_lived; cmd-hosttags = 0; list_init(&cmd-formats); list_init(&cmd-segtypes); list_init(&cmd-tags); list_init(&cmd-config_files); strcpy(cmd-sys_dir, DEFAULT_SYS_DIR); if (!_get_env_vars(cmd) goto error; /* Create system directory if it doesnt already exist */ if (*cmd-sys_dir & !dm_create_dir(cmd-sys_dir) log_error(Failed to create LVM2 system dir for metadata backups, config files and internal cache.); log_error(Set environment variable LVM_SYSTEM_DIR to alternative location or empty string.); goto error; if (!(cmd-libmem = dm_pool_create(library, 4 * 1024) log_error(Library memory pool creation failed); goto error; if (!_init_lvm_conf(cmd) goto error; _init_logging(cmd); if (!_init_hostname(cmd) goto error; if (!_init_tags(cmd, cmd-cft) goto error; if (!_init_tag_configs(cmd) goto error; if (!_merge_config_files(cmd) goto error; if (!_process_config(cmd) goto error; if (!_init_dev_cache(cmd) goto error; if (!_init_filters(cmd, 1) goto error; if (!(cmd-mem = dm_pool_create(command, 4 * 1024) log_error(Command memory pool creation failed); goto error; memlock_init(cmd); if (!_init_formats(cmd) goto error; if (!init_lvmcache_orphans(cmd) goto error; if (!_init_segtypes(cmd) goto error; if (!_init_backup(cmd) goto error; cmd-default_settings.cache_vgmetadata = 1; cmd-current_settings = cmd-default_settings; cmd-config_valid = 1; return cmd; error: dm_free(cmd); return NULL;向dm申请cmd内存,然后对cmd进行初始化。init_lvm中的 _init_rand(); _apply_settings(cmd);先暂且不管吧。lvm返回后,在lvm2_main里执行: cmd-argv = argv; lvm_register_commands();lvm_register_commands()又是一个似曾相识的构架:void lvm_register_commands(void)#define xx(a, b, c, d.) _register_command(# a, a, b, c, # d, driverloaded_ARG, debug_ARG, help_ARG, help2_ARG, version_ARG, verbose_ARG, quiet_ARG, config_ARG, -1);#include commands.h#undef xx在commands.h中,xx了好多种命令:xx(dumpconfig, Dump active configuration, 0, dumpconfig t-f|-file filename n ConfigurationVariable.n, file_ARG)xx(formats, List available metadata formats, 0, formatsn)xx(help, Display help for commands, 0, help n)/*xx(lvactivate, Activate logical volume on given partition(s), lvactivate t-d|-debugn t-h|-helpn t-v|-verbosen Logical Volume(s)n)*/xx(lvchange, Change the attributes of logical volume(s), CACHE_VGMETADATA, lvchangen t-A|-autobackup y|nn t-a|-available e|ly|nn t-addtag Tagn t-alloc AllocationPolicyn t-C|-contiguous y|nn t-d|-debugn t-deltag Tagn t-f|-forcen t-h|-helpn t-ignorelockingfailuren t-ignoremonitoringn t-monitor y|nn t-M|-persistent y|n -major major -minor minorn t-P|-partial n t-p|-permission r|rwn t-r|-readahead ReadAheadSectors|auto|nonen t-refreshn t-resyncn t-t|-testn t-v|-verbosen t-y|-yesn t-version n tLogicalVolumePath LogicalVolumePath.n, alloc_ARG, autobackup_ARG, available_ARG, contiguous_ARG, force_ARG, ignorelockingfailure_ARG, ignoremonitoring_ARG, major_ARG, minor_ARG, monitor_ARG, partial_ARG, permission_ARG, persistent_ARG, readahead_ARG, resync_ARG, refresh_ARG, addtag_ARG, deltag_ARG, test_ARG, yes_ARG)xx(lvconvert, Change logical volume layout, 0, lvconvert -m|-mirrors Mirrors -mirrorlog disk|core|-corelogn t-R|-regionsize MirrorLogRegionSizen t-alloc AllocationPolicyn t-b|-backgroundn t-d|-debugn t-h|-?|-helpn t-i|-interval secondsn t-v|-verbosen t-version n tLogicalVolumePath PhysicalVolumePath.nn lvconvert -s|-snapshotn t-c|-chunksizen t-d|-debugn t-h|-?|-helpn t-v|-verbosen t-Z|-zero y|nn t-version n tOriginalLogicalVolumePath SnapshotLogicalVolumePathn, alloc_ARG, background_ARG, chunksize_ARG, corelog_ARG, interval_ARG, mirrorlog_ARG, mirrors_ARG, regionsize_ARG, snapshot_ARG, test_ARG, zero_ARG)xx(lvcreate, Create a logical volume, 0, lvcreate n t-A|-autobackup y|nn t-addtag Tagn t-alloc AllocationPolicyn t-C|-contiguous y|nn t-d|-debugn t-h|-?|-helpn t-i|-stripes Stripes -I|-stripesize StripeSizen t-l|-extents LogicalExtentsNumber |n t -L|-size LogicalVolumeSizekKmMgGtTpPeEn t-M|-persistent y|n -major major -minor minorn t-m|-mirrors Mirrors -nosync -mirrorlog disk|core|-corelogn t-n|-name LogicalVolumeNamen t-p|-permission r|rwn t-r|-readahead ReadAheadSectors|auto|nonen t-R|-regionsize MirrorLogRegionSizen t-t|-testn t-type VolumeTypen t-v|-verbosen t-Z|-zero y|nn t-versionn tVolumeGroupName PhysicalVolumePath.nn lvcreate -s|-snapshotn t-c|-chunksizen t-A|-autobackup y|nn t-addtag Tagn t-alloc AllocationPolicyn t-C|-contiguous y|nn t-d|-debugn t-h|-?|-helpn t-i|-stripes Stripes -I|-stripesize StripeSizen t-l|-extents LogicalExtentsNumber%VG|LV|PVS|FREE |n t -L|-size LogicalVolumeSizekKmMgGtTpPeEn t-M|-persistent y|n -major major -minor minorn t-n|-name LogicalVolumeNamen t-p|-permission r|rwn t-r|-readahead ReadAheadSectors|auto|nonen t-t|-testn t-v|-verbosen t-versionn tOriginalLogicalVolumePath PhysicalVolumePath.nn, addtag_ARG, alloc_ARG, autobackup_ARG, chunksize_ARG, contiguous_ARG, corelog_ARG, extents_ARG, major_ARG, minor_ARG, mirrorlog_ARG, mirrors_ARG, name_ARG, nosync_ARG, permission_ARG, persistent_ARG, readahead_ARG, regionsize_ARG, size_ARG, snapshot_ARG, stripes_ARG, stripesize_ARG, test_ARG, type_ARG, zero_ARG)xx(lvdisplay, Display information about a logical volume, 0, lvdisplayn t-a|-alln t-c|-colonn t-d|-debugn t-h|-helpn t-ignorelockingfailuren t-m|-mapsn t-nosuffixn t-P|-partial n t-units hsbkmgtHKMGTn t-v|-verbosen t-version n tLogicalVolumePath LogicalVolumePath.n n lvdisplay

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论