0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

背景

某賃貸に引っ越したところ、全戸でDHCPが一つだった。

つまり、IPアドレスを固定することができない。

vmにsshで接続する場合ip aipconfigコマンドを使用し、IPアドレスを確認する必要がある。

この手間を解決するために、Shell Scriptを作成した。

作成にあたって、vmwareコマンド、vmのディレクトリ構造について確認しておく

vmrunコマンド

VMを操作可能なコマンドを以下の表に示す。

コマンド例 説明
vmrun start <path-to-vmx> 仮想マシンを起動
vmrun stop <path-to-vmx> 仮想マシンを停止(強制終了は hard オプション)
vmrun suspend <path-to-vmx> 仮想マシンを一時停止
vmrun reset <path-to-vmx> 仮想マシンをリセット
vmrun list 起動中の仮想マシン一覧を表示
vmrun getGuestIPAddress <path-to-vmx> ゲストOSのIPアドレスを取得
vmrun -T ws <command> VMware Workstation用
vmrun -T fusion <command> VMware Fusion用

vmrun を使用することで、VMの基本的な操作を行うことができる。

VMのディレクトリ構造

vmwareのディレクトリ構造は基本的に以下のようになっている。

~/Virtual Machines.localized/
├──Rocky Linux 64 ビット ARM.vmwarevm/
│   ├── Rocky Linux 64 ビット ARM.vmx         ← 仮想マシン設定ファイル(最重要)
│   ├── Rocky Linux 64 ビット ARM.vmsd        ← スナップショット管理ファイル
│   ├── Rocky Linux 64 ビット ARM.vmxf        ← チームや共有設定(使わないことも)
│   ├── Rocky Linux 64 ビット ARM.nvram       ← BIOS設定情報(NVRAM)
│   ├── *.vmdk                                ← 仮想ディスクファイル(複数に分割される)
│   ├── vmware.log                            ← ログファイル(複数世代あることも)
│   ├── *.lck/                                ← ロック情報(起動中に一時的に作成される)
│   └── *.plist                                ← macOSの情報用(Fusionの場合)
│
├── Ubuntu 64 ビット ARM.vmwarevm/
│   ├── Ubuntu 64 ビット ARM.vmx
│   ├── Ubuntu 64 ビット ARM.vmsd
│   ├── Ubuntu 64 ビット ARM.vmxf
│   ├── Ubuntu 64 ビット ARM.nvram
│   ├── Ubuntu ディスク-s001.vmdk ~ s00N.vmdk
│   ├── vmware.log
│   └── その他関連ファイル
│
├── CentOS 7.vmwarevm/
│   ├── CentOS 7.vmx
│   ├── CentOS 7.vmsd
│   ├── CentOS 7.vmxf
│   ├── CentOS 7.nvram
│   ├── CentOS ディスク-s001.vmdk ~ s00N.vmdk
│   ├── vmware.log
│   └── その他関連ファイル

vmrunコマンドで必要となる.vmxファイルのpathさえ分かれば、IPアドレスを確認することができる。

ShellScript

作成したShellScriptを以下に示す。

#!/bin/zsh

# Set the base directory where VMware virtual machines are stored
VM_BASE_DIR="$HOME/Virtual Machines.localized"

# Find all .vmx files under the base directory and store them in an array
vmx_files=()
while IFS= read -r line; do
  vmx_files+=("$line")
done < <(find "$VM_BASE_DIR" -name '*.vmx')

# Exit if no .vmx files are found
if (( ${#vmx_files[@]} == 0 )); then
  echo "⚠️ No .vmx files found. VM_BASE_DIR: $VM_BASE_DIR"
  exit 1
fi

# If multiple .vmx files are found, prompt user to choose one
if (( ${#vmx_files[@]} > 1 )); then
  echo "Select a VM to connect:"
  select vmx_file in "${vmx_files[@]}"; do
    [[ -n "$vmx_file" ]] && break
  done
else
  vmx_file="${vmx_files[1]}"
fi

echo "Selected VMX file: $vmx_file"

# Use vmrun to get the guest VM's IP address
IP=$(vmrun getGuestIPAddress "$vmx_file" 2>/dev/null)

# Exit if IP address could not be retrieved
if [[ -z "$IP" ]]; then
  echo "⚠️ Could not retrieve IP address. The VM may not be running."
  exit 1
fi

echo "🎯 IP address of VM: $IP"

# Connect to the VM via SSH (replace 'root' if needed)
ssh root@"$IP"

alias sshvmとして.zshrcに追記し使用している。

まとめ

dockerでも良いのでは?と思われる方もいらっしゃると思うので、containerとVMの違いについて機会があればまとめたい。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?