Git branch in zsh prompt

Comments Off

When working in a git directory, I would like to see the current branch as part of the Zsh prompt. There are more advanced use cases out there, but I’ll stick with the branch name for now.

The following lines in ~/.zshrc takes care of the prompt. There are a few gotchas, though: The git command will fail if not in a git controlled directory, so we’ll have to hide that failure message. Then, for Zsh to execute the function, rather than printing its verbatim name, the prompt_subst option has to be set. Finally, it is important to use single quotes for the PROMPT line. If double quotes are used, the first output of the function is used, and never called again.

function get-git-branch {
  b="`git symbolic-ref --short HEAD 2> /dev/null`" && echo "($b)" || echo ""
}
 
setopt prompt_subst
 
PROMPT='%n@%2m %~ $(get-git-branch) %# '

Comments Off

Java 8: File handling becomes fun

Comments Off

With the introduction of functional-style syntax and several new Stream based methods in Java 8, file handling finally becomes fun and easy. In fact, the new helper methods and lambda syntax even gives Python a run for its money when it comes to compact code.

Here is how you could read all lines of a file, given as a Path p (since Java 7), and output to stdout.

Files.lines(p).forEach(System.out::println);

To make it a bit more clear what is going on, here a bit more is included and broken up.

Path p = Paths.get("myfile");
 
Stream lines = Files.lines(p);
lines.forEach(System.out::println);
lines.close();

A similarly neat helper function exists for recursively walking over the directory tree of the file system. Again, this prints to stdout.

Files.walk(p).forEach(System.out::println);

Comments Off

Android SDK tools on Debian Wheezy

Comments Off

After downloading the Android SDK bundle, I could not start adb and fastboot, getting somewhat bizarre error messages like:

bash: ./adb: No such file or directory

bash: ./fastboot: No such file or directory

./adb: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

All of those were due to missing dependencies for i386 libraries. Doda’s article on the topic solved the issue. They can be installed by:

dpkg --add-architecture i386
aptitude update
aptitude install libstdc++6:i386 libgcc1:i386 zlib1g:i386 libncurses5:i386

Comments Off

Proto boards and microcontrollers – an overview

Comments Off

MAKE magazine has a nice write-up of several of the popular micro controllers, prototyping and hobby boards out there. 36 of them in total. Of course, that covers only a fraction of all the brands, models and variations. That list runs much much longer.

Comments Off

Hubsan X4 H107C Quadcopter

Comments Off

Lately I’ve had great fun with the miniature quadcopter Hubsan X4 H107C. I’ve already lost the first I got, and the second was this one from Deal Extreme. Right now, they all seems to be sold out, but there are hundreds of other brands to choose from in all price ranges. Starting with this single axis “flying saucer” at only $15. The first quadcopters start at about $30, and most of them seem to be copies of the Hubsan. They have a Walkera with GPS hold starting at $333, all the way up to the 6 channel version which supports First Person View (FPV) glasses.

YZ-L Mini 2-CH LED Radio Control R/C Flying SaucerHubsan X4 H107C 2.4G 4CH R/C Quadcopter w/ 0.3MP CameraWalkera QR X350 6-CH R/C Quadcopter w/ GPS / DEVO F7 5.8G FPV Transmitter RTFWalkera E002 FPV First Person View Goggle Glasses Support DEVO F4/F7

For the Hubsan X4, there are a few extras to get. First of all, the propellers will break, so make sure to get plenty of replacements. Also be aware that they are directional, so there are two of each kind in a bag. A few extra batteries are nice, since a single one will only give about 8 minutes of light time with almost an hour charge time. So to avoid spending hours charging them, some extra chargers also come in handy. Finally, a micro SD card is required for the camera; 2 GB, or even only 1 GB is plenty.

And up and up it goes! Until the wind takes it away, it lands on an inaccessible rooftop and you’ll have to get another one. :-)

