LoginSignup
36
42

More than 5 years have passed since last update.

Vagrant boxの日本語化

Last updated at Posted at 2015-10-11

[概要]

VagrantのboxファイルをAtlasからダウンロードするとUTC時間になっていたりと、海外仕様になっている。
海外仕様だと何かと不便なので、日本語化対応版として作り直す。
作りなおす際は、vagrantのpackageコマンドを使用して、再度box化する。

  • 実行環境
    • MacBook Pro (Retina, 13-inch, Late 2013)
    • 2.4 GHz Intel Core i5
    • 8 GB 1600 MHz DDR3
    • OS X El Capitan‎
    • Vagrant 1.7.4
    • VirtualBox 5.0.6

[事前準備]

Vagrant環境は事前に構築しとおくこと。
OS XへのVagrant導入方法

[作業内容]

Vagrant boxを追加する

### boxを追加
$ vagrant box add bento/centos-6.7
==> box: Loading metadata for box 'bento/centos-6.7'
    box: URL: https://atlas.hashicorp.com/bento/centos-6.7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) parallels
2) virtualbox
3) vmware_desktop

Enter your choice: 2 # virtualboxの環境なので2を選択
==> box: Adding box 'bento/centos-6.7' (v2.2.2) for provider: virtualbox
    box: Downloading: https://atlas.hashicorp.com/bento/boxes/centos-6.7/versions/2.2.2/providers/virtualbox.box
==> box: Successfully added box 'bento/centos-6.7' (v2.2.2) for 'virtualbox'!

# bentoとはChef社がメンテナンスしているboxです。
# 誰が作ったかわからないboxを使うのは不安な人にはおすすめ

### 作業ディレクトリ作成
$ mkdir bento
$ cd bento

### Vagrantfile生成
$ vagrant init bento/centos-6.7

### 仮装マシン起動
$ vagrant up
vagrant up                                                                                                                                                               
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'bento/centos-6.7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'bento/centos-6.7' is up to date...
==> default: Setting the name of the VM: bento_default_1444488217156_47646
==> 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: 22 => 2200 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. 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!
GuestAdditions 5.0.6 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /private/tmp/bento

日本語化に必要なパッケージ関連をインストール

### 仮想マシンにssh接続
$ vagrant ssh

### 日本語関連のパッケージをまとめてインストールする
$ sudo yum -y groupinstall "Japanese Support"
# sudo 管理者権限で実行(vagrantユーザはパス無しで実行可能)
# -y 自動でyes応答する

### マニュアルの日本語化
$ sudo yum -y install man-pages-ja

ロケールを変更

### 使用できるロケールを確認する
# ロケールとは、言語や国・地域ごとに異なる単位、記号、日付、通貨の表記規則のこと
$ locale -a | grep ja_JP.utf8
# -a 利用可能のロケールをすべて表示する
ja_JP.utf8
# 出力されたらロケールがあるのでOKです。
# 出力されなかった場合は下記で追加
$ sudo localedef -f UTF-8 -i ja_JP ja_JP.utf8
# -f 文字コードを指定
# -i localeを指定

### 設定ファイルを修正
$ sudo vi /etc/sysconfig/i18n

[修正前]

LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"

[修正後]

LANG="ja_JP.utf8"
SYSFONT="latarcyrheb-sun16"

タイムゾーンを変更

### タイムゾーンファイルをコピーする
$ sudo cp /usr/share/zoneinfo/Japan /etc/localtime

### システムクロック変更
$ sudo vi /etc/sysconfig/clock

[修正前]

ZONE="UTC"

[修正後]

ZONE="Asia/Tokyo"

新しくbox化する前に最適化する

### logディレクトリの中身削除する
$ sudo rm -fr /var/log/*

### tmpディレクトリの中身削除
$ sudo rm -fr /tmp/*

### yumのキャッシュ削除
$ sudo yum clean all

### 0埋め
# これをやると仮想ハードディスクの未使用領域をゼロ埋めし、box容量が小さくなる
$ sudo dd if=/dev/zero of=zero bs=4k

### 作られたファイルを削除する
$ sudo rm zero

### コマンドヒストリー削除
$ history -c
# -c コマンド履歴を削除

### 仮想環境をシャットダウン
$ sudo shutdown -h now
Broadcast message from vagrant@localhost.localdomain
    (/dev/pts/0) at 20:32 ...

The system is going down for halt NOW!
[vagrant@localhost ~]$ Connection to 127.0.0.1 closed by remote host.
Connection to 127.0.0.1 closed.

現在の状態を新しいboxとして作成

$ vagrant package
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: /private/tmp/bento/package.box

これで出力された、「package.box」が日本語化された新しい仮想マシンのboxデータになります。

[事後確認・作業]

package.boxが問題なく使えるかどうか確認します。
事前に先ほど作成した、package.boxを仮として作ったjp_bentoディレクトリに移動させておいてください。

$ cd jp_bento
$ vagrant box add jp_bento package.box # vagrant box add [仮想マシン名] boxファイル
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'jp_bento' (v0) for provider:
    box: Unpacking necessary files from: file:///private/tmp/bento/jp_bento/package.box
==> box: Successfully added box 'jp_bento' (v0) for 'virtualbox'!

### Vagrantfile生成
$ vagrant init jp_bento                                                                                                                                                   
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.

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'jp_bento'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: jp_bento_default_1444564033741_63514
==> 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: 22 => 2200 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
GuestAdditions 5.0.6 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /private/tmp/bento/jp_bento

問題なく起動してssh接続できれば完了です。

36
42
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
36
42