Mounting VirtualBox VDI images (and similar formats)

Sometimes you have a .vdi (or .vmdk or similar) virtual machine disk image, and you wish to access files inside, without starting the virtual machine itself and copying files via network or some ‘guest additions’ tools provided by the virtualization software.

To do this, you need nbd support enabled in the kernel (and module inserted) and qemu installed (which is actually another virtualization software, but we need it for the qemu-nbd utility).

In some distributions, the nbd module is enabled by default, on some you have to enable it when compiling the kernel sources.

Due to some weird reasons, the nbd module by default supports zero (0) partitions inside a block device:

So when inserting the module, set the parameter to a larger number (eg. 4 or 16 or whatever)

After that, you use the qemu-nbd utility to ‘connect’  the vdi image to the nbd block device (qemu-nbd actually works as a block device server), mount the partition, do stuff, umount the partition, and disconnect the qemu-nbd:


Checking how much data still needs to be written when sync-ing on Linux

Sometimes on Linux, when you copy alot of data to a slow USB flash drive, the operation is “done” in a second, but when you umount the drive (or run ‘sync’) it takes forever to write the cache to the actual device.

So, how to check the status (how much time is left, how much does it still need to write)?

The answer hides in /proc/meminfo. Specifically the “Dirty” and “Writeback” lines in that virtual file. Very simplified, the “Dirty” value tells you how much data is waiting in cache, and the “Writeback” tells you how much data is being currently written to the device (chunk size).

You can check both values in semi-realtime with the following command:


Measuring frequency with a BusPirate

I recently ordered a NE555 pulse generator from DX with some evil plans in mind, and I had to set the frequency to the desired value. Since calculating the frequency by hand and guessing the number of turns on the cheap pots was not an option, I had to measure it.

Since my multimeter only goes to 1000Hz, I decided to use my BusPirate (it goes to a couple of MHz). I’ve connected the GND and 5V leads to GND and VCC pins on the module, and the AUX lead to the modules output (BusPirate measures frequency on the AUX pin/lead).

BusPirate + ne555
BusPirate + ne555

After connecting the BusPirate to my computer via USB, and opening the serial port (“/dev/ttyUSB0”, 115200 8N1 in my case), i see the BusPirate interface. By default it stays in “HiZ” mode after being plugged in (high impedance mode on all pins – to protect the connected circuits). Pressing the ‘?’ key and Enter, you get a help printout:

If i press the ‘f’ key and Enter the frequency measurement starts, but it measures 0Hz (since the power supply to the module is off). To turn the power supply on, i need to use the “W” key, but this doesnt work in HiZ mode:

To use the power supply, I had to select some other mode (not HiZ) by using the “m” key, so I picked UART (3), and some default speed settings (which don’t matter now, since I wont actually use UART).

In UART mode, I can now turn the 5V power on, and measure the frequency:

Then I’ve tweaked the potentiometers, ran the measurement (“f” + Enter) again, and repeat until the desired frequency is set.

Browsing the Slovenian vehicle database

Slovenian “National Interoperability Framework” (NIO) published a database with data on every registered vehicle in Slovenia, so I decided to do some fun queries. (Query results are in Slovene, but i will try to translate the most interesting ones)

How many registered vehicles are in Slovenia?

What about different vehicle types?

Common manufacturers (cars only):

Since data entry is done at registration points by hand, you see “MERCEDES BENZ” and “MERCEDES-BENZ”  etc. as different manufacturers.

How many Lamborghinis (all vehicle types, all spellings)?

Nice! Must be alot of rich people here.. but lets check the vehicle type first:

Okay, maybe not. Lets check the not-tractors:

Nice, 4 actual Lamborhini cars.

What about some demographic data – how old are the owners (Starost == age)?

In Slovenia it is legal for a minor to own or inherit a vehicle, but to register it, an adult “user” with the appropriate licence must be assigned.

Which car does to youngest car owner have? (not sure if it’s inherited or a data entry error – age:=1)

How many cars are registered per administrative unit (geographical)?


What about fuel type (gasoline vs diesel vs others)?

Most common engine size/displacement (ccm):


And colors?


I will probably add more queries/results, when I get some new ideas and/or questions.

Converting data from Microsoft Access (mdb) format to MySQL

Slovenian “National Interoperability Framework” (NIO) publishes some interesting data every now and then, but most data is usually in MS Access ‘mdb’ format and/or ‘csv’. Working with csv files is painful, since you have to do everything by hand (no selects, filters etc, you have to go through each and every line and check the conditions in a giant loop), and using Microsoft Access on Linux is an even bigger NO.

So, i decided to download some data (anonymized data on drivers licences in Slovenia), and convert it from mdb to MySQL (and insert it into the database). I could have done the same with parsing the csv file, but doing it from mdb autogenerates the database schema, (with csv, you have to do that yourself, by hand).

For converting the data i used mdbtools. The data i used can be found here (zipped mdb).

The first thing after downloading and unzipping the data, was to check which (and how many) tables are inside – you can do this with the “mdb-tables” command.

So, there is one table in the database named “Vozniki_2013” (old data, I know).

