Linux あれこれ

Linux 関連(一部 Windows11 )の備忘録です。

Vagrant 実践編2: 既存の「EndeavourOS」(Openbox) から「Box」を作成〈H170-3〉

登録日: 2023-12-19 更新日: 2024-03-14

Xubuntu 22.04 LTS」および「Windows11」(22H2) の「VirtualBox 7.0」の仮想マシンに、色々なLinux ディストリビューションをインストールして、自分に最適なものはないか試しています。

開発環境では、それらの仮想マシン(ボックス)の管理に「Vagrant」(ベイグラント) というツールが、よく使われています。

-

前回の(実践編1) の投稿で、「Xubuntu 22.04.3 LTS」の「VirtualBox 7.0.10」にて「Vagrant」を使えるようにしました。

-

(実践編2):

今回は、その環境で、「Vagrant」を使って、 「VirtualBox」ですでに作成している「仮想マシン」である、「EndeavourOS 」のデスクトップ環境を「Vagrant」の「Box」として使える(保存できる)か、トライしました:

個人で使えれば良いだけなので、「Vagrant」の「Box」の公開を前提にした(vagrant ユーザの作成や、ssh 接続等の)設定は行いませんでした。

-

その備忘録です。

-

-

(注)リンクを戻るときはブラウザの左上の「←」をクリック
スマホの場合は、「ホーム」ボタンの横の「<」キーをタップ

-

-


目次

-

参考:

  1. 「Vagrant」コマンドについて:

  2. 「Vagrant」コマンドの使用例:

  3. 「Vagrant」コマンドの使用例:(目次)

  4.  修正版3: 使い回し可能な「Vagrantfile」:

-

今回の作業:

  1. 「仮想マシン」から「Box」ファイルを作成:

  2. 「Box」ファイルを「Box」の一覧に追加:

  3.  仮の「Vagrantfile」の作成:

  4. 「仮想マシン」を(作成して)立ち上げ:

  5.  最終的な「Vagrantfile」の作成:

-

「まとめ へ」

「目次詳細 へ」

-

-


- --- - --- - --- - --- - --- - --- - --- - --- - -

Vagrant」で「EndeavourOS」(Openbox) を起動:

-

1). ホストの「Xubuntu 22.04 LTS」の端末で「vagrant up」と入力:

仮想マシン」画面が立ち上がりました。「VirtualBox マネージャ」は起動されていません。

ホストの「端末」にて、ssh 接続を繰り返すのは、予想外:

→右の緑色の「CPU モニター」が大きめに振れてます。
なかなかタイムアウトにならず、そのままだと負荷になるので、「端末」画面を右上の「x」で閉じました。

-

「Box」作成のとき、事前に「仮想マシン」に、「Vagrant」の推奨の(vagrant ユーザの作成や、ssh 接続等の)設定を行わなかった影響のようです。

→「Vagrant」は ssh 接続が前提ということ。本処置でなく、「Vagrantfile」の修正で回避 できました。

-


2). ログイン画面が表示:

→壁紙が新しくなっています。
これは、「EndeavourOS」で次期 ISO イメージの公開に伴う、「大きめの更新」があったから。

-

→パスワード入力

-


3). デスクトップ画面が表示:

→元の「仮想マシン」で使っていた壁紙と環境です。

以前と同じように「仮想マシン」は使えます。支障ありません。

Vagrant」の「Box」として保存して、使えることを確認しました。

-

-


- --- - --- - --- - --- - --- - --- - --- - --- - --

VirtualBox」について:

-

VirtualBox」は、Oracle が開発している「仮想マシン」を構築するためのソフトウェアです。

-

-

-


- --- - --- - --- - --- - --- - --- - --- - --- - ---

Vagrant」 (ベイグラント) について:

-

公式サイト:

Vagrant by HashiCorp

-

仮想マシン」の管理や操作をコマンドで使うための「フロントエンド」のソフトウェアです:

-

→詳細は、前回に投稿の「こちら 」を参照:

-

-


- --- - --- - --- - --- - --- - --- - --- - --- - ----