Hubsan H107-A02 Replacement BladesHubsan H107C-A24 3.7V 380mAh Li-po Battery for H301CHubsan R/C Spare Parts H107-A06 USB Charger for H107 / H107L

Comments Off

Key mappings in Zsh

Comments Off

Z-Shell is a powerful alternative to Bash, but some of the details can take time to get use to, and some things just have to be changed. For example, the key binding for cursor and other special keys. Using CTRL + arrow keys to skip words might give funny characters like “;5D” and “;5C” instead. As pointed out by Luke Wilde, these keys have to be set up manually. In my case, I had to include the semi-colon in the bindkey command as well.

These should go in ~/.zshrc.

bindkey ';5D' emacs-backward-word
bindkey ';5C' emacs-forward-word
 
export WORDCHARS=''

Only funny thing about setting it up like that, is that if the actual character sequence “;5D” is pasted into the terminal, it will be taken as if the CTRL+LEFT key was pressed. I’m not aware of a work-around for that.

The Zsh wiki lists a few other possible key bindings, including for the Home and End keys:

bindkey "${terminfo[khome]}" beginning-of-line
bindkey "${terminfo[kend]}" end-of-line

Comments Off

Multiplexed SSH sessions for quicker connection

Comments Off

If you need to open multiple SSH connections to the same host, it can get tedious to re-authenticate for every one. And even with public key authentication and no password, the extra channel eats a bit of bandwidth. The solution is multiplexed SSH sessions: Authenticate once, and the following connections to the same host goes over the same session. It’s dead easy to set up:

In your ~/.ssh/config file add the following lines. (Make sure that file has user permissions only, i.e. 600).

Host *
   ControlMaster auto
   ControlPath ~/.ssh/master-%r@%h:%p

It takes effect immediately. SSH twice to the same host to verify.

Comments Off

UTF8 in MySQL

Comments Off

For some unknown reason, the default character set in Debian 7 (“Wheezy”) is different to Ubuntu 12.04 (“Precise Pangolin”). The former uses latin1, while the later utf8. (This could also be down to other local configuration I’m not aware of). The difference between the two is rather subtle, and might go unnoticed. One visible effect is when using letters with German umlaut or Scandinavian specific letters in ordered text. For example, the Norwegian letter Å (aring in HTML) is the last of the alphabet, but when using latin1, it is interpreted as an A (or possibly double a: aa) and ordered first. See this bug report and discussion for more details and examples.

The character set is specified in multiple places in the MySQL DBMS: On the database, table and for the client, server, connection, result set and underlying system. To view the current settings, these two commands give an overview (see here for details):

SELECT * FROM information_schema.SCHEMATA;
SHOW VARIABLES LIKE 'char%';

The output will be something like:

+--------------+--------------------+----------------------------+------------------------+----------+
| CATALOG_NAME | SCHEMA_NAME        | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
+--------------+--------------------+----------------------------+------------------------+----------+
| def          | information_schema | utf8                       | utf8_general_ci        | NULL     |
| def          | test               | latin1                     | latin1_swedish_ci      | NULL     |
| def          | myTESTdb           | latin1                     | latin1_swedish_ci      | NULL     |
+--------------+--------------------+----------------------------+------------------------+----------+
 
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

The above is from the Ubuntu 12.04 system where there were no problems with the character set of text values in the tables. Notice that are some latin1 settings there, but that seems to be fine. The difference on the Debian 7.0 system was that these variable were set to latin1: character_set_client, character_set_connection, character_set_results. One way to change them is through the SET commands in MySQL console:

SET character_set_connection = utf8;

However, that will not persist the settings. Furthermore, it will not be enough to fix the tables. Instead, the config file /etc/mysql/my.cnf has to be changed with the following lines. (from this discussion).

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

Finally, after that is done, the MySQL server restarted, and the data with the special UTF8 characters have to be re-inserted. That is a bit of a pain, and I did not dig too far into how to resolve a problem with existing that. There is a discussion on how to alter the DB and tables here, but I suspect the data will still contain the wrong characters, so it will not be enough. Fundamentally, the byte representation in lation1 and utf8 for these letters are different, so that has the be replaced somehow, as far as I understand.

