Thursday, November 7, 2013

安装Virtual Box

Reference :
http://www.howopensource.com/2013/04/install-virtualbox-ubuntu-ppa/


1) sudo apt-get install dkms
2) wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
3)sudo sh -c 'echo "deb http://download.virtualbox.org/virtualbox/debian raring contrib" >> /etc/apt/sources.list.d/virtualbox.list'
4) sudo apt-get update
5) sudo apt-get install virtualbox-4.2



Manage VirtualBox using command line(http://www.howopensource.com/2011/06/how-to-use-virtualbox-in-terminal-commandline/)

check groups:
$cat /etc/group
add user to vboxusers group:
$ sudo usermod -G vboxusers -a 'leiming'

Download and install extension pack. https://www.virtualbox.org/wiki/Downloads

/----------------- Headless VM launching  --------------/
to power on,
$VBoxManage startvm "Ubuntu1204" --type headless

to power off,
$VBoxManage controlvm "Ubuntu1204" poweroff

get the complete list of options with:
$VBoxManage controlvm

/* You don't need this section
In virtualbox4.2 menu, select File->Preference->Network, click add(+) host-only networks, and select OK.
Then, for the Setttings, go to Network->Adapter2, select "Host-only Adapter"
*/

Setup the forwarding like below:
$VBoxManage modifyvm "Ubuntu1204" --natpf1 "host2guest-ssh,tcp,,2222,,22"

login usign port forward
$ssh -p 2222 guest_username@localhost

To avoid type passwd every time, do the following
$ssh-keygen -t dsa
$cd ~/.ssh
$cat id_dsa.pub | ssh -p 2222 vm@localhost 'cat - >> ~/.ssh/authorized_keys'



%-------------  Debian ----------------------%
$su then $apt-get install/update xxx
$/sbin/ifconfig

We need to edit the /etc/apt/sources.list and comment out the line beginning with “deb cdrom …”
Using vi:     # vi /etc/apt/sources.list

find out the display amanger: $ cat /etc/X11/default-display-manager

auto login on lightdm xfce
$ su
$vim /etc/lightdm/lightdm.conf
change find  the following lines, and change according to your need. Note that there should be no space between equal sign and the username
autologin-user=username
autologin-user-timeout=0

%---------------------------------------------------%



VirtualBox Images  =>                   http://virtualboxes.org/images/
The format is vba , no vbi. This causes failure.

To enable the clipboard copy/paste selection  on both host and guest, go to VirtualMachine's Settings->General->Advanced->Shared Clipboard (Bidirectional).


Now I have Debian and Ubuntu12.04 setup in the VirtualBox 4.2.
Next, we are going to do Interaction between Host and Guests(vms).



//----------------------------------------------  send email via command line ----------------------------------------//
configure in ubuntu 13.04/12.04 (http://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/)

1) sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
2) sudo vim /etc/postfix/main.cf
3) add the following lines:
relayhost = [smtp.gmail.com]:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_CAfile = /etc/postfix/cacert.pem smtp_use_tls = yes

4) open and create the following file
sudo vim /etc/postfix/sasl_passwd
add following line:
[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD

5) Fix permission and update postfix config to use sasl_passwd file:
sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

6) validate certificates to avoid running into error
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

7) Finally, reload postfix config for changes to take effect:
sudo /etc/init.d/postfix reload

8) Test.
echo "Test mail from postfix" | mail -s "Test Postfix" you@example.com


Bugs:
You may need to $ sudo /etc/init.d/postfix restart, if you have error like  "the Postfix mail system is not running".
If the error continues,  check the end of /var/log/syslog for errors. If you see something like below,
"fatal: bind 0.0.0.0 port 25: Address already in use "
you need to unintall/stop sendmail. (sudo apt-get remove sendmail-*) Reboot!


//-------------------- how to install guest additions --------------------------------//
step1: First start up your Ubuntu guest OS and then click on Devices and choose Install Guest Additions.

step2:  In terminal, $cd /media/, find the VBADDITIONS folder, step in.  Since we are running in Ubuntu, so type $sudo ./VBoxLinuxAdditions.run
(the installer will create a few new folders, etc and install Guest Additions.)

step3:  Restart. When Ubuntu loads up, it will display a message stating that the guest OS supports mouse pointer integration. In my case of 1204, it didn't show, but you can notice the cdrom is loaded with the addtions image. So mission completed! (http://helpdeskgeek.com/linux-tips/install-virtualbox-guest-additions-in-ubuntu/)

=>Now we can enjoy the "VBoxManage guestcontrol"


// How to run script automatically when login Ubuntu13.04?
Solution1: add an @reboot cron task
1) run $crontab -e
2) add a line "@reboot /path/to/script" in the edit mode of your cron.
Done!

// How to run script as root in Ubuntu?
sudo vim /etc/rc.local
add the script before 'exit 0',
example as below
...
sh /usr/local/bin/my_script.sh
exit 0



// How can a VM run an infinite loop to monitoring status/request from host side ?
(reference:    http://linuxcommand.org/wss0160.php)
sudo chown root:root /home/vm/fromHost.sh
sudo chmod 700 /home/vm/fromHost.sh

sudo visudo, add following line after %sudo ALL=(ALL:ALL) ALL
vm  ALL=(ALL) NOPASSWD: /home/vm/fromHost.sh

write a python script to call fromHost.sh, like below
#!/usr/bin/env python

import os

def main():
os.system('sudo /home/vm/fromHost.sh')

if __name__ == '__main__':
main()

Finally, we can interrupt the infinite loop(run_test1.sh) by sending an SIGINT from fromHost.sh, which is triggered from host running fromHost.py in VM.


//----------------------------------------------To-do list---------------------------------------------------------------//
[x] host shell script to start/execute/check/end VMs
[x] script run at os startup as root
[x] listen to commands/message from client ?
[?] an infinite loop to check latest SVN and updates, pause if there were errors