1. 「VirtualBox」で作成した「仮想マシン」を「Vagrant」の「Box」として使えるかトライ:

-

Xubuntu 22.04 LTS」の「VirtualBox 7.0」にインストールした「EndeavourOS(Openbox)」の「仮想マシン」を使いました。

-

個人で使えれば良いだけなので、「Vagrant」の「Box」の公開を前提にした(vagrant ユーザの作成や、ssh 接続等の)設定は行いませんでした。

-

今回、使用したのは、「Openbox」環境です。
「EndeavourOS」の最新のISO イメージからは、「Openbox」の新規インストールが出来なくなりました。

以前に投稿した手順は使えないので、

「EndeavourOS」のインストール手順は、「EndeavourOS」(Xfce)の「こちら 」の手順を参照してください:

-

-


- --- - --- - --- - --- - --- - --- - --- - --- - -----

2. 「Vagrant」で「Box」ファイルを作成:

-

参考: 「vagrant package」コマンド から抜粋:

使い方: vagrant package [オプション] [name|id]

オプション:

  --base NAME            ベース ボックスとしてパッケージ化したい
                         VirtualBox 内の VM の名前 (VirtualBox の場合)
  --output NAME          出力するファイルの名前
  --include FILE,FILE..  ボックスにパッケージ化するカンマ区切りの追加ファイル
  --info FILE            追加のボックス情報を含むカスタム info.jsonファイルへのパス
  --vagrantfile FILE     ボックスに同梱する Vagrantfile

-


1). VirtualBox に追加されている「仮想マシン」の名前(フォルダ名)を確認:

$ ls -1 ~/'VirtualBox VMs'/
:
EndeavourOS_OpenBox               ←(★)
test3_default_1701354147406_49248        ←(「Vagrant」で管理)
ubn2204-dev_default_1702311334111_40485  ←(「Vagrant」で管理)
ubn2204_default_1701558714834_11706      ←(「Vagrant」で管理)

→「★」は、「Vagrant」で管理されていません:

Vagrant」で管理されていない場合の「仮想マシン」名は、上記の「フォルダ名」です。

Vagrant」で管理されている場合の「仮想マシン」名は、フォルダ名に含まれている「default」です。

-

参考: 「Vagrant」で管理されている(VirtualBox の)すべての「仮想マシン」を表示:

$ vagrant global-status
id       name    provider   state    directory                           
-------------------------------------------------------------------------
d4b57c8  default virtualbox poweroff /home/ubn/ダウンロード/vm/test3           
5770428  default virtualbox poweroff /home/ubn/ダウンロード/vm/ubn2204         
368c4fa  default virtualbox poweroff /home/ubn/ダウンロード/vm/ubn2204-dev     
:

→「Vagrant」で管理されている場合の「仮想マシン」名は、すべて「default」です。

-


2). 「作業フォルダ」を作成して、移動:

ちょっと長いけど、分かりやすい名前にしました:

$ mkdir -p ~/ダウンロード/vm/endeavour-openbox/
$ cd ~/ダウンロード/vm/endeavour-openbox/

→「Box」ファイルが作成される場所になります

-


3). 「Box」ファイルを作成:

$ cd ~/ダウンロード/vm/endeavour-openbox/
$ vagrant package --base EndeavourOS_OpenBox

「EndeavourOS_OpenBox」は、「virtualbox」での「仮想マシン」名です。
ここからベースとなる「Box」ファイルが作成されます。

==> EndeavourOS_OpenBox: Exporting VM...

↓ 時間がかかりました

==> EndeavourOS_OpenBox: Compressing package to: /home/ubn/ダウンロード/vm/endeavour-openbox/package.box

↓ 時間がかかりました

ubn@ubn-box:~/ダウンロード/vm/endeavour-openbox$ 

プロンプトに戻りました。

→作業フォルダに、「package.box」ファイルが作成されました:

$ pwd
/home/ubn/ダウンロード/vm/endeavour-openbox
$ ls -lA
:
-rw-rw-r-- 1 ubn ubn 12637560377 12月 12 13:55 package.box

→「12.6 GB」ほど

-