An alternative to all this is to get it right from the get-go. The MySQL documentation outlines how a new DB can be created with a specific character set. Again I’m not sure if that is enough, as the settings in the my.cnf file deal with the encoding of the client, connection, and result. There are probably several ways to solve the issue, though.

Comments Off

Removing Lens Distortion in GIMP

Comments Off

User yourgimptutor has a nice tutorial on how to remove lens barrel distortion using GIMP. The steps boil down to:

  1. Use the filter “Lens Distortion”: Filters -> Distorts -> Lens Distortion
    For a wide angled photo with the Canon EF-S 17-55 mm lens at 21.0 mm on a 50D body (1.6 crop) I found that adjusting the “Main” slider to +20 gave a good result.

  2. Use the Perspective Transform: Tools -> Transformation Tools -> Perspective
    The edges of the image can now be dragged out to the corners to fill the canvas size.

Comments Off

chroot to ARM

Comments Off

chroot allows you to “run a command or interactive shell with special root directory”, as the man page says. However, it is assumed that the second level root directory is built for the same CPU architecture. This causes a problem if you want to chroot into an ARM based image, for the Raspberry Pi, let’s say. qemu-arm-static, some “voodoo” and several tricks come to the rescue. The process is documented well at Sentry’s Tech Blog, and the original seems to be by Darrin Hodges.

After downloading and unzipping the image, it has to be mounted. There are a few ways to go about this, but I found the easiest was to use plain old mount with an offset. The typical RPi image file is a full disk image, as opposed to a single partition or ISO though. We are after the second partition, which in our case starts at sector 122880. (See this discussion for how to find the correct starting sector using fdisk).

mkdir /mnt/rpi
mount -o loop,offset=$(( 512 * 122880 )) 2014-01-07-wheezy-raspbian.img /mnt/rpi

Next we’ll copy a statically built QEMU binary for ARM to the mounted image. You might need to install QEMU on the host system first. Furthermore, we need to mount or bind the special system directories from the host to the chroot.

apt-get install qemu-user-static
cp /usr/bin/qemu-arm-static /mnt/rpi/usr/bin/

mount -o bind /dev /mnt/rpi/dev
mount -o bind /proc /mnt/rpi/proc
mount -o bind /sys /mnt/rpi/sys

Next comes the magic. This registers the ARM executable format with the QEMU static binary. Thus, the path to qemu-arm-static has to match where it is located on the host and slave systems (as far as I understand).

echo ':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:' > /proc/sys/fs/binfmt_misc/register

Finally, it’s time for the moment of truth:

chroot /mnt/rpi

uname -a
Linux hrb 3.2.0-4-amd64 #1 SMP Debian 3.2.51-1 armv7l GNU/Linux

