Limaを使って仮想マシンを構築してみる

2023.12.07
2024.03.24
コンテナ
Lima

はじめに

Lima とはどんなものなのか、どうやって使うのかざっくりまとめてみたいと思います。

Lima とは

Lima(Linux Machines)は、ファイル共有とポートフォワードを自動でやってくれる Linux 仮想マシンを構築してくれるツールです。

containerd を標準でサポートしており、そのほかのコンテナエンジン(Docker, Podman など)もサポートしています。

そのため、Lima で VM 構築して、その上でコンテナを起動するといったことができます。

インストール

インストールはbrewを使ってできます。

1brew install lima

使い方

簡単な使い方を紹介します。

VM の起動

下記のコマンドで VM の起動ができます。--name=[instance name]でインスタンスの名前を設定できます。デフォルトの名前はdefaultです。

1limactl start

実行すると、そのままの設定で起動するか、エディタを開いて設定を変更するか、テンプレートを選択するか聞かれますが、Proceed with the current configurationを選択するとそのままの設定で VM の起動が開始されます。

1❯ limactl start
2? Creating an instance "default"  [Use arrows to move, type to filter]
3> Proceed with the current configuration
4  Open an editor to review or modify the current configuration
5  Choose another template (docker, podman, archlinux, fedora, ...)
6  Exit

テンプレートから起動

テンプレートを選択して、起動する場合は下記になります。

1limactl start template://[template name]
1limactl start template://rocky-9

テンプレートは下記から選択できます。

Templates

設定ファイルから起動

自分で設定ファイルを作成して、その設定から VM を起動することもできます。

1limactl start [yaml file]
1limactl start my-vm.yaml

設定ファイルの中身はdefaultの設定を参考に作成できます。

lima/examples/default.yaml at master · lima-vm/lima

lima/examples/default.yaml at master · lima-vm/lima

Linux virtual machines, with a focus on running containers - lima-vm/lima

VM の一覧

下記で VM の一覧が確認できます。

1limactl list
1❯ limactl list
2NAME       STATUS     SSH                VMTYPE    ARCH      CPUS    MEMORY    DISK      DIR
3default    Running    127.0.0.1:60022    qemu      x86_64    4       4GiB      100GiB    ~/.lima/default

コマンドの実行とシェルへの接続

下記のコマンドで対象のインスタンスでコマンドを実行できます。

1limactl shell [instance] [command]

また、下記で対象のインスタンスのシェルに接続できます。

1limactl shell [instance]

limaコマンドもあり、下記 2 つのコマンドは同じものになります。

1lima
2limactl shell default

実際に使ってみる

下記の"Learning eBPF"のサンプルで用意されているlearning-ebpf.yamlを使って、VM を起動してみたいと思います。

learning-ebpf/learning-ebpf.yaml at main · lizrice/learning-ebpf

learning-ebpf/learning-ebpf.yaml at main · lizrice/learning-ebpf

Learning eBPF, published by O'Reilly - out now! Here's where you'll find a VM config for the examples, and more - lizrice/learning-ebpf

まずは、設定ファイルを使って VM を起動します。

1❯ limactl start learning-ebpf.yaml
2? Creating an instance "learning-ebpf"  [Use arrows to move, type to filter]
3> Proceed with the current configuration
4  Open an editor to review or modify the current configuration
5  Choose another template (docker, podman, archlinux, fedora, ...)
6  Exit

起動が完了して、limactl listを実行すると起動しているのが確認できます。

1❯ limactl list
2NAME             STATUS     SSH                VMTYPE    ARCH      CPUS    MEMORY    DISK      DIR
3learning-ebpf    Running    127.0.0.1:58090    qemu      x86_64    4       10GiB     100GiB    ~/.lima/learning-ebpf

シェルに接続してみます。

1❯ limactl shell learning-ebpf
2masa@lima-learning-ebpf:/Users/masa$

lsとかを実行すると、ホストマシンのファイルが確認でき、ファイル共有ができていることがわかります。また、ホームディレクトリは VM のユーザーのホームディレクトリになっていることがわかります。

1masa@lima-learning-ebpf:/Users/masa$ echo $HOME
2/home/masa.linux

VM の停止と削除は下記のコマンドでできます。

1limactl stop learning-ebpf
1limactl delete learning-ebpf

参考

Support

\ この記事が役に立ったと思ったら、サポートお願いします! /

buy me a coffee
Share

Profile

author

Masa

都内のIT企業で働くエンジニア
自分が学んだことをブログでわかりやすく発信していきながらスキルアップを目指していきます!

buy me a coffee