Saturday, July 22, 2017

VM networking with VirtualBox on Ubuntu

Last week just finished an awesome training session on docker. Granted our corporate networks were sort of a hurdle for the training. But VirtualBox enabled us to carry on with the training. So I went home and installed virtualbox on my ubuntu machine; created a few VMs and thought I could get them networking. To my dismay I couldn’t get my host to talk to any of my guest virtual machines…

So I tinkered around and finally narrowed down the minimal stuff you need to do.

Here are some assumptions before going forward:
  1. You have a freshly installed virtualbox in ubuntu
  2. You have gone ahead with the creation of two (or more) virtual machines. Ultimately you need two to start with. Whether you have cloned virtual machines or created from scratch is irrelevant.
  3. You have at least tried to ssh from your host machine to the guest. So the assumption is that you have installed some ssh server on the guest machine.
  4. (This may not be necessary) You may have tried adding another network adapter - a host-only adapter, but you find that you cannot set the adapter name.

Scope of this post

I am only going to show you networking between the host and the guest machine. These steps should enable guest-to-guest networking in the host. Now if inside a VM you can’t connect to the internet…I can’t help you there. So hoping that is not the case.

Further, this post only deals with plain host to guest networking and vice versa. It does not cover how you can make those guest machines participate in the host VLAN network. In other words, it does not cover networking between other hosts in the same VLAN network as our specific host, with these guest machines.

Getting started

1. Setting up the host-only adapter.

Chances are you need to explicitly setup this one.

1.1 Finding the host network interface

You need to do this on your host and the guest machine.

Type the following

ls /sys/class/net

This will list the interfaces on your machine. Its a good idea to do this on both machines. However, on the host machine, this helps us in figuring out whether virtualbox has created a network interface for you. Suppose your host gave you an output like follows:

$ ls /sys/class/net
docker0  eth0  lo  wlan0

It probably means VirtualBox isn’t running…
VirtualBox hasn’t created a network interface for you.

