Category Archives: Linux

How to compact VirtualBox’s VDMK file size

This guide explains how to shrink (compact) the virtual disk files (files having extension .vmdk) of your VirtualBox virtual machines so they consume less disk space. It is also easier to upload and share them with other people.

Fill the free space with zeros
VirtualBox only knows that the space is really free if it’s been set to zero, and standard deletion won’t do this. Login into the virtual machine and run the following command:

cat /dev/zero > zero.fill; sync; sleep 1; sync; rm -f zero.fill

Navigate to VirtualBox virtual machine folder
The virtual machines you have configured in VirtualBox are stored in folder “VirtualBox VMs” inside your home directory (or at least when Ubuntu is your host machine). Every machine has its own directory with name starting with the machine name. You have to navigate to the folder containing the files of the virtual machine having disk you want to compact and open terminal.

Get the UUID of the virtual disk
You have to obtain the UUID of the disk you want to shrink. I will explain you why later.
The command to do this is:

vboxmanage showhdinfo box-disk1.vmdk

This command assumes that your working directory is the directory that contains the vmdk file. Of course, you have to change the name of the vmdk file if it is different.
Write down the reported UUID because you will need it later.

Convert it to vdi
In order to compact the disk, you need to convert it to VDI format using this command

VBoxManage clonehd box-disk1.vmdk box-disk1.vdi --format vdi

Compact

VBoxManage modifyhd box-disk1.vdi --compact

Convert it back to vmdk

VBoxManage clonehd box-disk1.vdi box-disk1.vmdk --format vmdk
rm box-disk1.vdi

Set the original UUID
You converted the original vmdk disk file to vdi, compacted it and created new vmdk file from the compacted one. So, basically, you have created new virtual disk with new UUID which will not be recognized by VirtualBox unless you set the original disk’s UUID to the newly created one using the following command:

vboxmanage internalcommands sethduuid ./box-disk1.vmdk <original UUID here>

Voilà! The virtual disk size is successfully reduced!

Leave me a comment if you have any troubles or ideas how to improve this article. I am not VirtualBox expert and so I can’t guarantee that the above guide is the easiest way to get the job done.

How to restore a vagrant box using vmdk disk

The proper way to backup (and later restore) vagrant box is by using vagrant package command. This command creates a re-usable box file that can be easily transfered and used on other machines.

I have two Vagrant boxes I use on Ubuntu host machine. The last time I reinstalled my host machine OS I forgot to backup the boxes using vagrant package command, but saved the ~/VirtualBox VMs/ folder and all the files in it. This folder contains the virtual machines I amusing with VirtualBox (the virtual HDs to be more specific), so I used them to restore my boxes. Below are the steps I used to recover the boxes using the virtual HDs (vmdk files)

1. Import the machine
Go to ~/VirtualBox VMs/<name of the machine>/ and double click on the file with vbox extension. This should open VirtualBox GUI and import the machine.

2. Create base box file using the virtual machine you just imported
Use the following command to create a box file.

vagrant package --base <name of running virtual machine> --output ubuntu.box

3. Import the base box
First at all, you have to know the name of vagrant box you are restoring. Just open the Vagrantfile and look for line like this

config.vm.box = "ubuntu/trusty64"

As you can see, the box I am trying to restore is named “ubuntu/trusty64” and so I have to run the following command:

vagrant box add "ubuntu" ./ubuntu.box

Of course, you have to adjust the file path to the ubuntu.box file you have created in step 2.
To confirm that box is successfully added, you can run

vagrant box list

.

If everything is OK, you can now start your box in the usual way – using vagrant up command. Enjoy!

Add GIT branch information to Bash prompt

I’ve seen such as fancy Bash prompts on various tutorials and Linux examples over the Internet and I’ve always wondered how is achieved. I never really had a enough free time to learn more about it and explore the options. But being jobless for a month gave me opportunity to play with the thinks I like 🙂

My solution is pretty simple: when you navigate to git controlled folder, the bash prompt will show “@ branch” after the directory name. Nothing fancy.

Just open your ~/.bashrc configuration file with your favorite editor and add the following:

get_git_branch () {
git name-rev HEAD 2> /dev/null | sed "s/[a-zA-Z0-9]\+\ \(.*\)/ @ \1/"
}

than put this into your PS1 string:

$(get_git_branch)

