LoginSignup
11
7

More than 3 years have passed since last update.

仮想環境multipassを試す

Last updated at Posted at 2019-10-22

参考
1. multipass公式
2. Ubuntu Weekly Recipe 第590回 Windows/macOS/Linuxで使える仮想マシン管理ツール『multipass』

この記事に書いてあることはUbuntu Weekly Recipeにかかれていることをトレースしたものに自分なりの注意点を書き込んだものです。
お急ぎの方は公式(1)のGetting startedを、詳しく見たい人は(2)の記事を、中間の人はこの記事を見てください。

インストール

ホストマシンはubuntu18.04を使っていますので、snapでインストールしました。

sudo snap install multipass --classic --beta
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 option
  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 option
  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

インスタンス管理系コマンド

find: インスタンス検索

multipassのベースイメージを検索します。

$ multipass find
Image                   Aliases           Version          Description
snapcraft:core          core16            20191003         Snapcraft builder for Core 16
snapcraft:core18                          20191003         Snapcraft builder for Core 18
core                    core16            20190806         Ubuntu Core 16
core18                                    20190806         Ubuntu Core 18
16.04                   xenial            20191011         Ubuntu 16.04 LTS
18.04                   bionic,lts        20191008         Ubuntu 18.04 LTS
19.04                   disco             20191008         Ubuntu 19.04
19.10                   eoan              20191017         Ubuntu 19.10

launch --name [name]: インスタンスの開始(startと何が違うんだ)

イメージ名またはエイリアス名を使用して仮想マシンを作成します。
省略するとlts, 長期安定版が選択されるようです。上のリストの中だったら
18.04 bionic,lts 20191008 Ubuntu 18.04 LTS
docker立ち上げるときにベース名省略するとlatestが自動的に選択されるのと一緒ですね。

multipass launch --name [name]で任意の名前の

$ multipass launch --name ubuntu1804

イメージのタウンロードから構築まで私の環境では11分かかりました。

$ multipass list
Name                    State             IPv4             Image
ubuntu1804              Running           10.56.126.57     Ubuntu 18.04 LTS

stop [name]: 停止

$ multipass stop ubuntu1804

$ multipass list
Name                    State             IPv4             Image
ubuntu1804              Stopped           --               Ubuntu 18.04 LTS

start <name> : 起動(デフォルト名はprimary)

$ multipass start
Launched: primary

$ multipass list
Name                    State             IPv4             Image
primary                 Running           10.56.126.125    Ubuntu 18.04 LTS
ubuntu1804              Stopped           --               Ubuntu 18.04 LTS

$ multipass start ubuntu1804

$ multipass list
Name                    State             IPv4             Image
primary                 Running           10.56.126.125    Ubuntu 18.04 LTS
ubuntu1804              Running           10.56.126.57     Ubuntu 18.04 LTS

delete : 削除

$ multipass delete primary
# 引数つけないと怒られる

purge: 削除状態になったインスタンスすべてを完全に削除

$ multipass purge primary
This command takes no arguments
# 引数つけると怒られる
$ multipass purge

その他

  • recover : 削除状態になったインスタンスの復旧
  • restart [name]: 再起動

shellログイン

shell [name]: sshログイン

以下gihyo記事から引用

ポイントはdefault_userとしてmultipassを指定していること,そしてそのユーザーのログイン用に鍵を登録していることです。この鍵はmultipassが作っている鍵であり,multipassコマンド経由のSSHログインに使われます。

$ multipass shell ubuntu1804
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-65-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Tue Oct 22 12:24:15 JST 2019

  System load:  0.0               Processes:           80
  Usage of /:   24.1% of 4.67GB   Users logged in:     0
  Memory usage: 11%               IP address for ens3: 10.56.126.57
  Swap usage:   0%


 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

16 packages can be updated.
0 updates are security updates.


To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

multipass@ubuntu1804:~$ whoami
multipass
multipass@ubuntu1804:~$ exit
logout

multipassのユーザー名はmultipass

普通のsshログイン

普通のsshコマンドでログインするにはpublic keyを決めてあげないといけません。
launchする際にpublic keyが書かれたファイルを--cloud-initオプションで指定することによりパスワードを設定できます。

$ cat >cloud-config.yaml <<EOF
heredoc> #cloud-config
heredoc> password: ubuntu
heredoc> chpasswd: { expire: False }
heredoc> ssh_pwauth: True
heredoc> EOF

$ cat cloud-config.yaml
#cloud-config
password: ubuntu
chpasswd: { expire: False }
ssh_pwauth: True

$ multipass launch --cloud-init cloud-config.yaml
Launched: oriented-meerkat

$ multipass list
Name                    State             IPv4             Image
oriented-meerkat        Running           10.56.126.43     Ubuntu 18.04 LTS

$ ssh multipass@10.56.126.43
The authenticity of host '10.56.126.43 (10.56.126.43)' can't be established.
ECDSA key fingerprint is SHA256:ADn3rtj3ZmBe9z9RRJZKu/kfQBIpgHZ3xy8L5oruXCI.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.56.126.43' (ECDSA) to the list of known hosts.
multipass@10.56.126.43's password:
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-65-generic x86_64)

略
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

multipass@oriented-meerkat:~$ whomi

exec [name] [command]: sshログイン

