Multipass `mount` 命令完全指南:共享目录、ID 映射与挂载类型解析
发布时间:2026/9/26 9:45:32来源:尧图网络
虚拟化开发工具云原生【免费下载链接】multipassMultipass orchestrates virtual Ubuntu instances项目地址https://gitcode.com/gh_mirrors/mu/multipass点击查看免费下载multipass mount是 Multipass 中用于将宿主机本地目录映射到 Ubuntu 实例instance文件系统的核心命令支持经典classic与原生native两种挂载类型并可通过用户/组 ID 映射UID/GID mapping保持文件所有权的跨端一致性。本文基于仓库内 mount 命令参考文档 展开结合 数据共享指南、Mount 原理文档、ID 映射文档 以及 CLI 与 daemon 源码实现完整覆盖mount/umount的语法、全部选项、实际用例、底层工作原理与安全注意事项帮助你高效地在宿主机与实例之间共享数据。mount命令概览multipass mount将宿主机上的一个本地目录映射到实例中使目录内容及其后续变更在两端同时可见。你可以通过--type指定挂载类型并通过-g/-u定义组 ID 或用户 ID 映射使实例内文件与文件夹的所有权从宿主机 ID 映射到实例 ID。基本语法如下以 classic 挂载为例multipass mount --typeclassic /host/path instance name:/instance/path使用multipass umount命令可撤销映射。关于 classic 与 native 挂载的差异详见 Mount 原理关于如何用mount在宿主机与实例间共享数据的完整示例见 How to share data with an instance。完整命令行选项参考以下内容即multipass help mount的完整输出说明了该命令的全部可用选项Usage: multipass mount [options] source target [target ...] Mount a local directory inside the instance. If the instance is not currently running, the directory will be mounted automatically on next boot. Options: -h, --help Displays help on commandline options -v, --verbose Increase logging verbosity. Repeat the v in the short option for more detail. Maximum verbosity is obtained with 4 (or more) vs, i.e. -vvvv. -g, --gid-map host:instance A mapping of group IDs for use in the mount. File and folder ownership will be mapped from host to instance inside the instance. Can be used multiple times. Mappings can only be specified as a one-to-one relationship. -u, --uid-map host:instance A mapping of user IDs for use in the mount. File and folder ownership will be mapped from host to instance inside the instance. Can be used multiple times. Mappings can only be specified as a one-to-one relationship. -t, --type type Specify the type of mount to use. Classic mounts use technology built into Multipass. Native mounts use hypervisor and/or platform specific mounts. Valid types are: classic (default) and native Arguments: source Path of the local directory to mount target Target mount points, in name[:path] format, where name is an instance name, and optional path is the mount point. If omitted, the mount point will be under /home/ubuntu/source-dir, where source-dir is the name of the source directory.参数说明source本地要挂载的目录路径必须是宿主机上真实存在、可读的目录源码层面会对此进行校验见下文。target目标挂载点格式为name[:path]其中name是实例名称path是可选的挂载点路径。若省略path挂载点默认为实例内/home/ubuntu/source-dir其中source-dir为source目录的目录名。-u/--uid-map host:instance用户 ID 映射将文件与文件夹的所有权从宿主机用户 ID 映射到实例内的用户 ID。可多次使用多对映射但每个映射必须是一对一关系。-g/--gid-map host:instance组 ID 映射规则与 UID 映射相同。-t/--type type挂载类型合法取值为classic默认与native。-v/--verbose增加日志详细程度可重复v以获取更多细节最大冗长度为-vvvv。源码中的参数校验与默认行为上述选项并非仅是文档描述mount.cpp 中的parse_args实现了完整的解析与校验逻辑可作为实际行为依据source 校验代码使用QFileInfo检查 source 路径依次验证其存在Source path ... does not exist、是目录Source path ... is not a directory、可读Source path ... is not readable并通过QDir::absolutePath()规范为绝对路径后写入请求。target 解析每个 target 参数按:切分前半部分作为实例名后半部分作为目标路径源码注释表明若省略路径挂载点将位于home_in_instance即/home/ubuntu之下的source-dir。ID 映射格式校验通过正则表达式^([0-9][:][0-9])$校验-u/-g值必须是纯数字的host:instance形式非法输入会报Invalid UID/GID map given。默认 ID 映射当未显式指定-u/-g时CLI 会添加默认映射——宿主机当前用户 UID/GIDmcp::getuid()/mcp::getgid()映射到实例内的default_id在 client_platform.h 中定义为-1由后端解释为默认用户通常即ubuntu用户。这一点解释了为何默认挂载后实例内文件归属于ubuntu用户。挂载类型校验checked_mount_type只接受classic与native两个字符串不区分大小写toLower后比较其他取值抛出ValidationException提示Bad mount type ... specified, please use classic or native。命令执行解析成功后Mount::run通过 gRPC 将MountRequest分发给 daemon并使用旋转动画spinner反馈进度失败时通过standard_failure_handler_for输出错误。实际操作示例基础共享挂载宿主机目录到实例将宿主机本地目录映射到实例基本语法为multipass mount local path instance name例如在 Linux 系统上将本地家目录$HOME映射到名为keen-yak的实例multipass mount $HOME keen-yak运行multipass info keen-yak可查看挂载结果... Mounts: /home/michal /home/michal此后本地家目录/home/michal的内容在实例内即可见、可读写。挂载路径是持久的会一直保持到被显式卸载unmount为止即使实例关机重启后挂载也会在下次启动时自动恢复CLI 帮助文本与源码均说明了这一点If the instance is not currently running, the directory will be mounted automatically on next boot。挂载到指定路径若希望将本地目录挂载到实例中的特定路径可显式指定目标multipass mount $HOME keen-yak:/some/path注意覆盖语义如果/some/path在实例文件系统中已存在其原有内容会被挂载目录临时隐藏overlay但不会被覆盖或删除卸载后原目录内容会重新出现。因此不能将外部目录挂载到实例的$HOME目录之上——$HOME中包含访问实例所需的 SSH 密钥一旦被隐藏将无法再通过 shell 登录该实例。在创建实例时定义挂载也可以在创建实例时直接指定挂载使用launch命令的--mount选项multipass launch --mount /local/path:/instance/path使用 ID 映射保持文件所有权不同系统间用户 ID 往往不一致如 macOS 用户 UID 为501而 Ubuntu 实例内默认用户ubuntu的 UID 为1000。通过-u/-g指定映射可让文件在两端保持正确的所有权。例如将宿主机 UID501映射为实例内 UID1000multipass mount ~/Documents foo:Documents -u 501:1000由于 ID 映射在宿主机与实例之间双向生效它必须是一对一关系宿主机上的每个用户/组 ID 只能映射到实例内的一个 ID反之亦然。以下命令是非法的因为它将宿主机两个不同 UID501、502映射到了实例内同一个 UID1000Multipass 将无法确定宿主机上 UID 为501的文件在实例内应归属哪个 IDmultipass mount ~/Documents foo:Documents -u 501:1000 -u 502:1000正确的多对映射写法应为multipass mount ~/Documents foo:Documents -u 501:1000 -u 502:1001同样的规则也适用于反向场景不能把实例内的一个 ID 映射到宿主机上的两个不同 ID。ID 映射的详细原理参见 ID mapping 文档。卸载共享目录umount使用umount命令卸载先前挂载的路径。可指定具体路径进行定向卸载multipass umount keen-yak:/home/michal若不指定任何路径则一次性卸载该实例的所有共享目录multipass umount keen-yakumount的完整帮助输出如下Usage: multipass umount [options] mount [mount ...] Unmount a directory from an instance. Options: -h, --help Displays help on commandline options -v, --verbose Increase logging verbosity. Repeat the v in the short option for more detail. Maximum verbosity is obtained with 4 (or more) vs, i.e. -vvvv. Arguments: mount Mount points, in name[:path] format, where name are instance names, and optional path are mount points. If omitted, all mounts will be removed from the named instances.从源码看umount.cpp 中umount命令还注册了别名unmount其解析逻辑与mount的 target 解析一致按:切分name[:path]仅给定实例名而不给路径时即表示移除该实例上的全部挂载。挂载类型classic 与 nativeMultipass 提供两种挂载类型可通过--type选择classic默认基于 Multipass 内置技术SSHFSSSH 文件系统实现在所有后端上均可用兼容性更高但因 SSH 加密通信而略有性能开销。native使用驱动相关的高性能挂载技术仅在特定后端可用Hyper-V上通过 SMB/CIFS 实现QEMU上通过 9P 协议实现。从源码看挂载类型的分派发生在 daemon 侧daemon.cpp 中mount请求处理逻辑将请求中的MountTypeCLASSIC/NATIVE转换为VMMount::MountType定义见 vm_mount.h随后make_mount据此分派return mount.get_mount_type() VMMount::MountType::Classic ? std::make_uniqueSSHFSMountHandler(vm, config-ssh_key_provider.get(), target, mount) : vm-make_native_mount_handler(target, mount);即 classic 挂载由SSHFSMountHandler处理相关实现见 sshfs_mount_handler.cpp 及sftp_server相关文件而 native 挂载则委托给具体虚拟化后端实现例如 QEMU 的 qemu_mount_handler.cpp 与 Windows 的 smb_mount_handler.cpp。若挂载目录时实例正在运行daemon 会立即激活挂载否则挂载将在实例下次启动时自动建立这一行为在 test_daemon_mount.cpp 等测试中有覆盖验证。安全注意事项由于挂载以高权限身份执行使用时需留意以下安全边界详见 Mount 文档的安全小节Linux挂载以root身份执行snap 安装除外因此对宿主机整个操作系统具有写权限。但由于只有特权用户sudo、wheel、admin组成员能使用 Multipass这通常不构成问题。若通过 snap 安装snap 的 confinement 会限制挂载范围在/home目录内且不能挂载/home中的隐藏文件/文件夹并根据所连接的接口限制可移动介质。即便如此拥有 Multipass 访问权的用户 A 仍可能访问另一用户 B 在其家目录中建立的挂载。macOS同样以root身份挂载、对整个操作系统有写权限同样因仅特权用户可用 Multipass 而无虞。Windows挂载以特权用户SYSTEM身份执行对整个操作系统有写权限。出于历史原因Windows 上挂载默认被禁用如需启用可设置local.privileged-mounts键。该设置项在 Linux 和 macOS 上默认为trueWindows 上默认为false见 local-privileged-mounts 文档可通过multipass set local.privileged-mountsYes等命令修改支持on/off、yes/no、1/0、true/false及大小写变体。延伸阅读How to share data with an instance完整的宿主机与实例数据共享实操包含transfer命令的用法。Mount 原理classic 与 native 挂载的详细对比与安全说明。ID mappingID 映射的机制与一对一约束。umount参考卸载挂载的命令参考。launch参考在创建实例时通过--mount定义挂载。local.privileged-mounts控制mount是否被允许的设置项。赞分享虚拟化开发工具云原生【免费下载链接】multipassMultipass orchestrates virtual Ubuntu instances项目地址https://gitcode.com/gh_mirrors/mu/multipass点击查看免费下载相关推荐Multipass umount 命令完全指南卸载主机与实例间的目录映射Multipass umount 命令完全指南卸载主机与实例间的目录映射 multipass umount 是 Multipass 中与 mount 命令成对虚拟化开发工具云原生Multipass项目实例数据共享指南mount与transfer命令详解Multipass项目实例数据共享指南mount与transfer命令详解 前言 Multipass作为轻量级虚拟机管理工具提供了便捷的本地开发环境搭建方案虚拟化开发工具云原生Multipass文件共享终极指南mount命令与自动同步技巧全解析Multipass文件共享终极指南mount命令与自动同步技巧全解析 Multipass作为Ubuntu官方推出的轻量级虚拟机管理工具其文件共享功能是开发者虚拟化开发工具云原生创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
网站建设高端定制企业官网