so it become something like that

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w$(parse_git_branch) \n\$\[\033[00m\] '

Restart your terminal or type bash to start new bash session. Navigate to git controlled folder to test. It should look like this:

bash-git

Disabling graphical login in Raspbian

If you are using Raspberry PI as headless machine, you do not need to start the graphical server at all. It is useless when Rasbian is used as server OS and disabling it will free ~40MB of RAM.
To do so, you need to edit the /etc/X11/default-display-manager file using your favorite text editor.

$ sudo vim /etc/X11/default-display-manager

Then comment out the line

#/usr/sbin/lightdm

and add a new one

/bin/false

Save the file and reboot the machine. Voila!

Update

There is a easier way of doing it…
Just run the Rasbian configuration utility

$ sudo raspi-config

In boot option menu choose:

  1. Enable Boot to Desktop/Scratch and enable Console Text.
  2. Also check SSH by going in ssh and selecting Enable or disable ssh server.
  3. After you quit, you’ll be asked to reboot, choose Yes.

 

VLC does not support the audio or video format “hevc”. Unfortunately there is no way for you to fix this.

Just you have chosen a good mоvie to watch, you are seated comfortably in an armchair with pack of popcorn, clicked on your favorite media player VideoLAN and… bang! Error!

VLC does not support the audio or video format “hevc”. Unfortunately there is no way for you to fix this.

The error message above is not really correct. There is a way to fix it!

 

How To Configure a Mail Server Using Postfix and Dovecot on Ubuntu 14.04 LTS

This tutorial explains how to setup mail server on Ubuntu 14.04 using Postfix and Dovecot. It’s based on few other tutorials and does not pretend to show you all the best practices. I am writing this tutorial because it’s 99.9% sure that I will forget how I configured my own mail server very very soon and I have to write down all the info while it’s still fresh in my head.

Ok, let’s start to work!

Continue reading

How To Get The Log Instead of Just Revision Numbers by svn merge info

svn merge command is really great – it can be used to determine which revisions are already merged and which are eligible to merge from particular branch. But has one big drawback – it can output just the revision numbers, but not the corresponding log messages. So, it’s really hard to determine what has been commited actually with these revisions. Of course, you can run svn log command for the revisions in question, but what if there are, lets say, 50 commits?

You could build a list of commit messages by piping mergeinfo into the svn log command using xargs. It looks like this:

svn mergeinfo --show-revs=eligible ^/branches/version | tr "\\n" "," | xargs -i svn log -c {} ^/branches/version

To make the command little bit easier and shorter, you may consider to add an alias in your ~/.bash_aliases file as follow:

alias svnlog='tr "\\n" "," | xargs -i svn log -c {}'

Now, you can shorten the command like that:

svn mergeinfo --show-revs=eligible ^/branches/version | svnlog ^/branches/version

Enabling SSH Upgrade Access on WordPress on Ubuntu Server

As you may be already know, WordPress allows you to install and update plugins,  widgets, themes etc, as well as whole system trough the admin panel. It’s very convenient time-saving feature but it requires you to provide FTP or FTPS credentials every time when its used. It could be really frustrating!

SFTP (SSH over FTP) should not be confused with FTPS (File Transfer Protocol Over SSL). FTP is vulnerable to attacks and should be avoided because the server can only handle usernames and passwords in plain text. So, as many people, I don’t have it installed on my virtual private server. If you feel that you need to install and enable a FTP server, just for WordPress, think twice – you can use SSH instead and I’ll show you how! Continue reading

How to Undo Changes in “nano” Text Editor in Ubuntu

Nano has experimental undo/redo feature. As you’ll see from the nano manual (type “man nano” in a Terminal to read that), you’ll need to start nano with the -u option

 $ nano -u somefile.txt

… and then you can use Alt-U to undo and Alt-E to redo.

It’s little bit frustrating that you have to type the “-u” option every time when you want to edit a file, but hopefully there is a solution and its name is “alias”.

Aliases are a way for you to customize the commands by giving them aliases (nicknames). You can use them to remember commands with a lot of options or make their names shorter and easier to type. To setup aliases, all you need is to open the ~/.bash_aliases file and type in:

alias nano='nano -u'

If you want to add more aliases, enter each of them on separate line.

Now, typing “nano” is equal as typing “nano -u”. Tricky, yeah?