Monday, September 12, 2016

How to Install DEB package with All Necessary Dependencies Automatically

When you are installing .deb packages with dpkg -i command, it does not automatically install necessary dependencies. For example, when I was trying to install Dropbox client on my freshly installed Ubuntu, I get
$ sudo dpkg -i dropbox_2015.10.28_amd64.deb 
Selecting previously unselected package dropbox.
(Reading database ... 211659 files and directories currently installed.)
Preparing to unpack dropbox_2015.10.28_amd64.deb ...
Unpacking dropbox (2015.10.28) ...
dpkg: dependency problems prevent configuration of dropbox:
 dropbox depends on python-gtk2 (>= 2.12); however:
  Package python-gtk2 is not installed.

dpkg: error processing package dropbox (--install):
 dependency problems - leaving unconfigured
Processing triggers for gnome-menus (3.13.3-6ubuntu3.1) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu5) ...
Processing triggers for bamfdaemon (0.5.3~bzr0+16.04.20160701-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for mime-support (3.59ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.15-0ubuntu1) ...
Processing triggers for man-db (2.7.5-1) ...
Errors were encountered while processing:
 dropbox

This is quite annoying. One solution is to remove Dropbox and install dependency first and re-install Dropbox. This process is outlined here for Google Chrome installation, but the same holds for any other .deb packages.

Well, it turns out that there is a good utility that will do this for you automatically. Try this!
$ sudo apt-get install -y gdebi
...
$ sudo gdebi dropbox_2015.10.28_amd64.deb Reading package lists... Done
Building dependency tree        
Reading state information... Done
Reading state information... Done
Requires the installation of the following packages: python-gtk2 

cloud synchronization engine - CLI and Nautilus extension
 Dropbox is a free service that lets you bring your photos, docs, and videos
 anywhere and share them easily.
 .
 This package provides a command-line tool and a Nautilus extension that
 integrates the Dropbox web service with your GNOME Desktop.
Do you want to install the software package? [y/N]:y
Fetched 0 B in 0s (0 B/s)                                            
Selecting previously unselected package python-gtk2.
(Reading database ... 218142 files and directories currently installed.)
Preparing to unpack .../python-gtk2_2.24.0-4ubuntu1_amd64.deb ...
Unpacking python-gtk2 (2.24.0-4ubuntu1) ...
Setting up python-gtk2 (2.24.0-4ubuntu1) ...
Selecting previously unselected package dropbox.
(Reading database ... 218162 files and directories currently installed.)
Preparing to unpack dropbox_2015.10.28_amd64.deb ...
Unpacking dropbox (2015.10.28) ...
Setting up dropbox (2015.10.28) ...
Please restart all running instances of Nautilus, or you will experience problems. i.e. nautilus --quit
Dropbox installation successfully completed! You can start Dropbox from your applications menu.
Processing triggers for gnome-menus (3.13.3-6ubuntu3.1) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu5) ...
Processing triggers for bamfdaemon (0.5.3~bzr0+16.04.20160701-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for mime-support (3.59ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.15-0ubuntu1) ...
Processing triggers for man-db (2.7.5-1) ...

Much easier, right?

Linux LVM Tutorial Part 1

Here is my scenario: I have two physical SSDs, one with 500GB and another with 250GB. I installed Ubuntu on 250GB disk using default LVM configuration. I am trying to add the disk with 500GB space into LVM.

First, let's check my configuration:
root@desktop:~# fdisk -l /dev/sd*
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: ...

Device       Start       End   Sectors   Size Type
/dev/sdb1     2048   1050623   1048576   512M EFI System
/dev/sdb2  1050624   2050047    999424   488M Linux filesystem
/dev/sdb3  2050048 488396799 486346752 231.9G Linux LVM
Disk /dev/sdb1: 512 MiB, 536870912 bytes, 1048576 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x00000000
Disk /dev/sdb2: 488 MiB, 511705088 bytes, 999424 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/sdb3: 231.9 GiB, 249009537024 bytes, 486346752 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
fdisk: cannot open /dev/sdc: No medium found

As you can see, my 500GB drive is mapped to /dev/sda and 250GB drive is mapped to /dev/sdb with 3 partitions, the first being EFI system with 512MB of space, the second being Linux filesystem with 488MB, and the last being Linux LVM with 231.9GB of space. 

To allocate the entire 500GB drive to LVM, run
root@desktop:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x55bec7d8.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-976773167, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-976773167, default 976773167): 