To generate the schema I used the “mdb-schema” command (with MySQL output):

Every field is “varchar (510)”, and that’s long. I’ve checked the data, and it fits into “varchar (50)”, so you can do a sed replace, to change the field size in the output. After that, you connect to the database, paste this output into the console (or pipe it directly from the command), and you will have an empty table created for your data.

This would also be a good time to create any indexes you need.

Now all we need is the actual data; we can get that using the “mdb-export” command (we have to tell it the table we want to export, and the output syntax).

All you have to do now, is to redirect this to a file, and copy it to te server with the database, and import it (or pipe it directly, if you can use the MySQL client on the machine you’re using now).

If you have a large table with alot of lines/INSERTs, you will notice that the import takes alot of time. The main slowdown for me was because MySQL commits the data after every INSERT statement, and that takes time. If you want to speed it up, you can disable the autocommit, and manually commit at the end. Since i’ve exported the data to a text file first, I just added the two lines in the beginning and the end of the file:

And voila, you have all the data in your MySQL database now.

Fixing video tearing on Intel Broadwell graphics

I have a Thinkpad X250 laptop (Core i7 5600U CPU) with integrated graphics. When I watch video, in some cases (usually window mode + flash player) causes very noticable video tearing effect (almost like interlacing). It usually didn’t bother me (I mostly watch video fullscreen, where there is no tearing).

And the fix? More obvious then i though. Create a file /etc/X11/xorg.conf.d/15-intel.conf with this inside:

Yeah, who would have thought I had to enable the "TearFree" option…

Merging multiple ESRI SHP files into one

I’ve been playing around with GIS software for a couple of weeks now, and i’ve seen alot of datasources using tiled .shp files (instead of all the data in one huge .shp file, the data is split into “square” tiles). If you want to do stuff with that data, you sometimes want it in one .shp file, so here is a very simple script to merge the files to one:

It just does a for loop over all shp files, and uses GDAL to merge them.

This will create a merge.shp file with a layer named “merge” containing all the data from other shapefiles.

PS: this works with shapefiles of the same type (eg. all polygon or all points).
PPS: this is mostly a reminder for me, since i will probably forget the merge command, and I dont want to lose any more time googling it.

gkrellm2 – changing the next/prev theme shortcut

Gkrellm2 is a “widget” that displays different system/etc. monitors at the side of the screen. It has keyboard shortcuts (PageUp and PageDown) to change the selected theme. So, if you change desktops and widow focus alot, it somethimes happens that gkrellm is focused by mistake, and by using the PageUp/Down keys, you change the theme when you didn’t want to.

I have no idea why, but the key combination is hardcoded in src/gui.c.

Here is the patch to change the combination to Control+PageUp:


If you’re using gentoo. just put the file in /etc/portage/patches/app-admin/gkrellm and reemerge the package.

Aligator clips fail

Recently I’ve found a cheap USB boost converter (small device that turns a lower voltage (~1V-5V) source to a regulated 5V output – so you can charge your USB devices from a range of batteries with different (lower) voltages).

I’ve also received a pack of cables, with aligator clips on both ends, some time ago, so I’ve decided to use them instead of soldering/”improvising”.

Aligator clip cables
Aligator clip cables

For input power, I’ve used a TrustFire 18650 battery in a holder, and the aligator clip cables to connect the boost converter. On the output, I’ve also connected a USB power meter to measure the output voltage and current (I wasn’t sure if it was the 500mA or 1A module).

Boost module without load
Boost module without load

The voltage was 4.94 volts (somewhat low, but with such a small load, it could be the voltage regulator issue), and zero exit current. Everything is OK!

I’ve connected my powerbank to the USB port (to charge it, and check the current), and the situation got bad:

Boost module with load
Boost module with load

Current was around 120mA (well below useful), and the output voltage was 4.68V (also too low). So ok, it’s  a <$1 module, from eBay, probably no quality control whatsoever.. But just in case, let’s check the battery. I’ve connected a small 7segment LED display voltmeter to the boost module (under the clips), and nothing. The voltmeter requires ~3V to turn light up, so either it’s not working at all, or the voltage is too low. So I’ve used a proper multimeter, and measured the voltage of only 0.588 volts at the boost module! The batteries have protection circuits, so this isn’t a battery issue, since protection steps in at around 2.5 volts and shuts down the power completly. Voltage at the battery was around 3.8V when under load. Then I’ve touched the aligator clip cable with my hand (by accident), and it was warm to the touch – so here’s the problem!

Module voltage
Module voltage

Next thing, I’ve measured the voltage drop on the cables, and was surprised by the results (well, not really that much, since the cables heating up quite a bit). The voltage drop on the red cable was 1.657 volts and 1.378 volts on the black cable.  So, on a simple circuit with ~200mA of current, i was losing around 3 volts (~80% of the voltage) on the connecting cables.

Red lead voltage drop
Red lead voltage drop
Black lead voltage drop
Black lead voltage drop

What have I learned today? Always check/verify which cheap Chinese manufacturer to blame, so you don’t blame the wrong one!