In some cases, the error “qemu: uncaught target signal 4 (Illegal instruction) – core dumped” occurs. User kinsa notes here that the lines of the file ld.so.preload (i.e. on the slave, /mnt/rpi/etc/ld.so.preload) has to be commented out (with a # in front).

Congratulations, you now have an ARM based chroot. What to do with it? Maybe install a few “missing” packages before copying over to one or more SD cards, set up the users, modify passwords, etc. Or take advantage of the CPU and memory of the host system or compile from source.

apt-get install htop tree ipython ipython3 gnuplot

As a final note, when done, you want to clean up the mount points.

umount /mnt/rpi/dev
umount /mnt/rpi/proc
umount /mnt/rpi/sys
umount /mnt/rpi

Comments Off

Storage prices

Comments Off

The new year has not seen major changes to the prices of spinning disks. However, on the SSD side, there are some interesting news, with Samsung really gearing up. Their 840 EVO and Pro series are now amongst the most affordable and reliable disks on the market.

The EVO series uses TLC memory, which stacks more bits per cell. This increases speed, but decreases reliability. The rule of thumb is 1000 writes for TLC memory, while MLC might be 10 times higher at 10k writes. It means that TLC disks will typically be for low to medium consumer use, while MLC is for higher pro use (as the series name also suggests). This is also reflected in Samsung’s warranty: Three years on the TLC disks, which is good, and an impressive five years on the MLC Pro disks. By comparison, Western Digital’s Red series has three years warranty, while the Green series has only two years.

Also notable is the fact that for the first time price per byte is lower for an SSD disk than a stack of 100 CD-Rs. That is of course because the 100 CD stack has stayed at about 30 Euros for the last six or more years. Never the less, it underlines the fact that 2014 will be the year when decent sized SSDs reach reasonable prices. Upgrading an older laptop with a 500 GB or 750 GB SSD is soon at a point where it makes sense, and will give good return on investment.

Media Type Product Capacity Price CHF Price Euros Euros / GB GBs / Euro
Harddisk Seagate Barracuda 4TB 4000 GB 164.00 134.43 0.03 29.76
Harddisk Western Digital Caviar Green 3TB 3000 GB 125.00 102.46 0.03 29.28
Harddisk Western Digital Caviar Green 4TB 4000 GB 169.00 138.52 0.03 28.88
Harddisk Western Digital Caviar Green 2TB 2000 GB 91.00 74.59 0.04 26.81
External 3.5 Western Digital My Book Essential Edition 4TB, USB3 4000 GB 183.00 150.00 0.04 26.67
Harddisk Western Digital Red 4TB 4000 GB 185.00 151.64 0.04 26.38
Harddisk Western Digital Red 3TB 3000 GB 139.00 113.93 0.04 26.33
External 3.5 Western Digital My Book Essential Edition 3TB, USB3 3000 GB 143.00 117.21 0.04 25.59
Harddisk Hitachi Deskstar 7K4000, 4TB 4000 GB 191.00 156.56 0.04 25.55
External 2.5 Western Digital Elements Portable 2TB, USB3 2000 GB 119.00 97.54 0.05 20.50
Harddisk Western Digital Caviar Black 4TB 4000 GB 260.00 213.11 0.05 18.77
Harddisk Western Digital Caviar Green 1TB 1000 GB 67.00 54.92 0.05 18.21
External 2.5 Western Digital My Passport 2TB, USB3 2000 GB 135.00 110.66 0.06 18.07
External 2.5 Western Digital Elements Portable 1TB, USB3 1000 GB 82.00 67.21 0.07 14.88
Blu-ray Verbatim BD-R SL 25 @ 50GB 1250 GB 238.00 195.08 0.16 6.41
DVD+R DL Verbatim 8x DVD+R DL 25 @ 8,5GB 213 GB 42.00 34.43 0.16 6.17
DVD-R Verbatim 16x DVD-R 100 @ 4,7GB 470 GB 96.00 78.69 0.17 5.97
DVD+R DL Verbatim 8x DVD+R DL 10 @ 8,5GB 85 GB 36.00 29.51 0.35 2.88
SSD Samsung SSD 840 EVO Basic, TLC, 750GB 750 GB 339.00 277.87 0.37 2.70
CD-R Verbatim CD-R 100 @ 700MB 70 GB 35.00 28.69 0.41 2.44
SSD Samsung SSD 840 EVO Basic, TLC, 1TB 1000 GB 555.00 454.92 0.45 2.20
SSD Crucial M500 SSD, MLC, 120GB 120 GB 78.00 63.93 0.53 1.88
SSD Samsung SSD 840 EVO Basic, TLC, 500GB 500 GB 329.00 269.67 0.54 1.85
SSD Sandisk Ultra Plus, MLC, 256GB 256 GB 172.00 140.98 0.55 1.82
SSD Samsung SSD 840 EVO Basic, TLC, 250GB 250 GB 171.00 140.16 0.56 1.78
USB Flash Sandisk Cruzer Flash Drive 32GB 32 GB 23.00 18.85 0.59 1.70
SSD Kingston HyperX 3K, MLC, 240GB 240 GB 186.00 152.46 0.64 1.57
SSD Samsung SSD 840 EVO Basic, TLC, 120GB 120 GB 99.00 81.15 0.68 1.48
SSD Samsung SSD 840 Pro Basic, MLC, 512GB 512 GB 439.00 359.84 0.70 1.42
SSD Samsung SSD 840 Pro Basic, MLC, 256GB 256 GB 229.00 187.70 0.73 1.36
SSD Kingston SSDNow KC300, MLC, 120GB 120 GB 111.00 90.98 0.76 1.32
SSD Corsair Force GT, MLC, 240GB 240 GB 222.00 181.97 0.76 1.32
SSD Sandisk Extreme, MLC, 480GB 480 GB 457.00 374.59 0.78 1.28
SSD Corsair Neutron, MLC, 128GB 128 GB 129.00 105.74 0.83 1.21
SDXC Sandisk Ultra SDXC, Class 10, 15/30MB/s, 64GB 64 GB 67.00 54.92 0.86 1.17
SSD Corsair Force GT, MLC, 120GB 120 GB 126.00 103.28 0.86 1.16
SSD Samsung SSD 840 Pro Basic, MLC, 128GB 128 GB 139.00 113.93 0.89 1.12
USB Flash Sandisk Cruzer Flash Drive 16GB 16 GB 18.00 14.75 0.92 1.08
SDHC Sandisk Ultra, Class 10, 15/30MB/s, 32GB 32 GB 37.00 30.33 0.95 1.06
USB Flash Sandisk Cruzer Extreme 64GB 64 GB 97.00 79.51 1.24 0.80
SDHC Sandisk Ultra, Class 10, 30MB/s, 16GB 16 GB 27.00 22.13 1.38 0.72
SDXC Sandisk Extreme SDXC, UHS-I, 80/60MB/s, 64GB 64 GB 119.00 97.54 1.52 0.66
SDHC Sandisk Ultra, Class 10, 30MB/s, 8GB 8 GB 16.00 13.11 1.64 0.61
SDHC Sandisk Extreme, Class 10, 30MB/s, 16GB 16 GB 35.00 28.69 1.79 0.56
USB Flash Sandisk Cruzer Flash Drive 8GB 8 GB 18.00 14.75 1.84 0.54
SDXC Sandisk Extreme Pro SDXC, UHS-I, 95/45MB/s, 64GB 64 GB 148.00 121.31 1.90 0.53
SDHC Sandisk Extreme Pro, Class 10, 90/95MB/s, 32GB 32 GB 78.00 63.93 2.00 0.50
Compact Flash SanDisk Ultra 200x, 16GB 16 GB 49.00 40.16 2.51 0.40
Compact Flash Sandisk Extreme 800x, 120MB/s, 128GB 128 GB 426.00 349.18 2.73 0.37
SDHC Sandisk Extreme Pro, Class UHS-I, 90/95MB/s, 8GB 8 GB 27.00 22.13 2.77 0.36
SDHC Sandisk Extreme Pro, Class UHS-I, 90/95MB/s, 16GB 16 GB 55.00 45.08 2.82 0.35
Compact Flash Sandisk Extreme 800x, 120MB/s, 64GB 64 GB 230.00 188.52 2.95 0.34
Compact Flash SanDisk Extreme 400x, 60MB/s, 32GB 32 GB 123.00 100.82 3.15 0.32
Compact Flash Sandisk Extreme Pro 160MB/s, 256GB 256 GB 1306.00 1070.49 4.18 0.24
Compact Flash Sandisk Extreme Pro 600x, 90MB/s, 16GB 16 GB 126.00 103.28 6.45 0.15

Exchange rate: 1 Euro = 1.220000 CHF.

Comments Off

Fedora 20 released

Comments Off

Fedora 20 was released a few days ago. J.A. Watson at ZDNet has a brief overview of the different desktops available, and concludes that for the most they run just fine on any hardware, including “sub-notebooks”. Furthermore, even though the “spin” of each desktop have specialised in their own applications, there are always plenty more to chose from in the main Fedora repositories.

The Anaconda installer was written back in release 18, and FedUp (FEDora UPgrader) is now the main system upgrade tool. It is not quite clear whether it is preferred to perform that on a running system though, as opposed to booting from an installer image.

Thus, the following links still apply, even for existing installations:

Comments Off

Another assault on privacy by GCHQ

Comments Off

Recently, it was revealed by IT Security Guru that the British intelligence agency GCHQ had demand a backdoor into the secure email service PrivateSky by CertiVox. At the end of 2012, GCHQ made the request, but CertiVox chose to close the service instead of betraying their customers. This is preceding the similar heavy-handed threats by NSA which caused US based Lavabit and Canadian based Silent Circle to shut down their secure email services.

It is clear then, that it is not possible to operate secure email or communication services within these countries. In that light, it’s interesting to see Swiss hosting companies picking up business. “Business for Switzerland’s 55 data centres is booming”, claims the article. It will be interesting to see how it plays out. Will they be pressured by US as was the case with the banks? Or will they also sell out, as was the case with the Swiss based Crypto AG and their machines?

As many have pointed out, the physical security of a data centre is often less of an issue than its network and system security. Furthermore, it’s a question of how it is used and what is offered. PrivateSky is for example still operational, but only for its owners. If somebody offered a secure communication service from within the Tor network, it would be both hard to detect, so it might fly under the radar for a while, and hard to take down if hosted in Switzerland. That’s a business idea right, up for grabs for anybody with a bit of spare time and money.

Comments Off

VGA adapter for the Raspberry Pi

Comments Off

Just tested a HDMI to VGA adapter for the RPi with my old CRT 1024×768 monitor. It works great! This was the HD2V04 HDMI to VGA + 3.5mm Audio Jack Converter Adapter Box from DealExtreme, at $21.70. The VGA port was a bit tight, so I had to make sure it was properly connected. Also, the monitor did not display anything before a cold restart of the Pi. It comes only with a USB power cable, to it means a wall wart or powered USB hub is required. (It should go without saying that you don’t want to power it off the Pi itself).

Comments Off

DC-to-DC converters

Comments Off

There are a number of different DC-to-DC converters out there, used to convert between different voltage levels. Ranging from the small but inefficient linear regulators to switched-mode converters like the buck converter that can achieve 95% or even higher efficiency. Among the switched-mode converters there are quite a few variations based on different but often similar circuit topologies: The buck converter is a step-down converter, and as the name suggest its output voltage will be reduced from its input. A boost (step-up) converter has similar design, but will do the opposite. A buck-boost converter combines the two so output voltage can be converted from both higher and lower input voltage. However, the traditional buck-boost converter inverts the polarity, so that the output voltage is of the opposite polarity than its input. A single-ended primary-inductor converter (SEPIC) (aka. buck-boost SEPIC) solves that problem, and delivers fixed non-inverted output throughout the range of its input voltage.

If this is all a bit confusing, have a look at Julian Ilett’s excellent YouTube videos and reviews: He covers the SEPIC buck boost converter, and shows how it seamlessly goes from 0.5 to 30 V out on a 9V battery. He shows how a 400W boost converter can drive a 100W LED pad, and he has a review of several different buck converters, in fact several reviews. He also has many interesting videos on driving different LED boards, including 50W and 30W RGB. You’ll really have to watch all his 140 videos!

When it comes to buying these, DealExtreme of course has a lot on offer, but here it seems like eBay has a bigger variety across its different sellers. I’ve already ordered this one, and expect to get more in the future. In particular, one of the variations on the ZXY6005 (D) power supply looks like a must-have in a hobby workshop.

Comments Off