-


- --- - --- - --- - --- - --- - --- - --- - --- - ----- -

3. 作成された「Box」ファイルを「Box」の一覧に追加:

-


1). できた「Box」ファイルをホームの「Box」一覧に追加:

$ vagrant box add --name Endeav-openbox package.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'Endeav-openbox' (v0) for provider: 
    box: Unpacking necessary files from: file:///home/ubn/%E3%83%80%E3%82%A6%E3%83%B3%E3%83%AD%E3%83%BC%E3%83%89/vm/endeavour-openbox/package.box
Progress: 69% (Rate: 45.4M/s, Estimated time remaining: 0:00:37)

↓ ファイルを解凍中: カウントUP

==> box: Successfully added box 'Endeav-openbox' (v0) for ''!
ubn@ubn-box:~/ダウンロード/vm/endeavour-openbox$ 

プロンプトに戻りました。

-


2). 「Box」の一覧に追加されたか確認:

$ vagrant box list
Endeav-openbox              (virtualbox, 0)              ←(追加)
nercceh/ubuntu22.04-desktop (virtualbox, 4.0.3)
ubn2204-dev                 (virtualbox, 0)

-


3). 参考: ホームの「Box」の一覧にあるファイルを確認:

$ ls -lA ~/.vagrant.d/boxes/Endeav-openbox/0/virtualbox
-rw-rw-r-- 1 ubn ubn         505 12月 12 15:47 Vagrantfile
-rw-rw-r-- 1 ubn ubn 12739932672 12月 12 15:51 box-disk002.vmdk
-rwx------ 1 ubn ubn        9564 12月 12 15:51 box.ovf
-rw-rw-r-- 1 ubn ubn          25 12月 12 15:51 metadata.json

-

参考: ホームにある「Vagrantfile」の内容を確認:
$ cat ~/.vagrant.d/boxes/Endeav-openbox/0/virtualbox/Vagrantfile
Vagrant::Config.run do |config|
  # This Vagrantfile is auto-generated by `vagrant package` to contain
  # the MAC address of the box. Custom configuration should be placed in
  # the actual `Vagrantfile` in this box.
  config.vm.base_mac = "080027A4E2F0"
end

# Load include vagrant file if it exists after the auto-generated
# so it can override any of the settings
include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)

→ケースによって、使われるテンプレートが変わる(決まっている)みたい。

-

-


- --- - --- - --- - --- - --- - --- - --- - --- - ----- --

4. 「Vagrantfile」の作成:

-


1). 「Vagrantfile」の作成:

$ cd ~/ダウンロード/vm/endeavour-openbox/
$ vagrant init -m Endeav-openbox
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

→いつもの表示メッセージです

-

確認:
$ ls -1A
:
Vagrantfile         ←(追加)
package.box

-

2). 「Vagrantfile」の内容を確認:

$ cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "Endeav-openbox"
end

-


3). 「Vagrantfile」の修正:

前回の「Ubuntu 22.04」のBox の共有で使った内容を参考にしました:

$ cd ~/ダウンロード/vm/endeavour-openbox/
$ featherpad Vagrantfile

↓ すべて置換:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "Endeav-openbox"
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = true
    #vb.memory = "2048"
    vb.memory = "4096"
    vb.cpus = 2
    vb.customize ["modifyvm", :id, "--graphicscontroller", "vmsvga"]
  end
end

Ctrl+S で保存

-

-


- --- - --- - --- - --- - --- - --- - --- - --- - ----- ---

5. 「仮想マシン」を(作成して)立ち上げ:

-


1). 「仮想マシン」を立ち上げ:

$ cd ~/ダウンロード/vm/endeavour-openbox/
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'Endeav-openbox'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: endeavour-openbox_default_1702366872448_91638
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
: 繰り返し

    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...

→緑色のCPU モニターのバーが振れてます。負荷になってます。

-

その後、赤い警告表示:

Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

和訳:

「仮想マシン」の起動の待機中にタイムアウトになりました。

これは、Vagrant が設定された時間 (「config.vm.boot_timeout」値) 内に
ゲスト マシンと通信できなかったことを意味します。