Created a new partition 1 of type 'Linux' and of size 465.8 GiB.

Command (m for help): t
Selected partition 1
Partition type (type L to list all types): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Now, let's verify that /dev/sda has been formatted to type LVM:
root@desktop:~# fdisk -l /dev/sda
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: ...

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048 976773167 976771120 465.8G 8e Linux LVM

Perfect. Let's now add this physical drive to LVM physical volume:
root@desktop:~# pvcreate /dev/sda1
  Physical volume "/dev/sda1" successfully created

Again, let's verify:
root@desktop:~# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb3
  VG Name               ubuntu-vg
  PV Size               231.91 GiB / not usable 2.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              59368
  Free PE               4075
  Allocated PE          55293
  PV UUID               ...
   
  "/dev/sda1" is a new physical volume of "465.76 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda1
  VG Name               
  PV Size               465.76 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               ...

You can see that /dev/sdb3 is not fully allocated, as there is some free space. Let's examine logical volume:
root@desktop:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/root
  LV Name                root
  VG Name                ubuntu-vg
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2016-09-12 21:40:26 +0900
  LV Status              available
  # open                 1
  LV Size                215.99 GiB
  Current LE             55293
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

As you can see, there is only one logical volume, namely /dev/ubuntu-vg/root and its space is only 216GB out of total 232GB of /dev/sdb3. To enlare the volume of this logical volume, I run
root@desktop:~# lvextend /dev/ubuntu-vg/root /dev/sdb3
  Size of logical volume ubuntu-vg/root changed from 215.99 GiB (55293 extents) to 231.91 GiB (59368 extents).
  Logical volume root successfully resized.

Let's verify:
root@desktop:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/root
  LV Name                root
  VG Name                ubuntu-vg
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2016-09-12 21:40:26 +0900
  LV Status              available
  # open                 1
  LV Size                231.91 GiB
  Current LE             59368
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

Viola! I have enlarged the logical volume /dev/ubuntu-vg/root to use the entire /dev/sdb3 space.

Next, I will create three logical volumes from group volume home:
root@desktop:~# lvcreate --name vm --size 100GB home 
  Logical volume "vm" created.
root@desktop:~# lvcreate --name home --size 200GB home
  Logical volume "home" created.
root@desktop:~# lvcreate --name backup -l 100%FREE home
  Logical volume "backup" created.

The first command creates logical volume named vm with size 100GB from volume group home.
The second command creates logical volume named home with size 200GB from volume group home.
The third last command creates logical volume named backup with the remaining space of group volume home.

After all these steps, my final logical volume info is given below:
root@desktop:~# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/root
  LV Name                root
  VG Name                ubuntu-vg
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2016-09-12 21:40:26 +0900
  LV Status              available
  # open                 1
  LV Size                231.91 GiB
  Current LE             59368
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0
   
  --- Logical volume ---
  LV Path                /dev/home/vm
  LV Name                vm
  VG Name                home
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time desktop, 2016-09-12 23:14:54 +0900
  LV Status              available
  # open                 0
  LV Size                100.00 GiB
  Current LE             25600
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1
   
  --- Logical volume ---
  LV Path                /dev/home/home
  LV Name                home
  VG Name                home
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time desktop, 2016-09-12 23:15:11 +0900
  LV Status              available
  # open                 0
  LV Size                200.00 GiB
  Current LE             51200
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:2
   
  --- Logical volume ---
  LV Path                /dev/home/backup
  LV Name                backup
  VG Name                home
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time desktop, 2016-09-12 23:16:11 +0900
  LV Status              available
  # open                 0
  LV Size                165.76 GiB
  Current LE             42434
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:3