$ multipass exec ubuntu1804 -- ls -A
.bash_logout  .bashrc  .cache  .gnupg  .profile  .ssh
# multipass exec ubuntu1804 ls -A  <-これは動かない

オプションをつけたりするためにはダッシュ2つ--でコマンドを繋いであげます。

transfer [name] [file]: インスタンスへのscp

$ multipass transfer cloud-config.yaml ubuntu1804:cloud-config.yaml

$ multipass exec miami -- ls -A
.bash_history  .bash_logout  .bashrc  .cache  .gnupg  .profile  .ssh  cloud-config.yaml
# cloud-config.yamlがインスタンス内にコピーされた

$ multipass exec miami -- rm cloud-config.yaml

$ multipass exec miami -- ls -A
.bash_history  .bash_logout  .bashrc  .cache  .gnupg  .profile  .ssh

mount src_dir [name]:dst_dir

$ ls ../chrome
ChromeGestures.ini  Keyconfig4GoogleChrome.JSON  bookmark/  smartup.config
$ multipass mount ../chrome/ miami:chrome  # 親ディレクトリのchromeディレクトリをマウント

$ multipass exec miami -- ls -A
.bash_history  .bash_logout  .bashrc  .cache  .gnupg  .profile  .ssh  .sudo_as_admin_successful  chrome

$ multipass exec miami -- ls -A chrome  # lsすると同じファイルが見えるのでマウントされたのがわかる
ChromeGestures.ini  Keyconfig4GoogleChrome.JSON  bookmark  smartup.config

start と launchの違い

launch: Create and start a new instance.
start: Start the named instances. Exits with return code 0

launchは新しくインスタンス作成して、startは既存のインスタンス作成する...ようですが、multipass startでnameを指定しない時"primary"という名前でインスタンスが作成される様子がlaunchと何が違うんだろうか?という考えになりました。
しかしhelpを覗いてみるとlaunchの方がcpuやdisk容量変えたり出来るようでオプション豊富でした。
一方でstartは--allオプションつけることで既存の複数インスタンスを同時に始めることが出来るようです。

$ multipass launch --help
Usage: multipass launch [options] [[<remote:>]<image> | <url>]
Create and start a new instance.

Options:
  -h, --help           Display this help
  -v, --verbose        Increase logging verbosity, repeat up to three times for
                       more detail
  -c, --cpus <cpus>    Number of CPUs to allocate
  -d, --disk <disk>    Disk space to allocate. Positive integers, in bytes, or
                       with K, M, G suffix. Minimum: 512M.
  -m, --mem <mem>      Amount of memory to allocate. Positive integers, in
                       bytes, or with K, M, G suffix. Mimimum: 128M.
  -n, --name <name>    Name for the instance
  --cloud-init <file>  Path to a user-data cloud-init configuration, or '-' for
                       stdin

Arguments:
  image                Optional image to launch. If omitted, then the default
                       Ubuntu LTS will be used.
                       <remote> can be either ‘release’ or ‘daily‘. If <remote>
                       is omitted, ‘release’ will be used.
                       <image> can be a partial image hash or an Ubuntu release
                       version, codename or alias.
                       <url> is a custom image URL that is in http://, https://,
                       or file:// format.


$ multipass start --help
Usage: multipass start [options] [<name> ...]
Start the named instances. Exits with return code 0
when the instances start, or with an error code if
any fail to start.

Options:
  -h, --help     Display this help
  -v, --verbose  Increase logging verbosity, repeat up to three times for more
                 detail
  --all          Start all instances

Arguments:
  name           Names of instances to start. If omitted, and without the --all
                 option, 'primary' will be assumed.
Name:           ubuntu1804
State:          Running
IPv4:           10.56.126.210
Release:        Ubuntu 18.04.3 LTS
Image hash:     6203f6bff5ce (Ubuntu 18.04 LTS)
Load:           0.04 0.01 0.00
Disk usage:     4.4G out of 4.7G
Memory usage:   220.0M out of 985.4M
Mounts:         /home/u1and0/Dropbox/Program/chrome => chrome
                    UID map: 1000:default
                    GID map: 1000:default

launchではデフォルトはディスク5GB程度、メモリ1GB程度用意されるようです。

感想

使ってみた感触としてはVirtualbox + vagrantでやってたことがmultipass一つで済むのでいい感じ。
dockerをインストールして動かしてみましたが問題なく動きました。(windows環境で試したかったのですが、今回はubuntuマシン上のmultipass環境内のdocker)
業務で使うPCは Windows7が割り当てられているので、Virtualboxを用意してvagrantからsshして中ではdockerを動かしているというフクザツなことをやっていますが、Virtualboxとvagrantがまとまるのでうれしい。
ディストリビューションの差異はどうせdockerに吸収されるのでVirtualboxで用意するboxはubuntu一択です。(なんでもいいということ。)そのため、勝手に最新版のubuntuディストロをデフォルトで選んでくれるmultipassは私の用途にあっていると思いました。
Windows 10にはWSLがあるので、単なるshell環境として使うには出番なさそうですが、なんらかの制約でWSLが使えない状況の代替手段になるかと思いました。

逆に、向かない用途を挙げます。

  • GUIなし
    • CUI onlyです。サーバー用途向け。
  • ディストリビューションの種類が少ない。
    • いろんなディストロを試したいならVirtualboxなどの既存の仮想環境を使いましょう
11
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
7