:
認証設定も適切に設定されていることを確認してください。

ボックスが適切に起動している場合は、
タイムアウト (「config.vm.boot_timeout」) 値を増やせます。

-


2). 問題点1: 立ち上げ時に ssh 接続のリトライ繰り返し、その後タイムアウト:

参考:

[Vagrant] vagrant upがタイムアウトになるときの対処法

-

OS によって、立ち上がりに時間がかかる場合は、
「Vagrantfile」に下記の 1行を追記して、タイムアウト値を延ばします:

config.vm.boot_timeout = 600

(単位は秒で、デフォルトは 300秒)

-

ssh の設定が行われていないので、たぶん、改善しないと思われます。

念の為、設定してみました:

-

回避策1: 「Vagrantfile」の修正1:(追記: 効果なしだったので作業は要りません)
$ cd ~/ダウンロード/vm/endeavour-openbox/
$ featherpad Vagrantfile

↓ すべて置換:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "Endeav-openbox"
  config.vm.boot_timeout = 600                     ←(大きくしました)
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = true
    #vb.memory = "2048"
    vb.memory = "4096"
    vb.cpus = 2
    vb.customize ["modifyvm", :id, "--graphicscontroller", "vmsvga"]
  end
end

Ctrl+S で保存

-


3). 問題点2: ディスク立ち上げでなく、ライブ立ち上げされました:

-

「Box」作成時に、「仮想マシン」に最新OS のテストのための「ISO イメージ」をマウントし放しだったみたい。

たぶん、これもタイムアウトに影響したと思われます。いつもより立ち上げが遅くなります。

-

参考: これは有益な情報です: 「ISO イメージ」で立ち上げた「仮想マシン」も「Box」にできます:

→何かに使えそう。

-

回避策2:

「ISO イメージ」をアンマウントすれば良いだけ。

-


4). 「仮想マシン」を停止:

$ cd ~/ダウンロード/vm/endeavour-openbox/
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
    default: Guest communication could not be established! This is usually because
    default: SSH is not running, the authentication information was changed,
    default: or some other networking issue. Vagrant will force halt, if
    default: capable.
==> default: Forcing shutdown of VM...

和訳:

==> default: 「仮想マシン」を正常にシャットダウンしようとしています...
    default: ゲスト通信を確立できませんでした。
    default: これは通常、SSH が実行されていない、認証情報が変更された、
    default: またはその他のネットワークの問題が原因です。
    default: 可能であれば、Vagrant は強制的に停止します。
==> default: 「仮想マシン」を強制的にシャットダウンしています...
問題点3: →「SSH」を起動していないので、当然ですが、シャットダウン時の表示がうるさいです。

→警告が表示されるだけなので、支障なし。

-


5). 「VirtualBox」マネージャを起動:

VirtualBox」マネージャで「光学ドライブ」を取り外す方が早いです。

-

左側で仮想マシンの「endeavour-openbox_default_1702366872448_91638」をクリックして選択:

右の「ストレージ」にある、[光学ドライブ] Endeavouros-Galileo-11-2023.iso (2.43 GB) をクリック

→「仮想ドライブからディスクを除去」

→[光学ドライブ] 空 に変わりました。

-


6). 「共有フォルダ」を有効にするために、ホストOS を再起動:

$ reboot

-

-


- --- - --- - --- - --- - --- - --- - --- - --- - ----- ----

6. 「仮想マシン」に設定を反映させて、再立ち上げ:

-


1). 「仮想マシン」を再作成して、立ち上げ:

$ cd ~/ダウンロード/vm/endeavour-openbox/
$ vagrant reload
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying
: 繰り返し
問題点4: →「Vagrant」はSSH 接続できることが前提なので、立ち上げ時に接続のメッセージが繰り返されるのはしかたないかな:

警告が表示されないけど、ホストOS の負荷になっています。「タイムアウト値を大きくする」修正は逆効果でした。

→「端末」画面を右上の「x」で閉じました。

-


2). ログイン画面が表示:

→パスワード入力

-


3). デスクトップ画面が表示:

