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?

More than 5 years have passed since last update.

Amazon Linux 2の仮想マシンをVirtualBoxで作成する時のコマンド

0
Posted at

https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/amazon-linux-2-virtual-machine.html
https://www.virtualbox.org/manual/ch08.html#vboxmanage-intro

meta-datauser-dataを探り探り設定しながら仮想マシンを作成・削除したので、仮想マシンを作成する作業をコマンドで自動化しました。

前提

  • VirtualBoxのバージョン: 6.0.14
  • VBoxManageコマンドにパスが通っている
  • Windowsのコマンドプロンプトから実行するバッチ
  • NATのポートフォーワーディングによりSSH接続できるようにする(ホスト側のポート番号は任意)

create_vm.cmdとして以下を保存

@echo off
@setlocal enabledelayedexpansion
setlocal

set CURRENT_FOLDER=%~dp0
cd %CURRENT_FOLDER%

set VM_NAME=%1
set VM_LIST_PATH=%2
set SEED_ISO_FILE_PATH=%3
set VM_FILE_PATH=%4

VBoxManage list vms > "%VM_LIST_PATH%"

findstr "%VM_NAME%" "%VM_LIST_PATH%"
if "%ERRORLEVEL%"=="0" (
    echo 仮想マシン「%VM_NAME%」は作成済み
    exit /b 0
)

VBoxManage createvm --name "%VM_NAME%" --register

VBoxManage modifyvm "%VM_NAME%" --ostype Linux26_64 --memory 8192 --vram 128 --cpus 2 --boot1 dvd --boot2 disk --boot3 none --boot4 none --nic1 nat --nictype1 82540EM --cableconnected1 on --natpf1 SSH,tcp,,50022,,22 --nic2 hostonly --hostonlyadapter2 "VirtualBox Host-Only Ethernet Adapter" --nictype2 82540EM --cableconnected2 on

VBoxManage storagectl "%VM_NAME%" --name IDE --add ide --controller PIIX4 --portcount 2 --hostiocache on --bootable on
VBoxManage storageattach "%VM_NAME%" --storagectl IDE --port 0 --device 0 --type dvddrive --medium "%SEED_ISO_FILE_PATH%"

VBoxManage storagectl "%VM_NAME%" --name SATA --add sata --controller IntelAhci --portcount 1 --hostiocache off --bootable on
VBoxManage storageattach "%VM_NAME%" --storagectl SATA --port 0 --device 0 --type hdd --medium "%VM_FILE_PATH%"

endlocal

exit /b 0

使い方

call create_vm.cmd "<仮想マシン名>" "<仮想マシン一覧を出力するファイルパス>" "<seed.isoのパス>" "<Amazon Linux 2 VM イメージのパス>"

ハマったところ

  • storagectlでIDEの場合は最小ポート個数を2にする必要があったのに対して、SATAの場合は1で良かった。
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?