For Linux system to take effect on resize of the partition, you will need to run the following:
root@desktop:~# resize2fs logical_volume_path

where logical_volume_path is /dev/home/home or /dev/ubuntu-vg/root, etc.

How to Disable Swap Space

These days, memory costs so cheap that unless you are running multiple instances of virtual machines and heavy-load programs, swap space is no longer necessary. In my case, I have 16GB of RAM, and the default swap space allocated by Ubuntu is 16GB, which I think is waste of disk space. So, I decided to remove swap space. Here is how.

Disable swap:
$ sudo swapoff -a

If you want to permanently disable swap space, you may want to free disk space allocated to swap space. To do this, first remove auto-mounting swap at boot:
$ sudo vim /etc/fstab

In the file, comment out the line that is mounting the swap space. It should be obvious. In my case, it reads like:
#/dev/mapper/ubuntu--vg-swap_1 none            swap    sw              0       0

Now, you may want to use gparted or any other utilities and tools to free up the space!

Monday, September 5, 2016

How to Prevent Sleep When Remote Connection is On

This post is based on here. To prevent your system from going to sleep while ssh connection is on, follow the steps below:

Create /etc/pm/sleep.d/05_ssh_keepawake file
$ sudo vim /etc/pm/sleep.d/05_ssh_keepawake

Copy the following content to the file
#!/bin/sh
#check for SSH sessions, and prevent suspending:
if [ `who | grep -cv "(:"` -gt 0 ]; then
        echo "SSH session(s) are on. Not suspending."
        exit 1;
fi

Make it executable
$ sudo chmod +x /etc/pm/sleep.d/05_ssh_keepawake

Make sure to reboot your system to take effect!

Friday, September 2, 2016

Set Shared Library Path during Compilation without Using LD_LIBRARY_PATH

When you are linking a shared library that is not in the default shared library path, usually specified in /etc/ld.so.conf, plus /lib and /usr/lib, you need to set the environment variable LD_LIBRARY_PATH to point to the directory that contains user-created shared libraries. Here is how to save the path of the custom shared library path within the executable during compilation, so that one can execute the binary without setting LD_LIBRARY_PATH. For Mac OS X, the variable name is DYLD_LIBRARY_PATH.

For Linux,
$ gcc main.c -g /user/custom/shared/library/path -Wl,-rpath=/user/custom/shared/library/path

For Mac OS X,
$ gcc main.c -g /user/custom/shared/library/path -Wl,-rpath /user/custom/shared/library/path

The last option -Wl,-rpath will set the executable's rpath, so that it will look for the shared library in the specified folder. With this option, you won't need to set LD_LIBRARY_PATH or DYLD_LIBRARY_PATH.

Image Stitching with OpenCV 3.1.0

With OpenCV, stitching images to make a panorama is very easy. I will go over how to create a beautiful panorama image using OpenCV 3.1.0. Because there is a bug in OpenCV 3.1.0 Stitcher class when OpenCL is enabled, we will need to turn it off when building.

To build OpenCV 3.1.0 from source files, download it first from here or run
$ wget https://github.com/Itseez/opencv/archive/3.1.0.zip

Unzip it
$ unzip 3.1.0.zip
$ cd opencv-3.1.0

Create build folder
$ mkdir build && cd build

Create Makefile
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_EXAMPLES=ON -D CMAKE_BUILD_PREFIX=/usr/local/ -D WITH_OPENCL=OFF ..

If you encounter some ippicv hash error, try with ipp off:
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_EXAMPLES=ON -D CMAKE_BUILD_PREFIX=/usr/local/ -D WITH_OPENCL=OFF -D WITH_IPP=OFF ..

Next, let's build the library!

For Linux systems,
$ make -j `nproc`

For Mac OS X,
$ make -j `sysctl -n hw.ncpu`