-


4). 「仮想マシン」を停止:

$ cd ~/ダウンロード/vm/endeavour-openbox/
$ vagrant halt

-


5). 回避策1-2: 「Vagrantfile」の修正2(タイムアウト値を小さくしました):

今度は逆に、早くタイムアウト(端末を開放)するように値を小さくしました。

-

$ cd ~/ダウンロード/vm/endeavour-openbox/
$ featherpad Vagrantfile

↓ 2行の修正:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "Endeav-openbox"
  #config.vm.boot_timeout = 300                     ←(デフォルト値)
  config.vm.boot_timeout = 60                     ←(小さくしました)
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = true
    #vb.memory = "2048"
    vb.memory = "4096"
    vb.cpus = 2
    vb.customize ["modifyvm", :id, "--graphicscontroller", "vmsvga"]
  end
end

Ctrl+S で保存

-

参考:「config.vm.box = "Endeav-openbox"」の行を変更すれば、他の「仮想マシン」でも使いまわし出来ます:

-


6). 「仮想マシン」に設定を反映させて、再立ち上げ:

$ cd ~/ダウンロード/vm/endeavour-openbox/
$ vagrant reload
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...

赤色の警告(タイムアウト):

Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
ubn@ubn-box:~/ダウンロード/vm/endeavour-openbox$ 

→プロンプトに戻りました。

早めにタイムアウトになり、端末が開放されました。
すでに「仮想マシン」は起動されているので支障なし。

-

問題点5: リトライや警告の表示が邪魔して、それ以降のメッセージは確認できません:

→正常に立ち上がれば良いけど、うまく立ち上がらないときは情報が不足するかも。

-


7). もし、「Vagrantfile」を修正しない場合は、

仮想マシン」が立ち上がったら、「端末」画面を、右上の「x」で閉じます。

必要になった時に、「端末」を再び起動すればいいだけです。

これはこれで良いのかも。

-

-


- --- - --- - --- - --- - --- - --- - --- - --- - ----- -----

7. 「Vagrant」の個人的な使い方:

-

個人的には、色々な「Linux ディストリビューション」をインストールして確認する作業を行っています。

手間をかけてカスタマイズした「仮想マシン」ですが、ディスク容量には限りがあるので、残すのは 3個ぐらいに留めていて、その都度、削除しています。

-

Vagrant」は、「仮想マシン」を再現したり、廃棄したりが自由です。

手間をかけてカスタマイズした中で、有益な「Linux ディストリビューション」(仮想マシン)であれば、「Vagrant」で残す(保存する)ことにしました:

-

デスクトップ環境がある「仮想マシン」を「Box」に使った場合、

Vagrant」は「ssh 接続」が前提なので、推奨された「ssh 接続」等の設定が行われていないと、 「仮想マシン」の「立ち上げ」や「電源オフ」時に余計なメッセージが表示されます。

-

ssh 接続」では使わないのに、「仮想マシン」に「Vagrant」推奨の設定を追加するのは無駄です:

→「Vagrantfile」で、立ち上げのタイムアウト値を小さくする(下記の参考: を参照)と、すぐに端末が開放されました。

-

参考: 使い回し出来る「Vagrantfile」の内容:

→「Box」を作成した「仮想マシン」を「config.vm.box = "Endeav-openbox"」の行で指定すれば、他の「仮想マシン」でも使い回し出来ます:

-

-


まとめ

今回の目的は、「仮想マシン」を「Box」として簡単に保存出来るかです。なので、「Box」の公開時に行うべき?(vagrant ユーザの作成や、ssh 接続の設定)などの作業は省きました。

-

その影響で、ssh 接続の要らない、デスクトップ環境を持つ「仮想マシン」を「Box」にした場合であっても、 「仮想マシン」の立ち上げや、電源オフで、余計なメッセージが表示されます。

これは、「Vagrantfile」の修正 で何とか回避出来たので、当初の目的は果たせました。

-

-

-


-

-

    目次

-

「投稿の先頭 へ」

-

-


-

「この目次 の先頭へ」

「本編の目次 に戻る」

-