ぽっぽこメモ太郎

短くて分かりやすい備忘録を目指しています

Windows10(ホストOS)上に Windows10(ゲストOS)を構築する

目的

VirtualBox + Vagrant で、Windows10 上に Windows10 の仮想環境を構築したい

きっかけ

Windows10 上で開発することになったが、Windows10をそのまま開発に使うのは個人的に好きじゃない。
開発環境を切り離したいが、職場の都合で Docker は使えない為。

ちなみに、普段の開発では VirtualBox + Vagrant で構築した Linux環境 (Ubuntu, CentOS) で開発している。

環境

OS

ツール

つまづいた所

  • vagrant up の途中でエラーが出る
  • ゲストOSに自動でログインしてくれない

手順

boxファイル準備

公式が用意してくれている box ファイル を使用する。
このOSはブラウザテスト用らしいが、評価版OSとの違いは不明。

Virtual MachinesMSEdge on Win10 (x64) StableChoose a VM platformVagrant を選択して zipファイルをダウンロードするだけ。
お手軽。

f:id:hato_poppo_maru:20201108012137p:plain

ダウンロードした zip ファイルは解凍して、中身をVagrantfileと同じフォルダに格納した。
名前が長いのはあまり好きじゃないのでwin10.boxにリネーム。

Vagrantfile生成

仮想マシン用のフォルダを作成し、そこにVagrantfileを生成する。 Vagrantfile が何なのかは割愛。

今回は E:\Development\Vagrant\win10-dev を使うことにした。
以下、GitBash でのコマンド実行。
※ cdコマンド以外は コマンドプロンプトでも同じコマンドで実行可能

  • 移動
$ cd /e/Development/Vagrant/win10-dev
  • Vagrantfile生成
$ vagrant init

これで E:\Development\Vagrant\win10-dev に Vagrantfile が生成される。

Vagrantfile修正

いくつかの記事を参考にすると、以下のような記述が多かった。
※この設定ではうまくいかなかった。ホストOSの差か?

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

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "./win10.box"
  config.vm.guest = "windows"
  config.ssh.username = "IEUser"
  config.ssh.password = "Passw0rd!"
  config.ssh.insert_key = false

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = true

    # Customize the amount of memory on the VM:
    vb.memory = 1024 * 4
  end

  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

仮想環境構築開始

以下のコマンドで仮想環境を立ち上げることが出来る。
初回は諸々の環境構築を行うので、結構時間がかかる。

$ vagrant up

通常であれば問題なく進むが、今回は以下のようなエラーが発生した。

==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: IEUser
    default: SSH auth method: password
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. 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.

出力内容からすると、ssh 接続でタイムアウトになっているような気がする。
というか Windowsssh 接続って簡単に出来るものなのか…?

この辺りの設定について Vagrant の公式 を眺めていると以下の記述を発見。

config.vm.communicator (string) - The communicator type to use to connect to the guest box. By default this is "ssh", but should be changed to "winrm" for Windows guests.

ゲストOSが Windows の場合は config.vm.communicator を "winrm" に変更する必要があるらしい。

※WinRM とは Windows Remote Management の略。 名前の通り Windows を遠隔操作する為の機能であるが、リモートデスクトップと違いコマンドで操作出来るらしい。

ということで、Vagrantfile に

config.vm.communicator = 'winrm'
config.winrm.username = 'IEUser'
config.winrm.password = 'Passw0rd!'

を追加する。

  • 変更後の Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "./win10.box"
  config.vm.guest = "windows"

  config.vm.communicator = "winrm"
  config.winrm.username = "IEUser"
  config.winrm.password = "Passw0rd!"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = true

    # Customize the amount of memory on the VM:
    vb.memory = 1024 * 4
  end

  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

vagrant up を再実行する。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 5985 (guest) => 55985 (host) (adapter 1)
    default: 5986 (guest) => 55986 (host) (adapter 1)
    default: 22 (guest) => 2200 (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: WinRM address: 127.0.0.1:55985
    default: WinRM username: IEUser
    default: WinRM execution_time_limit: PT2H
    default: WinRM transport: negotiate
==> default: Machine booted and ready!
[default] GuestAdditions versions on your host (6.1.4) and guest (6.0.4) do not match.
Copy iso file C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso into the box $env:TEMP/VBoxGuestAdditions.iso

~ 省略(Guest Additions アップデート関連)~

==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 6.0.4
    default: VirtualBox Version: 6.1
==> default: Mounting shared folders...
    default: /vagrant => E:/Development/Vagrant/win10-dev

いい感じに動作している。

ただ、ゲストOSに自動でログインしてくれない。 参考にした記事には自動でログインすると書いてあったが、ssh から winrm に変えたからなのか…。よく分からず。

ゲストOSの設定でやってしまう方が手っ取り早いかと思い調べてみると以下の記事の 自動サインインの設定 が参考になった。

Windows の起動時にパスワードを入力せず自動でログインする方法 (自動サインインの設定方法)

これで vagrant up 時のエラーは無くなり、自動でログインしてくれるようになった。 まだ日本語化が出来ていないが、そこはそのうち追記する。