【Vagrant】日本語環境に設定したUbutuデスクトップをBoxファイルにする
環境
- ホストOS:OSX
- ゲストOS:Ubuntu14.02
- Vagrant :1.8.1
- プロバイダ:VirtualBox 5.0.16
- Packer:0.10.0
※最終的に行うBoxファイルの作成はVirtualBoxのみ対応しているので、ご注意を。
手順
PackerによるBoxファイルの作成
VagrantとPackerについては過去記事でも上げてます。
まずはベースとなるBoxファイルをPackerを利用して作成します。
Packerのテンプレートを一から作るのは大変なため、boxcutterをフォークして作成しています。
ubuntu/ubuntu1404-desktop.json at master · mktktmr/ubuntu · GitHub
ubuntu1404-desktop.json
{ "_comment": "Build with `packer build -var-file=ubuntu1404-desktop.json ubuntu.json`", "vm_name": "ubuntu1404-desktop", "desktop": "true", "cpus": "1", "disk_size": "130048", "iso_checksum": "3ffb7a3690ce9a07ac4a4d1b829f990681f7e47d", "iso_checksum_type": "sha1", "iso_name": "ubuntu-14.04.4-server-amd64.iso", "iso_path": "iso", "iso_url": "http://ftp.riken.jp/Linux/ubuntu-releases/14.04.2/ubuntu-14.04.4-server-amd64.iso", "memory": "2048", "preseed": "preseed.cfg", "vagrantfile_template": "tpl/vagrantfile-ubuntu1404-desktop.tpl" }
boxcutterのテンプレートからの主な変更点は、ISOイメージの取得先をオリジナルから、理研のミラーサーバに変更しています。
多分日本でダウンロードするならそのほうが早いかなと。
また、試行錯誤するために何度かpacker buildを繰り返したのですが、そのたびにISOイメージをダウンロードしてると時間かかるし、通信料がばかになりません。
PackerのビルドはローカルにISOイメージがあればダウンロードせずにそちらを参照する仕組みになっています。
その際に参照するディレクトリのPATHの設定が「iso_path」で、そこの値も変えています。
というわけであらかじめ理研のミラーサイトからISOファイルを入手して、gitのクローン先にisoディレクトリ掘って、突っ込んでおきます。
テンプレートの作成が終わったら、packer buildでテンプレートファイルを元にBoxファイルを作成します。
$ packer build -only=virtualbox-iso -var-file=ubuntu1404-desktop.json ubuntu.json virtualbox-iso output will be in this color. ==> virtualbox-iso: Downloading or copying Guest additions virtualbox-iso: Downloading or copying: file:///Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso ==> virtualbox-iso: Downloading or copying ISO virtualbox-iso: Downloading or copying: file:///Users/hoge/Documents/%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%A9/iso/ubuntu-14.04.4-server-amd64.iso ==> virtualbox-iso: Creating floppy disk... virtualbox-iso: Copying: http/preseed.cfg ==> virtualbox-iso: Creating virtual machine... ==> virtualbox-iso: Creating hard drive... ==> virtualbox-iso: Attaching floppy disk... ==> virtualbox-iso: Creating forwarded port mapping for communicator (SSH, WinRM, etc) (host port 3667) ==> virtualbox-iso: Executing custom VBoxManage commands... virtualbox-iso: Executing: modifyvm ubuntu1404-desktop --memory 2048 virtualbox-iso: Executing: modifyvm ubuntu1404-desktop --cpus 1 ==> virtualbox-iso: Starting the virtual machine... ==> virtualbox-iso: Waiting 10s for boot... ==> virtualbox-iso: Typing the boot command... ==> virtualbox-iso: Waiting for SSH to become available... ==> virtualbox-iso: Connected to SSH! ==> virtualbox-iso: Uploading VirtualBox version info (5.0.16) ==> virtualbox-iso: Uploading VirtualBox guest additions ISO... ==> virtualbox-iso: Provisioning with shell script: script/update.sh virtualbox-iso: ==> Disabling the release upgrader virtualbox-iso: ==> Updating list of repositories virtualbox-iso: Ign http://us.archive.ubuntu.com trusty InRelease virtualbox-iso: Get:1 http://us.archive.ubuntu.com trusty-updates InRelease [65.9 kB] virtualbox-iso: Get:2 http://security.ubuntu.com trusty-security InRelease [65.9 kB] virtualbox-iso: Get:3 http://us.archive.ubuntu.com trusty-backports InRelease [65.9 kB] virtualbox-iso: Hit http://us.archive.ubuntu.com trusty Release.gpg ・ ・ 中略 ・ ・ virtualbox-iso: zenity-common install virtualbox-iso: zip install virtualbox-iso: zlib1g:amd64 install virtualbox-iso: ==> Clearing last login information virtualbox-iso: 118528571+0 records in virtualbox-iso: 118528571+0 records out virtualbox-iso: 121373256704 bytes (121 GB) copied, 425.23 s, 285 MB/s virtualbox-iso: 189336+0 records in virtualbox-iso: 189336+0 records out virtualbox-iso: 193880064 bytes (194 MB) copied, 0.478528 s, 405 MB/s virtualbox-iso: ==> Clear out swap and disable until reboot virtualbox-iso: dd: error writing ‘/dev/dm-1’: No space left on device virtualbox-iso: 2049+0 records in virtualbox-iso: 2048+0 records out virtualbox-iso: 2147483648 bytes (2.1 GB) copied, 3.58034 s, 600 MB/s virtualbox-iso: dd exit code 1 is suppressed virtualbox-iso: mkswap: /dev/dm-1: warning: don't erase bootbits sectors virtualbox-iso: on whole disk. Use -f to force. virtualbox-iso: Setting up swapspace version 1, size = 2097148 KiB virtualbox-iso: no label, UUID=b604802c-0ad0-41a1-ade1-aa8d8d81dce5 virtualbox-iso: dd: error writing ‘/EMPTY’: No space left on device virtualbox-iso: 122139+0 records in virtualbox-iso: 122138+0 records out virtualbox-iso: 128071090176 bytes (128 GB) copied, 287.836 s, 445 MB/s virtualbox-iso: dd exit code 1 is suppressed virtualbox-iso: ==> Disk usage before cleanup virtualbox-iso: Filesystem Size Used Avail Use% Mounted on udev 990M 4.0K 990M 1% /dev tmpfs 201M 476K 200M 1% /run /dev/mapper/vagrant--vg-root 123G 3.4G 114G 3% / none 4.0K 0 4.0K 0% /sys/fs/cgroup none 5.0M 0 5.0M 0% /run/lock none 1001M 0 1001M 0% /run/shm none 100M 0 100M 0% /run/user /dev/sda1 236M 39M 185M 18% /boot virtualbox-iso: ==> Disk usage after cleanup virtualbox-iso: Filesystem Size Used Avail Use% Mounted on virtualbox-iso: udev 990M 8.0K 990M 1% /dev virtualbox-iso: tmpfs 201M 476K 200M 1% /run virtualbox-iso: /dev/mapper/vagrant--vg-root 123G 3.4G 114G 3% / virtualbox-iso: none 4.0K 0 4.0K 0% /sys/fs/cgroup virtualbox-iso: none 5.0M 0 5.0M 0% /run/lock virtualbox-iso: none 1001M 0 1001M 0% /run/shm virtualbox-iso: none 100M 0 100M 0% /run/user virtualbox-iso: /dev/sda1 236M 39M 185M 18% /boot ==> virtualbox-iso: Gracefully halting virtual machine... virtualbox-iso: Removing floppy drive... ==> virtualbox-iso: Preparing to export machine... virtualbox-iso: Deleting forwarded port mapping for the communicator (SSH, WinRM, etc) (host port 3667) ==> virtualbox-iso: Exporting virtual machine... virtualbox-iso: Executing: export ubuntu1404-desktop --output output-ubuntu1404-desktop-virtualbox-iso/ubuntu1404-desktop.ovf ==> virtualbox-iso: Unregistering and deleting virtual machine... ==> virtualbox-iso: Running post-processor: vagrant ==> virtualbox-iso (vagrant): Creating Vagrant box for 'virtualbox' provider virtualbox-iso (vagrant): Copying from artifact: output-ubuntu1404-desktop-virtualbox-iso/ubuntu1404-desktop-disk1.vmdk virtualbox-iso (vagrant): Copying from artifact: output-ubuntu1404-desktop-virtualbox-iso/ubuntu1404-desktop.ovf virtualbox-iso (vagrant): Renaming the OVF to box.ovf... virtualbox-iso (vagrant): Using custom Vagrantfile: tpl/vagrantfile-ubuntu1404-desktop.tpl virtualbox-iso (vagrant): Compressing: Vagrantfile virtualbox-iso (vagrant): Compressing: box.ovf virtualbox-iso (vagrant): Compressing: metadata.json virtualbox-iso (vagrant): Compressing: ubuntu1404-desktop-disk1.vmdk Build 'virtualbox-iso' finished. ==> Builds finished. The artifacts of successful builds are: --> virtualbox-iso: 'virtualbox' provider box: box/virtualbox/ubuntu1404-desktop-nocm-0.1.0.box
Boxファイルの登録
作成したBoxファイルをvagrantに登録します。
登録のためのコマンドはvagrant box add {登録名} {Boxファイルのパス | URL}です。
登録名は任意ですが、今回は「ubuntu1404-desktop」にしておきます。
$ vagrant box add ubuntu1404-desktop box/virtualbox/ubuntu1404-desktop-nocm-0.1.0.box ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box '1404-desktop' (v0) for provider: box: Unpacking necessary files from: file:///Users/hoge/Documents/git/boxcutter/ubuntu/box/virtualbox/ubuntu1404-desktop-nocm-0.1.0.box ==> box: Successfully added box '1404-desktop' (v0) for 'virtualbox'!
仮想マシンの起動
vagrant initをすることでVagrantfileのテンプレートファイルが作成されます。
引数には先ほど登録したBoxファイルの名前を指定します。
$ vagrant init ubuntu1404-desktop 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 Vagrantfile
Vagrantファイルの中身を確認。
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://atlas.hashicorp.com/search. config.vm.box = "ubuntu1404-desktop" # 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. # config.vm.network "forwarded_port", guest: 80, host: 8080 # 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" # end # # View the documentation for the provider you are using for more # information on available options. # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies # such as FTP and Heroku are also available. See the documentation at # https://docs.vagrantup.com/v2/push/atlas.html for more information. # config.push.define "atlas" do |push| # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME" # end # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision "shell", inline: <<-SHELL # sudo apt-get update # sudo apt-get install -y apache2 # SHELL end
今回立ち上げるゲストOSは日本語化のベースにするだけで、特にプロビジョニングなどはしないため、修正は行わずそのまま使います。
config.vm.boxの値のが正しく設定されていることだけ確認できればOKです。
というわけで、vagrant initに続けてvagrant upで仮想マシンを立ち上げます。
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'ubuntu1404-desktop'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: ubuntu1404-desktop_default_1459864237341_70016 ==> 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: Remote connection disconnect. Retrying... default: Warning: Remote connection disconnect. Retrying... default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Mounting shared folders... default: /vagrant => /Users/hoge/Documents/vagrant/ubuntu1404-desktop
日本語環境の設定
以下、ゲストOSのUbuntuでの作業になります。
ソフトウェアアップデート
初めてUbuntuを立ち上げるとソフトウェアアップデートの通知が来るので、アップデートしておきます。
再起動が必要な設定がこの後もあるので、ここでは再起動しないことにします。
日付のローカライズ
タイムゾーンをTokyoに設定します。
System Settings > Time&Date を選択します。
地図をクリックしたり、フォームに直接入力するなどしてLocationをTokyoに設定。
ついでにClockの設定も好みに合わせて設定しておきます。
言語のローカライズ
System Settings > Language Support を選択します。
Language Supportを開くと、「言語サポートに必要なソフトウェアのインストールが完了していないので、インストールしてください」的なことを言われます。
英語のサポートなので必要ないかもしれませんが、一応インストールします。
本題の日本語化の作業に入ります。
Install / Remove Languages を選択します。
Japaneseにチェックを入れて、Apply Changesをクリック。
日本語の言語サポートに必要なソフトウェアのインストールが始まります。
インストールが終了したら、日本語の優先度を上げるため言語のリストの一番上に「日本語」が来るようにドラッグします。
日本語を一番上にしたら、Apply System-Wideをクリックし、システム全体に適用します。
次に通貨や日付のフォーマットを日本に合わせる設定を行います。
Regional Format タブを選択します。
日本語を選択し、Apply System-Wideをクリックし、システム全体に適用します。
設定直後はExampleが正しく反映されません。
Language Supportを開き直すと正しく反映されるので、開き直して確認します。
ここまで設定したら、仮想マシンを再起動します。
再起動はホストOSのコンソールからvagrant reloadを叩いて行います。
再起動が済むとフォルダ名を日本語にリネームするか聞かれますので、好みでリネームするかどうか選択してください。
ちなみに私はしませんでした。
キーマップのローカライズ
キーボードのレイアウトがUS配列になっているので、日本語配列に設定します。
システム設定 > テキスト入力を選択。
日本語(Anthy)を選択して右下に現れるツールアイコンをクリックします。
IBus-Anthyの設定ダイアログが開くので、入力タイプを選択します。
キーボードレイアウトをデフォルトからjpに変更します。
ibusを再起動してくださいと表示されますが、一度ログアウトしてログインしなおすだけで大丈夫です。
ここまでで日本語化の作業は終わりになります。
ホストOSに戻ってvagrant haltで仮想マシンを止めてください。
日本語環境にした仮想環境のBoxファイルの出力
日本語設定を行った仮想マシンのVagrantfileがあるディレクトリでvagrant packageを実行することで、日本語設定がされた状態のBoxファイルを作成することができます。
下記例では--output オプションで出力されるBoxファイルのファイル名を指定しています。
指定しないとデフォルトの package.boxというファイル名で出力されます。
$ vagrant package --output ubuntu1404-ja-desktop-nocm-0.1.0.box ==> default: Clearing any previously set forwarded ports... ==> default: Exporting VM... ==> default: Compressing package to: /Users/hoge/Documents/vagrant/ubuntu1404-desktop/ubuntu1404-ja-desktop-nocm-0.1.0.box
ここまでの作業で日本語環境になったUbuntuが使えるようになりました。
利用のする際はBoxファイルの登録で行ったようにvagrant box addをしてvagrant init、vagrant upをするだけです。
今後はこのBoxファイルをベースに、Vagrantfileでプロビジョニングして色々と開発環境を作ってみようと思います。
参考
Vagrantの既存Boxに日本語環境 + GuestAdditions + Chefを入れ、新しいBoxとして追加する - メモ的な思考的な
Ubuntu 13.10 その7 - 日本語環境の構築・Ubuntu Japanese Teamのリポジトリーを追加する - kledgeb