Virtualbox: Difference between revisions

From 太極
Jump to navigation Jump to search
Line 339: Line 339:
=== How to setup a LAMP development environment ===
=== How to setup a LAMP development environment ===
https://www.howtoforge.com/tutorial/vagrant-ubuntu-linux-apache-mysql-php-lamp/
https://www.howtoforge.com/tutorial/vagrant-ubuntu-linux-apache-mysql-php-lamp/
However, I got an error complaining password in the configuration. See [https://github.com/mitchellh/vagrant/issues/1295 this post] for help.


== Headless VirtualBox with phpvirtualbox ==
== Headless VirtualBox with phpvirtualbox ==

Revision as of 20:32, 5 March 2015

Installation on Ubuntu

http://www.ubuntugeek.com/virtualbox-4-3-8-released-and-ubuntu-installation-instructions-included.html

Check if hardware (CPU) supports virtualization

egrep -c '(vmx|svm)' /proc/cpuinfo
  • If 0 it means that your CPU doesn't support hardware virtualization.
  • If 1 (or more) it does - but you still need to make sure that virtualization is enabled in the BIOS.

If your CPU does not supports hardware virtualization, you can only install 32-bit virtual machines (not 64-bit).

Virtualization in the host machine

Note that even the host machine is 64-bit, if the vT is not available/enabled in the BIOS, we cannot install 64-bit OS in the guest machine.

Virtual box guest addition

Install guest addition for virtualbox on Debian See the link [1]

We can check the extension pack version or if it was installed by going to the File -> Preferences -> Extensions.

virtual machine software benchmark

Trouble Shooting

Forum

Old builds

When it is impossible to find a solution for some unknown errors when installing a new build of VB, it is worthy to go back to install an old build of VB.

The old build & extension pack of VB can be downloaded from https://www.virtualbox.org/wiki/Download_Old_Builds.

For example, I always got an error 'supR3HardenedWinReSpawn'. The log file shows something like

13d8.c54: supR3HardenedMonitor_LdrLoadDll: error opening 'C:\Windows\system32\wintab32.dll': 0 (NtPath=\??\C:\Windows\system32\wintab32.dll; Input=C:\Windows\system32\wintab32.dll)

The only solution is to install an old version of VB.

the headers of the current running kernel were not found

sudo apt-get install dkms build-essential linux-headers-$(uname -r)

virtualbox failed to create the host-only network

Virtualbox (*.deb) was downloaded from virtualbox website. However, when I want to create a host-only network, I have to execute

sudo /etc/init.d/vboxdrv setup
sudo apt-get install dkms build-essential linux-headers-$(uname -r)

This also solves the error "Kernel driver not installed (rc=-1908)".

Ubuntu 14.10

Especially, if my host is already Ubuntu, I want to use RightCtrl + F1 and RightCtrl + F7 to switch between the terminal and the graphical desktop. If my host is Windows OS, I can use Ctrl + Alt + F1 and Ctrl + Alt + F7 to switch.

Use VirtualBox

Different networks

See diagrams on https://blogs.oracle.com/fatbloke/entry/networking_in_virtualbox1

  1. NAT
  2. Bridge
  3. Host Only: Host can access guest, but not the other direction. Outside cannot access guests. Good for running a test web server.
  4. Internal Network: Guests can access each other but host still cannot access guests. This is good for testing when you need a separate, clean network, and you can create sophisticated internal networks with vm's that provide their own services to the internal network. (e.g. Active Directory, DHCP, etc).

Share folders

Suppose we want to share some folder from host to guest. Assume in Virtualbox, we use "Downloads" as the folder name in Share Folders dialog.

Method 1: Use Auto-mount option built in VirtualBox. But I get a permission denied error. The reason is we have to add the current user to the group vboxsf.

sudo usermod -a -G vboxsf USERNAME
# reboot is required
# the shared folder is under /media/ with sf_ as prefix.

See http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/ for more examples of using usermod command.

Howtogeek.com provides another way to add user to vboxsf.

sudo adduser USERNAME vboxsf

We can then use id USERNAME to verify you are in the vboxsf group.

Method 2: We can use the following method to get an access to Downloads folder for this session only. See also howtogeek.com.

sudo mkdir /mnt/Downloads
sudo mount -t vboxsf Downloads /media/downloads

Method 3: We can use the following method for permanent solution.

sudo nano /etc/fstab
sudo mkdir /mnt/Downloads
# Add a new line
# Downloads /mnt/Downloads/ vboxsf defaults 0  0  
sudo mount -a

PS: There is a bug in virtualbox 4.3.10 which results an error

mount: wrong fs type, bad option, bad superblock on hostshare,
       missing codepage or helper program, or other error
       In some cases useful info is found in syslog - try
       dmesg | tail or so

The solution is to run

sudo rm /sbin/mount.vboxsf
sudo ln -sf /usrs/lib/x86_64-linux-gnu/VBoxGuestAdditions/mount.vboxsf /sbin/mount.vboxsf

Seamless mode

If we want to run the app from guest machine on host machine (to hide the guest machine desktop) so the app looks like from the host machine, we can head to View->Switch to Seamless Mode in VirtualBox.

Snapshots

If we are installing a sketchy program, we can take a snapshot, install the program, then revert to your snapshot if things go wrong.

http://www.howtogeek.com/150258/how-to-save-time-by-using-snapshots-in-virtualbox/

Create a snapshot:

  1. Create a clean machine.
  2. There are two ways:
    • (When the VM is off) Switch from 'Details' to 'Snapshots' on the right-hand side of the VB window. Right click 'Current State' and click 'Create Snapshot'.
    • (When the VM is on) Machine > Take a snapshot. Give it an intuitive name and description. Shutdown the current machine (This seems to be a good practice every time after we create a VM).

After we open an original snapshot and do not nothing, when we shutdown the VM, the VM status is 'changed'.

Restore a snapshot:

  1. Select the Virtual machine from the list and switch over to the snapshots view (top-right corner). Right click a snapshot and choose 'Restore snapshot'. Uncheck the option of 'Create a snapshot of the current machine state'. Click 'Restore'.
  2. You should see the "Current State(changed)" will become "Current State" the same as the snapshot you selected to restore to.

Port forward

If you’d like to run server software inside a virtual machine, you’ll probably want to set up port forwarding so the server software is reachable from outside of the virtual machine. You’ll find this option in the Network settings pane, under Advanced.

If we change to use the 'Bridged' network, we don't need to use port forward. We use 'port forward' if we use the default 'NAT' network.

http://www.howtogeek.com/122641/how-to-forward-ports-to-a-virtual-machine-and-use-it-as-a-server/

Increase the guest machine space

Boot virtual machines from USB drives

http://www.howtogeek.com/187721/how-to-boot-from-a-usb-drive-in-virtualbox/

Clone vs Import/Export appliance

Clone will create a new VM directory (including *.vdi) on the same machine.

Import/Export appliance (*.ova) allows to transport the appliance to a new machine.

  1. The appliance will not be touched on the new machine.
  2. The storage space cannot be adjust when we import an appliance.
  3. The .ova file can be opened in VMware Play.

Start virtual machine when the system boots up

Manage virtual machines

VBoxManage

vagrant

Vagrant boxes

Why vagrant

Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.

To achieve its magic, Vagrant stands on the shoulders of giants. Machines are provisioned on top of VirtualBox, VMware, AWS, or any other provider. Then, industry-standard provisioning tools such as shell scripts, Chef, or Puppet, can be used to automatically install and configure software on the machine.

Tested on an Ubuntu host machine

$ sudo dpkg --install vagrant_1.7.1_x86_64.deb 
[sudo] password for mli: 
Selecting previously unselected package vagrant.
(Reading database ... 209831 files and directories currently installed.)
Unpacking vagrant (from vagrant_1.7.1_x86_64.deb) ...

$ vagrant init hashicorp/precise32  # No 'sudo' is needed
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.

$ cat Vagrantfile

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'hashicorp/precise32' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'hashicorp/precise32'
    default: URL: https://atlas.hashicorp.com/hashicorp/precise32
==> default: Adding box 'hashicorp/precise32' (v1.0.0) for provider: virtualbox
    default: Downloading: https://atlas.hashicorp.com/hashicorp/boxes/precise32/versions/1.0.0/providers/virtualbox.box
==> default: Successfully added box 'hashicorp/precise32' (v1.0.0) for 'virtualbox'!
==> default: Importing base box 'hashicorp/precise32'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Setting the name of the VM: Downloads_default_1420231386363_57276
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> 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: 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 its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> 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: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
    default: /vagrant => /home/mli/Downloads

$ vagrant destroy

More

$ vagrant box add ubuntu/trusty64
$ vagrant up
$ vagrant ssh
$ vagrant destroy
$$ ls /vagrant

# Web server
$ nano bootstrap.sh
$ nano Vagrantfile # add provision
$ vagrant --provision
$ vagrant ssh
$ nano Vagrantfile # add port forward
$ vagrant reload

# Share
$ vagrant login
$ vagrant share
Ctrl + C

# Teardown
$ vagrant suspend
$ vagrant halt
$ vagrant destroy

# Provider
$ vagrant up --provider=vmware_fusion

Install on a Windows machine

Note: If we want to use Vagrant with Virtualbox, we need to install Virtualbox separately. Vagrant does not include Virtualbox in its installer. Vagrant's installer (~150MB) only installs itself.

Go to the Download page to download Windows version (no separate 32 or 64-bit versions). The program will be installed onto C:\HashiCorp directory.

I can install Virtualbox and Vagrant on a virtual Windows 7 guest machine. This is a great advantage of vagrant over docker in that I cannot run (installation is fine) dock2boot on a Windows 7 guest machine (Docker requires a real Windows to work). But it is perfect OK to install and run docker on an Ubuntu guest machine.

After installing virtualbox & vagrant, vagrant will be available on a global environment. That said, we can open a command window and start to run 'vagrant'.

C:\Users\brb>vagrant init hashicorp/precise32
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.

C:\Users\brb>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/precise32'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Setting the name of the VM: brb_default_1421961025717_91472
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
...
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/brb

C:\Users\brb>
C:\Users\brb>vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:

Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: C:/Users/brb/.vagrant/machines/default/virtualbox/private_key

C:\Users\brb>

The next step is to use Putty to connect to the virtual machine. We can follow the instruction at https://github.com/Varying-Vagrant-Vagrants/VVV/wiki/Connect-to-Your-Vagrant-Virtual-Machine-with-PuTTY. However, the private key used for conversion is not the one as described in the article. We should uses the private key indicated in the above message (C:/Users/brb/.vagrant/machines/default/virtualbox/private_key in this case).

Note that when we run 'vagrant up', we will see a new VM was created in VirtualBox and once we run 'vagrant destroy', the VM will be deleted.

If we run 'vagrant destroy' and then 'vagrant up' again, the old private key will not work in the new guest machine.

Sharing a Windows/host folder in Vagrant vm

See the tutorial video on youtube. In this example, an apache is created in vm. The goal is to install Apache in the vm and use a folder on Windows/host system as the DocumentRoot.

First we edit Vagrantfile, uncomment 'config.vm.network' line, and modify ip as anything you want. After we modify Vagrantfile, we can issue 'vagrant reload' to make the change effective immediately.

Now use 'vagrant ssh' to ssh to the vm and install apache2, ... and start apache2.

The interesting thing is if we create a new subfolder (/vagrant/www) in the vm, this folder will appear in the Windows/host system as well. If we change /etc/apache2/sites-available/default file AND modify DocumentRoot to /vagrant/www, <Directory> to /vagrant/www, and AllowOverwrite to None, we will be able to use the www subdirectory from the Windows/host system as the DocumentRoot for the Apache in the vm.

Use vagrant with Docker (instead of VirtualBox) provider

Use vagrant to run a VM remote desktop with desktop environment

How to setup a LAMP development environment

https://www.howtoforge.com/tutorial/vagrant-ubuntu-linux-apache-mysql-php-lamp/

However, I got an error complaining password in the configuration. See this post for help.

Headless VirtualBox with phpvirtualbox

http://www.howtoforge.com/managing-a-headless-virtualbox-installation-with-phpvirtualbox-ubuntu-12.04

For some reason, I got an error 'Could not connect to host (127.0.0.1:18083)'. A solution is to restart it. See this wiki from sourceforget.net.

sudo /etc/init.d/vboxweb-service stop
sudo /etc/init.d/vboxweb-service start

Other visualization software

Hypervisor/Virtual machine monitor

Hypervisor from wikipedia.

VMware

apt-get update
apt-get install build-essential module-assistant
m-a prepare
cd Downloads
tar xzvf /media/cdrom0/VMwareTools-9.2.0-799703.tar.gz
cd vmware-tools-distrib
./vmware-install.pl
[Accept all default]
shutdown -r now

Convert between vmdk and ova format

See this article in howtogeek.com

vSphere

This is a type 1 hypervisor.

oVirt

Emulation vs virtualization

Virtualization allows users to configure virtual machines outfitted with similar hardware and processors as the host system, whereas emulation simulates different hardware and processor configurations in software.

Emulators are what allow you to run old Commodore 64 or NES games on a PC, for example.