If latter is the case, you can re-verify this (assumption #4)

If however, virtualbox is running and you see an entry like vboxnet0 - I guess you are okay and you can skip the next step

1.2 Setting up the VirtualBox network interface.

This is fairly simple. Follow the steps given here ->

2. Setting up an extra network adapter for your virtual machine.

We need a way to tell VirtualBox to setup an interface for our virtual machine. VirtualBox will give it a unique IP address via which you can at least begin to ping the guest machine. But this is not so straight forward.

Make sure you have powered-off your VM.

(While you are at it, you can also verify with exercise 1.1 for the host - if there is no network interface - you are in a big soup)

Follow the steps here to add the extra network adapter ->

After this boot into your VM (in shell) and login as root

Hopefully based on the VM you are using there is probably a command to list the “active” network interfaces - Say if you are using ubuntu its ifconfig; if its a pure debian based os its ip address.

Go ahead and try the command out.

Note, the emphasis is on “active”. Your OS will detect your network interfaces, but won’t activate all of them. In other words, you can list the set of interfaces your OS is aware of. But you have to take the additional effort of activating them.

How to list all the network interfaces? See exercise 1.1

3. Activating the VM network interface in the guest

So you are inside your VM.

(Just for the heck of it. Simply verify exercise 2 was done correctly, that means carry out exercise 1.1 for the guest machine).

So the output of exercise 1.1 (on the guest) looks like this:

$ ls /sys/class/net
eth0 eth1 lo

Listing active interfaces (ifconfig) will probably have a private ip for eth0. There won’t be an entry for eth1. Here is how to set it:

Using a text editor like vi or nano, edit the /etc/network/interfaces file append the following lines to the file

# this is the secondary interface
auto eth1
iface eth1 inet dhcp

Now type

$ ifup eth1

If all goes well you will see and output the last line of which says you’ve obtained a lease of an ip address.

Verify again with command to list active network iterfaces

$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:91:02:83  
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::a00:27ff:fe91:283/64 Scope:Link
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1      Link encap:Ethernet  HWaddr 08:00:27:70:A2:B5  
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::a00:27ff:fe70:a2b5/64 Scope:Link
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

At this point, from the host you should be able to ping the guest machine. You can do a similar set of exercises for the other VM, and then ping the 2nd VM from the first one. But that is left to you as homework.

Hope this article helps you getting started with networking. Now you are hopefully ready to experiment with docker swarm at least.

How to add a new network adapter to an existing VM (Oracle VirtualBox)

This same post is available here ->

If for what ever reason you cannot access the above, continue reading this post.

This post is only meant to supplement this post.

Step 1: open the settings dialog for your VM

Step 2 : go to the network option in the sidebar - click on the Adapter 2 tab

Step 3: on the adapter 2 tab, notice that the UI is disabled. We have to check the checkbox to specify this adapter

step 4: after you've done so we have to specify a value for "Attached to:" - Select "Host only Adapter" here

Step 5: Select a value for the "Name" select field. Notice it says vboxnet0 - that is the default. That is it. Click OK and you are done. Now power-on your VM and verify if the adapter is detectable by your OS

Thursday, February 2, 2017

learn programming (or to become a programmer)

Programming is an interesting exercise. Sometimes at work, I discover a problem. And a good chunk of my time goes into figuring out a solution to fix it. When I discover a challenge in the piece of code I am writing or the solution I am implementing, it becomes more exciting.

But programming isn't everyone's cup of tea. There are good many articles and forum posts which say what the programming profession is and if programming is the right job for you. Here are few links to that effect:

I highly recommend you continue reading this post after visiting the above links.

Here are some less-mentioned guides about how to go about learning programming:

1. The more you realize what a computer actually is, the more comfortable you might be with learning how to write programs. Its not really writing code or compiling more of figuring out the answer to why write programs?

2. It also helps to think of a computer as an IO (input/output) machine. It is designed to accept certain kinds of input, do some processing and calculations, and output to a set of destinations. The most common of destinations is a computer terminal (or a computer monitor).

3. Get a general sense of how software works? Is it interactive? Does it require human interaction? If the software does not need human interaction, and it works, how the hell does it work?

4. Its not about a language, or the best language. The language is only a vehicle for you to get programming. The language alone doesn't tell the computer what to do. But it is important to figure out what the computer is capable of...and how that language can achieve all that. Step 3 is sort of important here. As you do this, you slowly figure out how a computer actually works.

Now if all this sounds like something you are not bound to like...then don't get into programming. Its not your thing. There are loads of other career choices to make - it need not have to be related with what you studied in college. But it has to be something you enjoy doing.

If you are a novice programmer probably the best place to get started might be
CodeAcademy. Most of the programming languages come with a developer environment (or an IDE). Here you can learn any language without that hassle. If you are just starting out, keep the next thing as a to-do item at the back of your mind...probably when you achieve a certain level of proficiency writing simple programs you can have a go at this: find an answer to what makes a web application different from a desktop application?

All the best.

Tuesday, January 3, 2017

konva js - a fork of kineticjs - a javascript 2d graphics/animation library

I've been exploring KonvaJs for a while. And I like it. I've done some pretty awesome animations using this library too.,output

The more you use the library, the more you'd want to make a custom shape. And there is a solution for that. However, that talks about manually drawing shapes using the 2d canvas primitives. But, you'd also want to re-use what is already offered by the library. Konva docs currently don't have a say about how to do this.

I may have found a hack. It may not be elegant when you think it. But it will do...

Here is my git repo if you are interested in code for an example.

Ps: you'll need npm and gulp to run it.

Sunday, June 19, 2016

a web interface to mongo? good or bad idea?

Its a bad idea.

Been exploring mongo db recently. I had experimented with it before briefly. But recently it was more of a quest to get a question answered.

Why build a HTTP interface to MongoDB?

You can scale without the overhead of installing mongo db driver software. It does not sound all that cool, but I guess that it is the only benefit.

Otherwise it isn't a good idea to expose it. Imagine, if that particular client (which talks to your database via http) is given to the end-user; people could potentially corrupt data.

That being said. I still like the former idea. I had recently written an email server in c# that generated enormous amount of logs. Plus, it also had to serially pump data somewhere. I just fancied the idea: why not do it over http?

However, I decided I won't go that road. I still fancy the idea, however, I do feel its not efficient or fast enough. Because you want to minimize the number of operations first hand. If I did it over http, the cycle of operations go like:

  1. Establish socket connection to the target server
  2. Acquire the socket stream
  3. Write data (to send) to stream
  4. Flush the stream
  5. Close the socket
In C# (or any other framework of choice) most of these are abstracted by the libraries I use; hence I don't have to do everything, just I've to ensure I compose the data, and call the http api's correctly. But inevitably those 5 things happen beneath the hood. 

I rather opt for a connection oriented approach. Something in the lines of:
  1. Open socket
  2. Acquire streams
  3. Reuse those streams as many times as needed
  4. Dispose of those streams when done and close the socket.
This eventually means I've to ensure that the driver libraries are bundled properly with my app. But for now that is just the road to go...

I did however, come across a python implementation of a http interface to mongo db (from their website itself). Immediately found out it was broken. And I am a bit disappointed. People say, that when there is no recent commit on a github source repository, it eventually means that that software is obsolete. They are usually right...

However, there is an intent. A curiosity I have to answer. Hence I forked the repo. Hacking up the source to make stuff work. So far I've understood why its broken, and I've taken small steps to fix it too. But that isn't my objective though.

Friday, March 6, 2015

Matrix permutation

I came across this interesting problem when I was solving a coursera assignment (8-puzzle) on Algorithms & Data Structures. This isn't related to the 8-puzzle where you need to visualize the solution as a game tree data structure.

I think this is related to the game tree.

Consider you have a \(2 \times 2\) grid where there is an \(X\) in one of the squares. You can make children of that figure only by moving \(X\) it to the adjacent square either left, right, top, bottom. How many iterations do I need to ensure that \(X\) has been through all the locations. It is possible that you can get the \(X\) in a previous position by making a child of child (from the previous iteration: avoid such repetition.

Here is a figure that shows the case for the grid in question:

This is a very trivial example. What could be the solution for a larger grid?

Wednesday, February 25, 2015

writing code for doing combinations

Combination is a common problem in mathematics which deals with different ways via which we can select r objects given n, where r < n. So we have n distinct objects and we can't have repetitions. For e.g., if we are to select 3 letters from the string ABCD, we have the following


So there are a total of 4 ways of selecting 3 objects.

Perfectly follows the formula,
\[{n \choose r} = \frac{n!}{r! \times (n-r)!}\]
A more simplified version of the above formula is,
\[{n \choose r}= \frac{n(n-1)(n-2)...(n-r+1)}{r!}\]
This is actually better for computations.

But how to write code to output the various selection as we've done above?

I'd take to pseudo code but you can't implement the pseudo code in one language and see the results immediately. Hence I've taken to python. I am not python-biased...I just like the simplicity of expressing logic. It models pseudo code fairly well...

There are better ways to write can use a generator instead of a loop that creates and returns an array. "Generator" is a concept that is available on most of the popular programming languages. You can explore my other gists to see how this is done in python and java.

Meanwhile, happy programming.