Multipass — 如 Docker 般的虛擬機

Jack Kuo
5 min readFeb 11, 2020

好讀版:https://jackkuo.org/post/multipass_tutorial/

前言

會看到這套件是因為推有大力推薦,原本想說不過就是虛擬機,KVM、VirtualBox 這些老牌肯定更快更穩。但我錯了… ,用過之後覺得,這體驗很像是在用 docker 呢!

所有的操作都可以透過指令完成,你可能會想說「這其他 VM 也都可以啊!」,沒錯,但是那些指令大多又臭又長,映像檔管理也十分不方便,若要透過腳本部署,還得透過 Vagrant。

是的,這些在 Multipass 中全部內建,給你一個如 docker 般的體驗(當然啟動速度沒那麼快啦)。

虛擬化方面

  • 在 Linux 上面是透過 KVM
  • 在 Windows 上面是透過 Hyper-V
  • 在 Mac 上面透過 HyperKit(可選用 VirtualBox)

安裝

Linux

$ snap install multipass --classic

Mac

$ brew cask install multipass

執行

最快速的方式,全部使用預設值

$ multipass launch

或是給點參數

$ multipass launch -c 4 -m 512M -d 2G -n My_First_Instance

其中

  • -c 代表 cpu 數量
  • -m 代表記憶體
  • 可以用 K, M, G 等單位表示
  • 最小值:128M,預設 1G
  • -d 代表磁碟空間。
  • 可以用 K, M, G 等單位表示
  • 最小值:512M,預設 5G
  • -n 代表自訂名稱

列出虛擬機

$ multipass listName                    State             IPv4             Image
gorgeous-cricket Running 192.168.64.5 Ubuntu 18.04 LTS
nourished-oryx Running 192.168.64.2 Ubuntu 18.04 LTS
test Running 192.168.64.4 Ubuntu 18.04 LTS

查看狀態

$ multipass info testName:           test
State: Running
IPv4: 192.168.64.4
Release: Ubuntu 18.04.3 LTS
Image hash: ae2c9391b71a (Ubuntu 18.04 LTS)
Load: 0.00 0.02 0.00
Disk usage: 995.9M out of 2.0G
Memory usage: 88.3M out of 481.2M

進入 shell 環境

$ multipass shell testWelcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-76-generic x86_64)  System information as of Tue Feb 11 18:35:49 CST 2020  System load:  0.0               Processes:             127
Usage of /: 49.6% of 1.96GB Users logged in: 0
Memory usage: 28% IP address for enp0s2: 192.168.64.4
Swap usage: 0%
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
ubuntu@test:~$

終止

$ multipass stop XXX

刪除

$ multipass delete XXX

這時候,使用 multipass list 還看得到那個 instance,必須使用 multipass purge 才會真的清掉。

列出所有可用映像檔

$ multipass find

其他指令

就不逐一列出了

Available commands:
delete Delete instances
exec Run a command on an instance
find Display available images to create instances from
get Get a configuration setting
help Display help about a command
info Display information about instances
launch Create and start an Ubuntu instance
list List all available instances
mount Mount a local directory in the instance
purge Purge all deleted instances permanently
recover Recover deleted instances
restart Restart instances
set Set a configuration setting
shell Open a shell on a running instance
start Start instances
stop Stop running instances
suspend Suspend running instances
transfer Transfer files between the host and instances
umount Unmount a directory from an instance
version Show version details

特殊之處

  • 可以透過 cloud-init 來部署環境
  • 可以使用 libvirt 驅動,因此可以使用 virsh,像是管理 KVM 一樣

--

--