2015年10月24日土曜日

[VirtualBox5] CentOS6インストール後の設定

VirtualBox5 で CentOS-6.7-x86_64-minimal.iso を使って仮想マシンを作成してみた。

仮想マシン作成後に行った VirtualBox 関連の作業は、以下のとおり

1.ネットワーク設定


仮想マシンのネットワークはブリッジにしました。
仮想マシンのネットワークについては、下記URLのページが参考になります。
CentOSのインストール直後は、ネットワークが使えませんでした。
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 08:00:27:13:10:2d brd ff:ff:ff:ff:ff:ff
/etc/sysconfig/network-scripts/ifcfg-eth0 をみると ONBOOT が "no" になっていたので、yes に修正しました。
DEVICE=eth0
HWADDR=08:00:27:13:10:2D
TYPE=Ethernet
UUID=a3f14be7-d818-4b1b-807b-755f2edd085c
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
ネットワークを再起動したら、DHCPでIPアドレスが割り当てられて、使えるようになりました。
[root@localhost ~]# service network restart
インターフェース eth0 を終了中:                            [  OK  ]
ループバックインターフェースを終了中                       [  OK  ]
ループバックインターフェイスを呼び込み中                   [  OK  ]
インターフェース eth0 を活性化中:
eth0 のIP情報を検出中... 完了。
                                                           [  OK  ]
[root@localhost ~]# ip a
1: lo: <loopback> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <broadcast> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:13:10:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.40/24 brd 192.168.1.255 scope global eth0
    inet6 fe80::a00:27ff:fe13:102d/64 scope link tentative
       valid_lft forever preferred_lft forever
[root@localhost ~]#

2.Guest Addition のインストール


Guest Addition をインストールすると、いろいろと便利な機能を使えるようになります。
詳しくは、下記URLを参照。
まず、VirrtualBoxで Guest Additions の isoイメージを仮想マシンに挿入します。
[デバイス] > [Guest Additions CDイメージの挿入]を選択します。



仮想マシンにログインして、マウントします。 /dev/cdrom がない場合は、 /dev/sr0 でマウントします。
[root@localhost ~]# mount /dev/cdrom /mnt
mount: ブロックデバイス /dev/sr0 は書き込み禁止です、読込み専用でマウントします
インストーラを実行すると、以下のようにエラーになりました。
[root@localhost mnt]# ./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.6 Guest Additions for Linux............
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
The gcc utility was not found. If the following module compilation fails then
this could be the reason and you should try installing it.

The headers for the current running kernel were not found. If the following
module compilation fails then this could be the reason.
The missing package can be probably installed with
yum install kernel-devel-2.6.32-573.el6.x86_64

Building the main Guest Additions module                   [失敗]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the Guest Additions              [  OK  ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.

gcc, kernel-devel, perl が必要みたいなので、インストールします。
[root@localhost mnt]# yum install perl gcc kernel-devel-2.6.32-573.el6.x86_64
再度、インストーラーを実行したら以下のようになりました。
[root@localhost mnt]# ./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.6 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 5.0.6 of VirtualBox Guest Additions...
Stopping VirtualBox Additions                              [  OK  ]
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [  OK  ]
Building the shared folder support module                  [  OK  ]
Building the OpenGL support module                         [失敗]
(Look at /var/log/vboxadd-install.log to find out what went wrong. The module is not built but the others are.)
Doing non-kernel setup of the Guest Additions              [  OK  ]
Starting the VirtualBox Guest Additions                    [  OK  ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.


GUI は使用しないので OpenGL の失敗は無視しました。

3.ACPIシャットダウンをつかえるようにする


初期状態では、ACPIシャットダウンがつかえませんでした。
acipd をインストールして、起動し、自動起動がONになっていることを確認します。
[root@localhost ~]# yum install acpid
[root@localhost ~]# service acpid start
[root@localhost ~]# chkconfig --list | grep acpid
acpid           0:off   1:off   2:on    3:on  4:on  5:on        6:off
これで、[仮想マシン] > [ACPIシャットダウン]でシャットダウンできるようになります。



4.共有フォルダを設定する


ホストマシンと仮想マシンの間でファイルを共有できるようにします。
この機能を使用するには GustAddition のインストールが必要です。

まず、ホストマシンの Windows にフォルダを作成します。
今回は、フォルダ名を vmshare としました。

VirtualBox で[仮想マシン] > [設定] で設定画面を開き、下図のように 共有フォルダの設定をします。



これで、仮想マシンを再起動すると、ホストマシンのWindows と仮想マシンの CentOSでファイルを共有できるようになります。
上記設定では、仮想マシンが起動すると、自動的にマウントされます。
マウント場所 /media/sf_<フォルダー名> になります。
[root@localhost ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmshare on /media/sf_vmshare type vboxsf (gid=498,rw)
[root@localhost ~]# ls -l /media
合計 0
drwxrwx---. 1 root vboxsf 0 10月 23 22:17 2015 sf_vmshare

ちなみに、ネットワーク設定がNGで通信できない場合でも、共有フォルダでファイルの共有はできます。

5.仮想マシンをクローンした後のネットワーク設定


VirtualBox では、仮想マシンの複製をつくることができます。



同じ MACアドレスのマシンを複数起動すると通信できなくなるので、1台をクローンして複数のマシンを起動したい場合は、上記のように MACアドレスを初期化します。

クローンしたマシンの初期状態では、ネットワークが使用できません。
以下のように、NIC が eth1 として認識されます。
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 08:00:27:ac:86:bd brd ff:ff:ff:ff:ff:ff
/etc/udev/rules.d/70-persistent-net.rules を見ると、以下のとおり。
クローンしたマシンの新しいMACアドレスが eth1 に割り当てられています。
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:13:10:2d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:ac:86:bd", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

自分のMACアドレスを eth0 に割り当てるように修正します。
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x8086:0x100e (e1000)
#SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:13:10:2d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:ac:86:bd", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

/etc/sysconfig/network-scripts/ifcfg-eth0 でも新しいMACアドレスを使うように修正します。 以下のように、UUID と HWADDR をコメントアウトします。
DEVICE=eth0
#HWADDR=08:00:27:13:10:2D
TYPE=Ethernet
#UUID=a3f14be7-d818-4b1b-807b-755f2edd085c
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
これで、仮想マシンを再起動するとネットワークがつかえるようになります。
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:ac:86:bd brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.41/24 brd 192.168.1.255 scope global eth0
    inet6 2408:210:d340:c000:a00:27ff:feac:86bd/64 scope global dynamic
       valid_lft 2591814sec preferred_lft 604614sec
    inet6 fe80::a00:27ff:feac:86bd/64 scope link
       valid_lft forever preferred_lft forever