轻量级虚拟机 Multipass
multipass
Multipass 是一款适用于 Windows、macOS 和 Linux 的 轻量级虚拟机管理工具 。
在不同平台上使用各自平台原生的虚拟机管理程序来启动 Ubuntu VM,在 Windows 上使用 Hyper-V
,在 macOS 上使用 QEMU
和 HyperKit,在
Linux 上使用 LXD
,以实现最小的开销和最快的启动时间。将虚拟机管理程序切换到 Virtualbox
是轻而易举的。
安装 Multipass
下载 Multipass for Windows
Note: You needWindows 10 Pro/Enterprise/Education v1803 or later
, or anyWindows 10 with VirtualBox
Ensure your network is private
Make sure your local network is designated as private, otherwise Windows prevents Multipass from starting.Run the installer
You need to allow the installer to gain Administrator privileges.
命令说明
multipass help
# or
multipass -h
# or
multipass --help
Usage: multipass [options] <command>
Create, control and connect to Ubuntu instances.
This is a command line utility for multipass, a
service that manages Ubuntu instances.
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)
v's, i.e. -vvvv.
Available commands:
alias Create an alias
aliases List available aliases
authenticate Authenticate client
delete 删除实例
exec 在实例中执行命令
find 列出可用于创建实例的镜像
get 获取某个配置项
help 查看帮助
info 查看实例信息
launch 创建并启动一个 Ubuntu 实例
list 列出所有可用的实例
mount 挂载文件夹到实例中
networks 列出可用的网络接口
purge 永久删除所有已删除的实例
recover 恢复已删除的实例
restart 重启实例
set 设置某个配置项
shell 打开一个运行中的实例 shell
start 启动实例
stop 停止实例
suspend 挂起实例
transfer 在宿主机和实例之间传输文件
umount 从实例中卸载一个已挂载的目录
unalias Remove aliases
version Show version details
开始使用
查看可用镜像
multipass find
Image Aliases Version Description
snapcraft:core18 18.04 20201111 Snapcraft builder for Core 18
snapcraft:core20 20.04 20210921 Snapcraft builder for Core 20
snapcraft:core22 22.04 20220426 Snapcraft builder for Core 22
18.04 bionic 20220712 Ubuntu 18.04 LTS
20.04 focal,lts 20220711 Ubuntu 20.04 LTS
22.04 jammy 20220712 Ubuntu 22.04 LTS
anbox-cloud-appliance latest Anbox Cloud Appliance
charm-dev latest A development and testing environment for charmers
docker latest A Docker environment with Portainer and related tools
jellyfin latest Jellyfin is a Free Software Media System that puts you in control of managing and streaming your media.
minikube latest minikube is local Kubernetes
创建实例
命令格式: multipass launch [选项] [[<remote:>]<镜像> | <url>]
选项和参数说明
选项:
-c, --cpus <cpus> 分配的cpu核数.
最小: 1, 默认: 1.
-d, --disk <disk> 分配的磁盘空间. 正整数,单位 bytes. 或使用 K, M, G 后缀.
最小: 512M, 默认: 5G.
-m, --mem <mem> 分配的内存大小. 正整数, 单位 bytes. 或使用 K, M, G 后缀.
最小: 128M, 默认: 1G.
-n, --name <name> 实例名称. 如果没有指定,则使用随机名称. 如果使用 'primary',
用户的 home 目录将自动挂载到实例的 /home/ubuntu/Home .
--cloud-init <file> | <url> Path or URL to a user-data cloud-init
configuration, or '-' for stdin
--network <spec> Add a network interface to the
instance, (太长了, 这里不复制了)
--bridged Adds one `--network bridged` network.
--mount <local-path>:<instance-path> Mount a local directory inside the
instance. If <instance-path> is omitted,
the mount point will be the same as the
absolute path of <local-path>
--timeout <timeout> Maximum time, in seconds, to wait for
the command to complete.
Arguments:
image 要使用的镜像. 如未指定,则默认使用 Ubuntu LTS 镜像.
# 注意: 如果使用的镜像本地不存在, 则会先消耗一些时间下载镜像,请耐心等待
# 创建默认基于 lts 的实例
multipass launch
# 和上面的效果完全一样
multipass launch lts
# 自定义配置: 2核, 2G内存, 40G硬盘, 实例名称 vm2004, 使用 lts 镜像。
multipass launch -c 2 -m 2G -d 40G -n vm2004 lts
# 自定义配置: 2核, 2G内存, 40G硬盘, 实例名称 vmdocker, 使用 docker 镜像。
# docker 镜像里已经安装好了 docker 和 portainer
multipass launch -c 2 -m 2G -d 40G -n vmdocker docker
# 创建实例时执行自定义初始化配置
multipass launch --name ubuntu --cloud-init config.yaml
上面的 config.yaml
示例,在初始化容器的时候,自动下载安装 Node.js:
#cloud-config
runcmd:
- curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
- sudo apt-get install -y nodejs
runcmd
可以指定容器 首次启动 时运行的命令
用户自定义的 cloud-init 的配置文件,必须以
#cloud-config
开头,这是 cloud-init 识别它的方式。cloud-init
文件说明参考: https://cloudinit.readthedocs.io/en/latest/topics/examples.html
查看所有可用实例
multipass list
# or
multipass ls
Name State IPv4 Image
primary Running 172.16.198.1 Ubuntu 20.04 LTS
vmdocker Running 172.16.198.14 Ubuntu 22.04 LTS
172.17.0.1
judicious-spittlebug Running 172.16.198.16 Ubuntu 20.04 LTS
vm2004 Stopped -- Ubuntu 20.04 LTS
查看实例信息
multipass info primary
Name: primary
State: Running
IPv4: 172.16.198.5
Release: Ubuntu 20.04.2 LTS
Image hash: 075cbdf6b7d1 (Ubuntu 20.04 LTS)
Load: 0.24 0.06 0.02
Disk usage: 2.0G out of 4.7G
Memory usage: 137.4M out of 981.3M
Mounts: /Users/mac => Home
UID map: 501:default
GID map: 20:default
外部执行实例内的命令
# 执行 vm2004 实例的 pwd 命令
multipass exec vm2004 pwd
# 输出: /home/ubuntu
进入实例内部 shell
# 进入 vm2004 实例的 shell
multipass shell vm2004
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-122-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Sun Jul 24 00:23:56 CST 2022
System load: 0.39
Usage of /: 4.1% of 38.58GB
Memory usage: 10%
Swap usage: 0%
Processes: 126
Users logged in: 0
IPv4 address for enp0s2: 172.16.198.13
IPv6 address for enp0s2: fd08:8bc3:bcd0:dfcd:60bf:51ff:feb8:4f4e
* Super-optimized for small spaces - read how we shrank the memory
footprint of MicroK8s to make it the smallest full K8s around.
https://ubuntu.com/blog/microk8s-memory-optimisation
2 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable
Last login: Sun Jul 24 00:21:19 2022 from 172.16.198.1
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
ubuntu@vm2004:~$
挂载目录
# 命令格式
multipass mount 宿主机目录 实例名:实例内目录
# 举个🌰
multipass mount /Users/mac/hello vm2004:/hello
# 查看 vm2004 信息
multipass info vm2004
Name: vm2004
State: Running
IPv4: 172.16.198.13
Release: Ubuntu 20.04.4 LTS
Image hash: 692406940d6a (Ubuntu 20.04 LTS)
Load: 0.00 0.11 0.08
Disk usage: 1.6G out of 38.6G
Memory usage: 149.6M out of 1.9G
Mounts: /Users/mac/hello => /hello
UID map: 501:default
GID map: 20:default
卸载挂载目录
# 命令格式, 如果未指定挂载路径则卸载实例内的所有挂载目录
multipass umount <实例名>[:<挂载路径>]
# 举个🌰
multipass umount vm2004:/hello
传输文件
# 命令格式
# source: 源文件路径, 如果以 <name:> 开头则表示实例内的文件路径
# destination: 目标文件路径, 如果以 <name:> 开头则表示实例内的文件路径
multipass transfer [options] <source> [<source> ...] <destination>
# 举个🌰
# 实例内文件传输到宿主机
multipass transfer vm2004:/home/ubuntu/demo.txt /Users/mac/demo.txt
# 宿主机文件传输到实例内
multipass transfer /Users/mac/demo.txt vm2004:/home/ubuntu/demo.txt
启动实例
# 命令格式
multipass start [实例名称]
# 启动一个名叫 primary 的默认实例,注意第一次启动会先消耗一些时间下载镜像,请耐心等待
multipass start
# 启动一个名叫 vm2004 的实例,该实例必须是已存在的
multipass start vm2004
multipass ls
Name State IPv4 Image
primary Running -- Ubuntu 20.04 LTS
vmdocker Running 172.16.198.14 Ubuntu 22.04 LTS
172.17.0.1
judicious-spittlebug Running 172.16.198.16 Ubuntu 20.04 LTS
vm2004 Running -- Ubuntu 20.04 LTS
停止实例
# 命令格式
multipass stop [--all] [实例名称]
# 停止所有实例
multipass stop --all
# 停止名为 judicious-spittlebug 的实例
multipass stop judicious-spittlebug
# 停止 primary 实例
multipass stop primary
# 未指定实例名, 且未指定 --all, 则默认操作 primary 实例
multipass stop
删除实例
# 命令格式,
# --all: 删除所有实例[软删]
# -p: 立即清除实例[彻底删除]
multipass delete [--all] [--purge | -p] [实例名称]
# 删除 judicious-spittlebug
multipass delete judicious-spittlebug
multipass ls
Name State IPv4 Image
primary Stopped -- Ubuntu 20.04 LTS
vmdocker Running 172.16.198.14 Ubuntu 22.04 LTS
172.17.0.1
judicious-spittlebug Deleted -- Not Available
vm2004 Stopped -- Ubuntu 20.04 LTS
可以看到 judicious-spittlebug
状态为 Deleted
;
恢复已删除实例
# 恢复 judicious-spittlebug
multipass recover judicious-spittlebug
multipass ls
Name State IPv4 Image
primary Running 172.16.198.1 Ubuntu 20.04 LTS
vmdocker Running 172.16.198.14 Ubuntu 22.04 LTS
172.17.0.1
judicious-spittlebug Stopped -- Ubuntu 20.04 LTS
vm2004 Running 172.16.198.13 Ubuntu 20.04 LTS
可以看到 judicious-spittlebug
状态已经由 Deleted
变更为 Stopped
;
彻底删除实例
# 永久清除所有已删除的实例, 包括它们的数据
multipass purge
## 或者在软删除的时候通过 --purge 参数来完全删除实例
# 如: 删除 judicious-spittlebug
multipass delete -p judicious-spittlebug
multipass ls
Name State IPv4 Image
primary Running 172.16.198.1 Ubuntu 20.04 LTS
vmdocker Running 172.16.198.14 Ubuntu 22.04 LTS
172.17.0.1
vm2004 Running 172.16.198.13 Ubuntu 20.04 LTS
可以看到 judicious-spittlebug
镜像已被彻底删除;
常见问题
实例异常,启动时提示 Instance stopped while starting
有时 multipass 实例会启动失败,尤其在宿主机宕机或异常关闭后。
~ % multipass start -vvvv ubuntu-lts
start failed: The following errors occurred:
Instance stopped while starting
~ % multipass ls
Name State IPv4 Image
ubuntu-lts Starting - Ubuntu 20.04 LTS
重启 multipass 守护进程可以解决问题: sudo pkill multipassd
~ % ps -ef | egrep multipass
0 35588 1 0 3:46PM ?? 0:00.89 /Library/Application Support/com.canonical.multipass/bin/multipassd --verbosity debug
501 35657 34642 0 3:49PM ttys000 0:00.00 egrep multipass
~ % sudo pkill multipassd
~ % ps -ef | egrep multipass
0 35660 1 0 3:49PM ?? 0:00.63 /Library/Application Support/com.canonical.multipass/bin/multipassd --verbosity debug
501 35665 34642 0 3:50PM ttys000 0:00.00 egrep multipass
~ % multipass start
Starting ubuntu-lts |
~ % multipass ls
Name State IPv4 Image
ubuntu-lts Running 192.168.64.2 Ubuntu 20.04 LTS
~ %
Multipass 实例状态一直处于 starting
参考: How to fix Multipass stuck at "Starting" state on Mac OSX?
解决方法:
ps -ef | grep -i multipass | awk '{print "sudo kill -9 "$2}' | sh
例如:
# Step 1: see the status of VMs
$ multipass ls
Name State IPv4 Image
vm1 Stopped -- Ubuntu 20.04 LTS <-- this one is OK, stopped in previous command
vm2 Starting -- Ubuntu 20.04 LTS <-- this one is not OK, why it's stuck?
# Step 2: apply the fix
$ ps -ef | grep -i multipass | awk '{print "sudo kill -9 "$2}' | sh
# Enter your password or PIN
# Step 3: see the status of VMs again
$ multipass ls
Name State IPv4 Image
vm1 Stopped -- Ubuntu 20.04 LTS <-- no changes for this vm
vm2 Stopped -- Ubuntu 20.04 LTS <-- OK, it's stopped now
或者使用下面脚本, 将其添加到你的 ~/.bashrc
, ~/.bash_profile
or ~/.zshrc
文件中, 然后在需要的时候使用 mpkill
即可;
# You can also put this inside your ~/.bashrc, ~/.bash_profile or ~/.zshrc file
# so you can just run `mpkill` when you need it
function mpkill() {
echo "[1/3] Killing Multipass process..."
ps -ef | grep -i multipass | awk '{print "sudo kill -9 "$2}' | sh
echo "[2/3] Sleeping..."
sleep 3
echo "[3/3] Launching new Multipass process..."
open -a Multipass
}