In this extra part I want to talk a little bit about accessing the Internet from within the VM and installing packages in Gentoo. I will also talk about practically uploading your files into the VM.
Network setup to access Internet
I cannot give a comprehensive lecture on Linux networking configuration and commands in this blog therefore I will just write how you can give outside network access to your test environment.We are going to use a bridge interface if your host connects to the outside network over an ethernet cable. We are going to use NAT if you are using a laptop with wireless access like me. (Because wireless interfaces and ethernet bridges don't work by default for various reasons)
Ethernet Bridge
Most common networking setup in a Linux virtualization host is connecting the guests to the outer network over a bridge interface. If we were using Xen or libvirt/KVM our default VM setup would be like that. However we are using raw QEMU and we get to do the necessary preparations.Let's assume you are accessing your gateway over eth0, the network interface connecting to the guest is tap0 and everyone takes IP addresses via DHCP.
You need to find a way to create a bridge, add eth0 to that bridge and let the bridge take an IP address over DHPC instead of eth0. You will also need to install bridge-utils package for the bridging utility brctl.
For example you can learn how to do that in Ubuntu here. You will need to write the following lines in /etc/network/interfaces (make sure to take a backup of the file just in case before editing):
auto eth0 iface eth0 inet manual auto br0 iface br0 inet dhcp bridge_ports eth0 bridge_stp off bridge_fd 0 bridge_maxwait 0
You should test this by restarting network
sudo /etc/init.d/networking restart
When you issue ifconfig or ip addr you should see that the IP address is in br0 interface, not eth0. Also you will notice that you can no longer manage eth0 from Ubuntu's network-manager program.
Once you have your bridge handled by Ubuntu you should create a file to automate qemu side. Create /etc/qemu-ifup and write the following to make it add tap0 interface to the bridge everytime.
#!/bin/bash ip link set $1 up brctl addif br0 $1
You must do one critical thing here which can break your computer. Open the /etc/sudoers file using the program visudo and edit it. If you write something wrong you may lose your sudo rights and never be able to execute commands with root privileges (and you will need to rescue your system by booting up with a rescue USB or CD).
Write the following into Cmnd aliases section in /etc/sudoers file:
Cmnd_Alias QEMU_NET_SH = /etc/qemu-ifup your_username ALL=(ALL) NOPASSWD: QEMU_NET_SH
Write the username you use in the system instead of your_username and qemu will automatically do network stuff as if you did by using sudo command.
You can just skip this and configure network by running the qemu-ifup script manually.
WLAN NAT
If you connect to the internet over wireless like me then you will realize that you can't add wireless interfaces to ethernet bridges. We can however do NAT to let packets from ethernet to move to wireless interface (and the other way around). Create /etc/qemu-ifup and write the following:#!/bin/bash sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE sudo ip add address 192.168.1.1/24 dev tap0 sudo ip link set tap0 up sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward" sudo /etc/init.d/dnsmasq start
Here wlan0 is your wireless interface with whichever IP address your wireless router gave you. You add another IP address for the host and enable host side interface in next two lines. (It should be different if your router is working on 192.168.1.0/24 network to prevent confusion).
This way the host is connected to two different networks at once. One is the wireless network and the other one is virtual network between host and the guest. To enable packet transmission between the two you need to enable Linux's router mode by enabling ip_forward in next line.
Finally you need to setup dnsmasq to your computer so that guest can ask your computer for DNS queries and your computer can ask them to the router in turn. In Ubuntu you can just apt-get dnsmasq package and the edit /etc/dnsmasq.conf file. If you are using ethernet bridge you should uncomment "#interface=" and add br0 to it. If you did WLAN NAT then you should add the IP address of tap0 device to listen address list: "listen-address=127.0.0.1,192.168.1.1"
Finally you should configure the IP address of the guest. You can checkout Gentoo's network config page. Basically you need to create a file, /etc/conf.d/net.
If you are using ethernet bridge it should have one line:
config_enpXsY = "dhcp"
where enpXsY is your ethernet interface as Gentoo names it.
If you use WLAN NAT, then you probably want to give the guest a static IP address. That is done by writing the following in /etc/conf.d/net.enpXsY:
config_enpXsY="192.168.1.41/24" routes_enpXsY="default via 192.168.1.1" dns_servers_enpXsY="192.168.1.1"
Here you assign the IP address in the first line and give default gw and DNS server info in second and third lines.
After writing /etc/conf.d/net you need to write the following to enable the config.
rc-update add net.enpXsY default
Now you should be able to ping google.com inside the guest.
To install new packages in Gentoo you use emerge command (part of portage).
To install evtest for example you type: emerge evtest
To list packages which needs update: emerge -uDp world
To update: emerge -uD world
Finally, a practical method to upload files and download files from the guest. You can use scp just like any other computer with ssh.
Enable sshd in guest:
rc-update add sshd default
Create and upload an rsa key to the guest with an empty passphrase:
ssh-keygen #enter empty passhphrase ssh-add id_rsa scp .ssh/id_rsa root@192.168.1.41:/root/.ssh/authorized_key #enter guest root password
Now guest won't ask for password when you ssh or scp to it. You can copy files to and from the guest from host.
scp test.txt root@192.168.1.41:/root/
No comments:
Post a Comment