如何在Linux上使用MultiPass创建和启动Ubuntu VM

时间:2020-03-21 11:44:12  来源:igfitidea点击:

有多种程序和工具可用于部署Ubuntu VM以进行测试和学习目的。
其中一些是Docker,LXD,LXC,ProxMox,VirtualBox和VMware等。

我使用VirtualBox经常用于在多个Linux发行版上测试各种应用程序。
今天,我们将学习另一个名为multipass的又一个实用程序。
无论我们是常规的用户或者开发人员或者系统管理员,我们都可以在Linux中的几秒钟内立即创建和启动Ubuntu VM。

MultiPass是Canonical团队开发的命令行实用程序,用于在本地计算机上快速部署Ubuntu实例。
由于MultiPASS获取我们最新的实例,因此我们不必浪费时间在启动时更新!使用MultiPass,我们可以启动Ubuntu VM并使用它们来测试代码或者应用程序。
我们可以直接从本地主机中的VM中运行命令或者登录VM的shell并从那里运行。
也可以将主机系统的目录安装到实例中,并共享主机系统和VM之间的文件和文件夹。
MultiPass支持GNU/Linux,Mac OS和Windows。

在Linux上安装MultiPass

MultiPass可作为捕捉包提供,因此可以安装在支持Snapd的任何Linux操作系统上。
SNAP预装在所有Ubuntu版本中,从版本16.04 LTS,Ubuntu Flavors,Solus 3及以上,Zorin OS等,我们可以如此链接中所述安装Snapd。

安装Snapd后,使用命令安装MultiPass:

$sudo snap install multipass --classic

在成功的多处理器安装后,我们将看到以下输出:

multipass 1.0.2 from Canonical* installed

现在让我们继续前进并在本地系统中部署Ubuntu实例。

在Linux上使用MultiPass创建和启动Ubuntu VM

使用MultiPass运行Ubuntu VM非常简单且直接直接。

启动Ubuntu VM.

要下载并启动一个新鲜的Ubuntu VM,只需运行:

$multipass launch --name theitroad-instance

或者,

$multipass launch -n theitroad-instance

此处onitoad-instance是我的Ubuntu VM的名称。
我们可以根据喜好命名VM。

上面的命令将下载一个新鲜和最小的Ubuntu实例并自动启动它。

默认情况下,MultiPass将下载当前的Ubuntu LTS版本。

列出VMS.

要列出系统上当前下载的VM,请运行:

$multipass list

示例输出:

Name                    State             IPv4             Image
theitroad-instance      Running           10.39.86.203     Ubuntu 18.04 LTS

正如我们在上面的输出中看到的,启动了一个名为"onItoad-instance"的新Ubuntu实例,并使用Ubuntu 18.04 LTS版本运行。
而实例的IP地址是10.39.86.203.

从本地系统执行VM中的命令

MultiPass允许我们在Ubuntu实例中运行命令。
例如,我们可以使用命令找到运行VM的系统详细信息:

$multipass exec theitroad-instance -- lsb_release -a

启动Ubuntu VM的shell

而不是从本地系统运行命令,而是可以直接启动其shell并运行命令或者按照任何测试。

$multipass shell theitroad-instance

输入"退出"以从实例注销。

查找可用的实例启动

正如我所说,MultiPass将为我们提供当前LTS版本,即Ubuntu 18.04.
但是,我们可以启动其他可用VM,如果我们愿意。

要列出所有可用的VM,请运行:

$multipass find

示例输出:

Image                   Aliases           Version          Description
snapcraft:core          core16            20170221         Snapcraft builder for Core 16
snapcraft:core18                          20170221         Snapcraft builder for Core 18
core                    core16            20170213         Ubuntu Core 16
core18                                    20170210         Ubuntu Core 18
16.04                   xenial            20170218.1       Ubuntu 16.04 LTS
18.04                   bionic,lts        20170218         Ubuntu 18.04 LTS
19.10                   eoan              20170129         Ubuntu 19.10
daily:20.04             devel,focal       20170226         Ubuntu 20.04 LTS

如我们所见,SIMPCRACT CORE,Ubuntu Core,Ubuntu 16.04,18.04,19.10和20.04日常建设。

要启动任何其他实例,请指定如下所示的图像名称。

$multipass launch --name theitroad-instance 19.10

此命令将启动使用Ubuntu 19.10运行的实例。

创建具有自定义规范的实例(CPU,硬盘大小和内存大小)

默认情况下,MultiPass将使用1 CPU,5GB硬盘和1GB 内存创建Ubuntu VM。
这足以进行基本测试。
但是,某些时候我们可能希望创建具有较低或者更高配置的VM而不是默认值。
也是可能的!

以下命令将使用2 CPU,2 GB 内存和20 GB硬盘大小旋转VM。

$multipass launch -c 2 -m 2G -d 20G -n theitroad-instance

可以使用后缀k(适用于KB),M(用于MB),G(对于GB)来指定大小。

  • 最小允许的CPU:1
  • 最小允许硬盘大小:512 MB
  • 最小允许内存大小:128 MB

查找运行实例的信息

可以通过以下命令查看有关运行实例的信息:

$multipass info theitroad-instance
Name:           theitroad-instance
State:          Running
IPv4:           10.39.86.203
Release:        Ubuntu 18.04.4 LTS
Image hash:     3c3a67a14257 (Ubuntu 18.04 LTS)
Load:           0.08 0.02 0.01
Disk usage:     999.1M out of 4.7G
Memory usage:   74.0M out of 985.1M

暂停实例

要暂停运行实例,请运行:

$multipass suspend theitroad-instance

验证是否暂停或者不使用info命令:

$multipass info theitroad-instance                            
Name:           theitroad-instance
State:          Suspended
IPv4:           -
Release:        -
Image hash:     3c3a67a14257 (Ubuntu 18.04 LTS)
Load:           -
Disk usage:     -
Memory usage:   -

正如我们所看到的,我的onItoad-实例被暂停了。

启动和停止实例

要停止运行的VM,请运行:

$multipass stop theitroad-instance

要启动VM:

$multipass stop theitroad-instance

从多人托盘图标管理Ubuntu VM

如果我们对MultiPASS命令行选项不满意,则可以从多级基托图标图标管理VM。

从主机系统的菜单或者短划线启动MultiPASS GUI。
然后,我们将看到系统托盘上的多级数据项图标。

从多级和托盘图标,可以:

  • 开始/停止VM,
  • 打开运行VM的Shell访问,
  • 禁用/启用multipass自动启动选项,
  • 退出MultiPass。

删除VM.

一旦使用Ubuntu实例完成,如果不再需要它们,可以删除它们。

首先停止要删除的VM。

$multipass stop theitroad-instance

然后,使用命令从本地主机中删除它:

$multipass delete theitroad-instance
$multipass purge

得到帮助

运行以下命令以查看可用选项和子命令的列表以及描述。

$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     Display this help
  -v, --verbose  Increase logging verbosity, repeat up to three times for more
                 detail
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

要查看每个子命令的帮助部分:

$multipass help <sub-command>

例子:

$multipass help launch