ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

Lima 实例使用指南:启动、自定义、执行命令与目录挂载全解析

Lima 实例使用指南:启动、自定义、执行命令与目录挂载全解析 Lima 实例使用指南启动、自定义、执行命令与目录挂载全解析【免费下载链接】limaLinux virtual machines, with a focus on running containers项目地址: https://gitcode.com/GitHub_Trending/lim/lima本指南围绕 LimaLinux virtual machines中实例的日常使用展开从limactl start启动首个实例、通过模板自定义实例配置到用limactl shell/lima在虚拟机中执行命令再到宿主 home 目录挂载与用户 shell 的设置。读完本文你将掌握 Lima 实例从创建到日常操作的全流程并了解这些操作背后的源码实现机制。启动你的第一个 Linux 实例运行limactl start INSTANCE即可创建并启动第一个实例其中INSTANCE是实例名称缺省时默认为default。这个默认名称在源码中被显式定义// cmd/limactl/main.go const ( DefaultInstanceName default ... )直接在终端中执行$ limactl start ? Creating an instance default [Use arrows to move, type to filter] Proceed with the current configuration Open an editor to review or modify the current configuration Choose another template (docker, podman, archlinux, fedora, ...) Exit ... INFO[0029] READY. Run lima to open the shell.选择Proceed with the current configuration直接使用当前配置继续然后在宿主终端中等待输出READY字样即表示实例已成功启动。READY由 hostagent 在实例就绪后打印随后即可用lima打开 shell见下文。交互菜单背后的实现逻辑上述四个菜单选项并非 UI 层的花架子而是limactl start内部一个完整的状态机。在 cmd/limactl/start.go 中chooseNextCreatorState函数通过循环让用户反复选择Proceed with the current configuration直接返回当前模板配置进入实例创建流程Open an editor to review or modify the current configuration调用editutil.OpenEditor打开编辑器修改 YAML编辑时文件头部会附带说明例如“对于 default 实例多数情况下你无需修改该文件”“若要取消启动请将该文件保存为空文件”Choose another template列出所有可用模板docker、podman、archlinux、fedora等内部以_开头的模板会被过滤掉见 filterHiddenTemplates供重新选择Exit以成功退出码中止。自动化场景关闭交互界面如果需要在脚本或 CI 中自动化创建实例可以加--ttyfalse禁用交互式用户界面。从 cmd/limactl/main.go 可以看到--tty是一个全局标志默认值取决于 stdout 是否为终端rootCmd.PersistentFlags().Bool(tty, isatty.IsTerminal(os.Stdout.Fd()), Enable TUI interactions such as opening an editor. Defaults to true when stdout is a terminal. Set to false for automation.)当--ttyfalse时loadOrCreateInstance会走modifyInPlace分支直接应用参数对应的 yq 表达式而不再弹编辑器并打印日志Terminal is not available, proceeding without opening an editor。与之等价的是--yes-y标志它是--ttyfalse的别名但两者不能同时使用源码中会直接报错。自定义实例从模板创建从模板docker创建一个名为default的实例再启动它limactl create --namedefault template:docker limactl start defaultlimactl create的用法是limactl create FILE.yaml|URL除了template:docker这种内置模板引用外还支持本地文件limactl create --namedefault /usr/local/share/lima/templates/fedora.yaml远程 URL务必使用可信来源limactl create --namedefault URL标准输入cat template.yaml | limactl create --namelocal -此时--name必填相关实现见 cmd/limactl/start.go 中newCreateCommand的示例说明。常用创建参数与 YAML 字段的映射limactl create/limactl start继承了limactl edit的全部编辑参数见 cmd/limactl/editflags/editflags.go这些参数最终都会转换为对实例 YAML 的 yq 表达式。常用的有参数说明对应 YAML 字段示例--cpusNCPU 数量.cpus N--memoryN内存大小单位 GiB.memory NGiB--diskN磁盘大小单位 GiB.disk NGiB--mountPATH[:w]追加挂载目录:w后缀表示可写.mounts [...]--mount-onlyPATH[:w]覆盖现有挂载.mounts [...]--mount-none移除所有挂载.mounts null--mount-writable让所有挂载可写.mounts[].writable true--mount-typeTYPE挂载类型reverse-sshfs、9p、virtiofs.mountType ...--plain纯模式禁用挂载、端口转发、containerd 等.plain true--containerdMODEcontainerd 模式user/system/usersystem/none.containerd.user/.system--vm-typeTYPE虚拟机类型qemu、vz、krunkit、wsl2、hcs 等.vmType ...--archARCH架构x86_64、aarch64、riscv64、armv7l、s390x、ppc64le.arch ...--port-forwardHOST:GUEST[,statictrue]端口转发.portForwards [...]--setEXPR直接用 yq 表达式修改模板可多次传入任意字段--param NAMEVALUE设置模板参数可多次传入.param[NAME] VALUE需要注意两点部分参数只对新实例有效。例如--arch、--plain、--containerd在 YQExpressions 中被标记为onlyValidForNewInstances对已存在实例传入时会打印提示并跳过源码提示“Hint: create a new instance withlimactl create --arch... --nameNAME”。部分参数被标记为实验性。如--mount-inotify、--audio-device、--audio-interface使用时日志会输出--xxx is experimental警告。--set的典型用法来自limactl create --help示例limactl create --set.cpus 2 | .memory 2GiB注意部分 yq 运算符受限制详见limactl help yq-restrictions对应源码 cmd/limactl/yq_restrictions.go。start命令还额外支持以下标志见 cmd/limactl/start.go--foreground在前台运行 hostagentWindows 上不可用--timeoutDURATION等待实例进入运行状态的超时时间默认值定义于instance.DefaultWatchHostAgentEventsTimeout对 Windows guest 首次启动会自动延长超时--progress通过跟踪 cloud-init 日志显示 provision 脚本进度。更完整的参数清单可在本地运行limactl create --help、limactl start --help、limactl edit --help查看对应命令实现见 cmd/limactl/start.go、cmd/limactl/edit.go。在实例中执行 Linux 命令运行limactl shell INSTANCE COMMAND即可在 VM 中执行任意命令例如查看内核信息limactl shell default uname -alimactl shell的实现位于 cmd/limactl/shell.go底层通过 SSH 连接到实例使用实例目录下的ssh.config与本地转发端口并构造一段 shell 脚本执行。它支持一系列实用标志标志说明--shellSHELL指定本次会话使用的解释器如/bin/bash--workdirDIR指定 guest 内的初始工作目录--preserve-env将宿主环境变量传播到 guest可用LIMA_SHELLENV_BLOCK屏蔽、LIMA_SHELLENV_ALLOW豁免--start若实例未运行则先启动--reconnect重新建立 SSH 会话用于清理失活的 control master--syncDIR将宿主目录同步到 guest退出时再同步回来要求宿主安装rsync且实例不能配置宿主挂载lima快捷命令与$LIMA_INSTANCE对于名为default的实例limactl shell default COMMAND可以简写为lima uname -alima是一个 shell 包装脚本cmd/lima本质上是limactl shell --instance $LIMA_INSTANCE的别名。它支持以下环境变量$LIMA_INSTANCE要使用的实例名默认default$LIMA_SHELLguest 内使用的 shell 解释器默认使用实例内配置的用户 shell$LIMA_WORKDIRguest 内初始工作目录默认是宿主当前目录$LIMACTLlimactl二进制路径默认取$PATH中的limactl。因此你可以用LIMA_INSTANCEfoo lima uname -a在非 default 实例上执行命令也可直接使用limactl shell --instancefoo uname -a。limactl shell在实例未运行时默认会报错并提示先启动但在 TTY 交互环境下会弹出询问是否立即启动脚本场景可用--start让命令自动拉起实例。Home 目录只读挂载与 guest 独立目录宿主 home 的只读挂载默认情况下宿主 home 目录会以只读方式挂载到 guest 中路径规则为macOS 宿主/Users/${USER}其他宿主/home/${USER}这个默认挂载定义在模板基础配置 templates/_default/mounts.yaml 中mounts: - location: ~其中location: ~即宿主 home未指定writable字段因此默认只读Lima 自 v2.0 起不再默认挂载/tmp/lima见该文件注释。三个与之相关的启动选项limactl start --mount-writable将所有挂载改为可写。底层对应 yq 表达式.mounts[].writable true见 cmd/limactl/editflags/editflags.golimactl start --mount-none禁用所有挂载对应.mounts nulllimactl start --plain纯模式启动除挂载外还会一并禁用端口转发、containerd 等服务。挂载类型可通过--mount-type在reverse-sshfs、9p、virtiofs之间选择底层映射到.mountType字段。guest 独立的 home 目录guest 内部还有一个独立于宿主挂载的用户 home 目录macOS guest/Users/${USER}.guest其他 guest/home/${USER}.guest自 Lima v2.1 起Lima v2.1 之前/home/${USER}.linux之所以带.guest/.linux后缀是为了避免与宿主 home 的只读挂载点冲突——两个目录在 guest 中各自独立存在、互不干扰。guest 内的下载、缓存等个人文件应写入该独立目录而不是只读的宿主挂载。Shell 补全completionLima 为limactl提供了多 shell 的补全能力相关实现见 cmd/limactl/completion.go。启用 bash 补全在~/.bash_profile中加入source (limactl completion bash)启用 zsh 补全先查看帮助limactl completion zsh --help再按提示将输出加入~/.zshrc等配置文件。启用后输入limactl start TAB会补全已存在的实例名与模板名输入limactl shell TAB会补全实例名--cpus、--memory、--disk等标志还会给出基于宿主硬件推算的候选值见 cmd/limactl/editflags/editflags.go 中的completeCPUs与completeMemoryGiB。设置用户的登录 shellguest 内用户的默认登录 shell 可以通过以下方式覆盖实例 YAML 的user.shell字段user: shell: /bin/zsh命令行标志limactl create --shell/bin/zsh或limactl edit --shell/bin/zsh。注意该 shell 必须已经存在于 guest 镜像中且必须是绝对路径。这一点由校验逻辑保证——从 pkg/limayaml/validate_test.go 的测试用例可以看到/bin/bash合法而bash相对路径会被拒绝对 Windows guest 则只接受cmd.exe、powershell.exe、pwsh.exe及其绝对路径形式。对已存在的实例可以直接在 guest 内使用chsh修改登录 shelllimactl shell default chsh -s /bin/zsh如果只想在单次会话中使用不同的 shell而不修改任何配置使用limactl shell --shellSHELLlimactl shell --shell/bin/zsh default在非交互场景下limactl shell未指定--shell时会优先使用实例配置的user.shell否则回退到 guest 内的$SHELLWindows guest 则回退到cmd.exe见 cmd/limactl/shell.go。小结启动实例limactl start缺省实例名default交互菜单可切换模板或编辑配置自动化场景用--ttyfalse自定义实例limactl create --nameNAME template:docker配合--cpus、--memory、--mount-*、--containerd等参数底层转换为 yq 表达式写入 YAML执行命令limactl shell INSTANCE COMMANDdefault 实例可简写为lima COMMAND并支持$LIMA_INSTANCE、$LIMA_SHELL、$LIMA_WORKDIR环境变量目录挂载宿主 home 默认只读挂载--mount-writable改为可写、--mount-none/--plain禁用guest 独立 home 位于/home/${USER}.guestShell 补全bash 用source (limactl completion bash)zsh 参考limactl completion zsh --help用户 shell通过user.shell、--shell或 guest 内chsh设置单次会话用limactl shell --shellSHELL。如需进一步了解实例的 SSH 直连、自动启动等进阶用法可继续阅读仓库中的 website/content/en/docs/usage/ssh.md 与 website/content/en/docs/usage/autostart.md。【免费下载链接】limaLinux virtual machines, with a focus on running containers项目地址: https://gitcode.com/GitHub_Trending/lim/lima创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表