Sunday, 25 October 2015

How To: Automatically cd to a directory after you create it

The majority of the times that I create a directory I would then change to that directory. To make this happen automatically I add the following to my .bashrc :

mkcd () { mkdir -p "$@" && eval cd "\"\$$#\""; }

Example :

$ mkcd ~/tmp/FOO
$ pwd

How To: Display your Top 10 cli commands

Its useful to be able to see which command you use most often on a Linux box. I add the following to my .bashrc :

cli-top-10() {
  history | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}' | sort -rn | head

Example output :

57 ls
49 cd
26 mv
25 curl
18 wget
17 rm
15 sudo
13 packer
12 docker
12 vim

How To: Permanently Display line numbers for Vim or Vi

It can be very useful when troubleshooting to display line numbers in Vim or Vi. To enable this in your ~/.vimrc file add :

:set number

which can also be manually set within Vim or Vi. Within the editor to remove the line numbers :

:set nonumber

Tuesday, 8 September 2015

VirtualBox slow on installation of Guest OS

I use VirtualBox for testing in many environments. Normally the defaults are fine for installing Linux but on one environment it was really slow to just install. Installing Debian Jessie from an ISO would normally take 15 minutes but in this environment after an hour it was still only at 35%. The host OS was not over taxed and has lots of spare CPU + RAM resources free.

After some testing I found the issue was resolved by :

1. Open Settings
2. Navigate to System->Motherboard
3. Change the Chipset to ICH9 (see below)
4. Press OK

Now the installation will be fast.

Monday, 7 September 2015

Change docker default storage location

One of the first tasks I wanted to do after installing Docker on Arch Linux was to be change the default partition storage location. The default location is /var/lib/docker which is the path docker uses as the root of the docker runtime.

Looking at the man pages for docker its not amazingly obvious at first which option needs to be configured but after some research I found it is :

-g, --graph=""

And the service status shows this option is not set by default

$ sudo systemctl status docker
* docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2015-09-07 13:07:10 PDT; 2h 0min ago
 Main PID: 420 (docker)
   CGroup: /system.slice/docker.service
           `-420 /usr/bin/docker daemon -H fd://

Its possible to define --graph within the /usr/lib/systemd/system/docker.service file but this will be overwritten with every new release of docker.

The solution is first create a directory:

$ sudo mkdir /etc/systemd/system/docker.service.d

Then create a file similar to the following

$ cat /etc/systemd/system/docker.service.d/dockerruntimeroot.conf
ExecStart=/usr/bin/docker daemon -H fd:// --graph /mnt/docker-data


$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

Now the new path will be shown in the service status

$ systemctl status docker
* docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/docker.service.d
   Active: active (running) since Mon 2015-09-07 15:52:28 PDT; 1min 49s ago
 Main PID: 8981 (docker)
   CGroup: /system.slice/docker.service
           `-8981 /usr/bin/docker daemon -H fd:// --graph /mnt/docker-data

Monday, 22 December 2014

Monitor and Display Apartment Temperature using BeagleBone Black

As a project I wanted to be able to monitor the temperature in my apartment and display it on an LCD screen. I also decided that I would also like to simultaneously display the local city temperature.


In addition to the BeagleBone Black I purchased from AdaFruit

AdaFruit provide two Python libraries

For the OS I am running the latest version of Debian wheezy

# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.7 (wheezy)
Release: 7.7
Codename: wheezy

After following the wiring instructions from Adafruit and installing the two python libraries as mentioned above I could display the temperature from my Apartment on the LCD screen. The next step was to get the local temperature in Vancouver, Canada. I looked at a few options such as screen scraping using the Python Beautiful Soup library on the Environment Canada website. The screen scraping did work but not consistently. Looking around for some other options I found the fantastic OpenWeatherMap project, which for Canada uses Environment Canada as a data source. There is a Python library pyowm for OpenWeatherMap. This can be installed using pip :

# pip install pyown

From OpenWeatherMap you can register for free and obtain an API key. Using the pyowm library is relatively straight forward which then allows the data to be obtained and displayed on the LCD screen. 

I have a cron job that runs every 15 minutes during the hours I want the screen to display. I posted the Python script I used on my GitHub repository in case anyone finds it a useful reference.

Fix : Linux NTFS Error Mounting $MFTMirr does not match $MFT (record 0)

With an external USB hard drive that is used to share files between Windows and Linux computers you may get an Error on Linux similar to the following :

Error mounting: mount exited with exit code 13: $MFTMirr does not match $MFT (record 0). Failed to mount ‘/dev/sdd1′: Input/output error NTFS is either inconsistent, or there is a hardware fault, or it’s a SoftRAID/FakeRAID hardware. In the first case run chkdsk /f on Windows then reboot into Windows twice. The usage of the /f parameter is very important! If the device is a SoftRAID/FakeRAID then first activate it and mount a different device under the /dev/mapper/ directory

To resolve the issue is to use ntfsprogs which can be installed from most Linux distributions e.g.

Arch Linux

$ sudo pacman -S ntfsprogs

Ubuntu/Linux Mint/Debian

$ sudo apt-get install ntfsprogs

To fix the issue run the following command syntax

$ sudo ntfsfix <drive mount point>

Change the drive to be the same shown in the Error. For example in the above error the command is

$ sudo ntfsfix /dev/sdd1