After some time, the build should end successfully. Let's test stitch function out. It turns out that there is an example code in the source:
$ vim ../samples/cpp/stitching.cpp
$ vim ../samples/cpp/stitching_detailed.cpp

Actually, we just built this example file, and its executable is located in build/bin directory
$ cd bin/
$ ls cpp-example-stitching*

To test it, simply provide the image files to be stitched
$ ./cpp-example-stitching image1.jpg image2.jpg
$ ./cpp-example-stitching_detailed image1.jpg image2.jpg --features orb

You may want to download the files from here. If successful, the resultant panorama image files should have been produced
$ eog result.jpg

For Mac OS X, run
$ open -a Preview result.jpg

You may also want to run other OpenCV test and example files in the bin folder. To run tests, you will need to copy OpenCV extra data and set OPENCV_TEST_DATA_PATH variable:
$ git clone git://github.com/Itseez/opencv_extra.git
$ export OPENCV_TEST_DATA_PATH=`pwd`/opencv_extra/testdata

Now, you can run test files:
./opencv_test_stitching

Finally, to install it on the system, one needs to run
$ sudo make install

Enjoy your panorama image!

Thursday, September 1, 2016

Three Minutes Daily Vim Tip: Ctags

When you are dealing with a large code base, you definitely want to jump back and forth within the code. Let's assume that you are looking at OpenCV library source code. To find functions and jump back and forth between functions and files in Vim, one can use ctags:

First, install ctags
$ sudo apt-get install ctags

Next, create the tags for the source directory
$ cd ~/opencv-3.1.0
$ ctags -R

Finally, use Vim to search for a specific tag
$ vim -t Stitcher

The above command looks for a function, class, or variable named that contains the word Stitcher. There should only be one return, and Vim will open up stitching.hpp file and point to the class Stitcher.

To view the list of previously found tag, run the following within Vim
:ts

To search for tag blend, run
:ts blend

To go to the next or previous tag result in the list, run
:tn
:tp


Now this is where the fun begins! To jump to the tag currently pointed by the cursor, press
<ctrl> + ]

To go back to the caller tag, press
<ctrl> + t

To go back to the previous position, press
<ctrl> + o

Happy Vimming!

Saturday, August 27, 2016

Three Minutes Daily Vim Tip: Open Specific Location

Say you are compiling your code, and the compiler warns you with error in line 77 of your main.c file. You want to go to this specific line of this file. You could do this in two steps, i.e., open up the file and go to the line.

$ vim main.c
:77

Well, there is a a bit easier way to do this in one command:
$ vim +77 main.c

This command will open up main.c file at line 77.

Next, say you want to open up the file and go to your function main(). To do this, you would normally do
$ vim main.c
/main(

Again, there is a single command to do this:
$ vim +/main\( main.c

Note the presence of backslash \, which will tell the shell to input literal parenthesis ( character.

Happy Vimming!

Friday, August 26, 2016

Conditional Break with GDB

Say you want to setup a break point for given condition. To do this in gdb, simply enter the following command:
(gdb) break main.c:80 if x == 0

This will break only when the variable x is equal to 0 in the context. This will be very handy when debugging for certain conditions.

Monday, August 22, 2016

VirtualBox: Share Files between Mac OS X Host and Windows Guest

VirtualBox supports Shared Folders feature so that one can access files directionally between the host and guest systems. In this post, I will go over how to share files between VirtualBox Windows guess and Mac OS X host.




First, open up VirtualBox, and select your Windows virtual machine. Click on the Settings icon to open up the settings window, and click on Shared Folders tab. 




On the right hand side, click on a small button that reads Adds new shared folder. On the drop down menu for Folder Path, choose Other...




Finally, simply choose the folder in your host system (i.e., Mac OS X) which will be shared with the guest system (i.e., Windows). Check Auto-mount option.




Now, the setting is complete. Fire up your virtual machine, and you should be able to see your shared drive as a network drive when you open up My Computer window. The shared drive address should be \\vboxsrv