Diff the output of two processes

This is a small but useful bash trick to apply ‘diff’ to the output of two separate commands:

laptop-ben:~ benjamin$ diff <(echo a) <(echo b)
1c1
< a
---
> b

Listing methods of an Objective-C class

One of the nicer things about Objective-C is that reflection is actually pretty easy to do. This code sample lists the methods of a class:

#include <objc/runtime.h>

// List the methods of the class instance "myClass"
unsigned int methodCount;
Method* methods = class_copyMethodList([myClass class], &methodCount);
for (int i=0; i<methodCount; i++)
{
	char buffer[256];
	SEL name = method_getName(methods[i]);
	NSLog(@"Method: %@", NSStringFromSelector(name));
	char *returnType = method_copyReturnType(methods[i]);
	NSLog(@"The return type is %s", returnType);
	free(returnType);
	// self, _cmd + any others
	unsigned int numberOfArguments = method_getNumberOfArguments(methods[i]);
	for(int j=0; j<numberOfArguments; j++)
	{
		method_getArgumentType(methods[i], j, buffer, 256);
		NSLog(@"The type of argument %d is %s", j, buffer);
	}
}
free(methods);

This code originally comes from http://distriqt.com/post/863, I’ve fixed it up so you can actually compile it :) Don’t forget to replace ‘myClass’ with your class name..

Welcome to my new blog

After 4 years of using sphpblog (which is now very much unmaintained and looks a bit outdated) I’ve decided to move to a new blogging solution. After lots of experimenting I ended up with Tinkerer, a static blog generator written in Python. It’s less vulnerable than the previous PHP solution, looks nice and modern and offers all the formatting options of Sphinx. The documentation is a bit rough around the edges (lot of searching and experimenting helped) but I’ve managed to make it work.

All the blog postings on my old blog have been migrated to this blog so there should be no need to acces the old blog (which can still be found here: http://www.smork.info/blog/index.php)

Excellent guide to troubleshooting iowait in Linux

Recently my Sheevaplug was experiencing high load caused mostly by iowait. This excellent guide shows you exactly how to troubleshoot this kind of problem using tools that are usually available on most systems. Well recommended!

More file renaming fun

One tool that made my life easier when I recently discovered it: ‘rename’. It allows you to specify a sed-like command line to easily search/replace part of file names like this:

rename s/"SEARCH"/"REPLACE"/g *.txt

Of course it also works for files in subdirs if you use none globbing like this

rename s/"SEARCH"/"REPLACE"/g */*.txt

Generating random strings with openssl

Quickly need a string of random characters? Just use the ‘openssl’ command like this:

benjamin@plug:~$ openssl rand -hex 20
3e7e9c366c16752d5cca56dd99bd8f02cfcdc16

Update 2017-01-10

I’m always greatful when people take the time to write me an e-mail that can improve a post. So this update is brought to you by Robert, thank you very much! By his benchmarks the below method is about 100% quicker (it could however be a bit less accurate but sometimes speed is more important):

benjamin@nas:~$ od -An -x -N 20 /dev/random|tr -d " \n"
807d5a0f1d6ae58e225cd37b8e35f3a15733813e

Blazingly fast sshfs

I’ve been using SSHFS for a while now as my “Swiss army knife” tool for quickly transferring a collection of files between two computers. I’m usually too lazy to bother with setting up an NFS export or SMB share and SSHFS does the trick nicely (as does rsync but it’s less interactive). This tool allows you to mount a directory on a remote system using only an SSH connection.

However, on my LAN the overhead of SSH encryption and compression gets in the way of transfer speeds. So on a trusted network, you can mount SSHFS like this:

sshfs -o Ciphers=arcfour -o Compression=no server://some/folder /mnt/some_local_folder

This will:

  1. Use the ‘arcfour’ cipher which is the fastest encryption method (and not very safe but we don’t care since it’s a trusted network)
  2. Disable the built-in compression SSH uses by default

The difference in transfer speed is very big, copying files from my Sheevaplug ARM server (which doesn’t has a very fast CPU) went from about 1.1 megabyte/s to 10 megabyte/s (wire speed on my 100mbit network basically).

By the way, if you still want to use rsync run it like this for the same setup:

rsync -e"ssh -c arcfour -o Compression=no" ...rest of rsync cmd...

Search bash history with arrow keys

This is a nice trick to selectively browse your bash history with the up/down arrow keys. Replicated from here for my convenience:

Create ~/.inputrc and fill it with this:

"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on

This allows you to search through your history using the up and down arrows i.e. type “cd /” and press the up arrow and you’ll search through everything in your history that starts with “cd /”.

Converting OSStatus to plain text

Just a small snippet so I don’t forget. Here’s how you convert an OSStatus to plain text:

OSStatus x = AudioSessionSetActive(true);
NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:x userInfo:nil];

Book review: Sewer, Gas & Electric (Public Works Trilogy) by Matt Ruff

../../../_images/ruff_sewer_gas_electric.jpg

Because I just finished this book, I thought it would be nice to post a short review here. To start, here’s the description from the book sleeve:

The year is 2023. High above Manhattan, human and android steelworkers are constructing a new Tower of Babel for billionaire Harry Gant. In the streets below, a Wall Street takeover artist has been murdered, and Gant’s crusading ex-wife has been hired to find out why. Accompanying her is Ayn Rand, resurrected from the dead and bottled in a hurricane lamp to serve as an unwilling assistant. Other characters in this extraordinary novel include submarine ecoterrorist Philo Dufrense; a Volkswagen Beetle possessed by the spirit of Abbie Hoffman; and Meisterbrau, a mutant great white shark running loose in the sewers beneath Times Square-all of whom, and many more besides, are caught up in a vast conspiracy involving Walt Disney, J. Edgar Hoover, and a mob of homicidal robots.

Now that you read that, you probably understand why I didn’t even try to write a better summary right? This book lives at an intersection of Hitchhikers Guide to the Galaxy and the work of Neal Stephenson (Reamde, Cryptonomicon, Anathem). Matt Ruff’s writing style is very humorous and contains a lot of very strange characters and subplots just like HHGTTG. Coupled with this is the epic scale of the book and the very detailed descriptions of the world the story is set in that I have grown to love in Neal Stephenson’s books. The book at times is almost written like the plot of an action movie (again echoing Reamde) and once you start it’s hard to put it down. Like other books with complex storylines, the ending is never as good as the actual story (hard to wrap things up in a reasonable way usually) but this book actually does a decent job! Highly recommended!

Amazon link

Adjusting timestamps on JPG and regular files

Having returned from a holiday recently, I found out that my camera was set to a different timezone. Oh dear, now all the (EXIF) timestamps are wrong! Luckily there’s a tool called ‘exiv2’ on Debian that can easily fix this (among many other functions):

First, let’s adjust all timestamps and deduct 1 hour from them:

exiv2 -a -1 *.jpg

If you need to add time, specify a positive number of hours after the ‘-a’ parameter

Next, we want to adjust the file timestamp to match the new EXIF date/time:

exiv2 -T *.jpg

Now we want to adjust the timestamps for any .MOV files as well. Since there is no EXIF tag, I will just adjust the file timestamps directly with ‘touch’:

for i in *.MOV; do touch -r $i -d '-3600 seconds' $i; done

So what does this last line do? For each MOV file, it takes the current timestamp (-r) as a reference and deducts 3600 seconds (1 hour) from the timestamp. Wrap this in a loop to process all files and you’re done! Quick and easy fix and now the timestamps will line up again with my other devices :)

Changing SD card in sheevaplug fails to boot

When I replaced the SD card in my Sheevaplug this weekend, the device failed to boot from it. As it turns out, some SD cards are more quickly initialized than others. To fix this, follow these steps:

  1. Attach a min-USB cable to the plug device
  2. On a debian PC execute these steps to attach a serial console (you might need to reboot the unit):
modprobe usbserial vendor=0x9e88 product=0x9e8f
modprobe ftdi_sio vendor=0x9e88 product=0x9e8f
sudo apt-get install cu
chown uucp /dev/ttyUSB0
cu -s 115200 -l /dev/ttyUSB0
  1. In the u-boot console, “printenv bootcmd_mmc” will output something like this:
mmcinit; ext2load mmc 0:1 0x00800000 /uImage; ext2load mmc 0:1 0x01100000 /uInitrd
  1. The trick is to add another ‘mmcinit;’ step to give the device some extra time to initialize. Copy the value from step 3, add in another “mmcinit; ” like this:
setenv bootcmd_mmc 'mmcinit; mmcinit; ext2load mmc 0:1 0x00800000 /uImage; ext2load mmc 0:1 0x01100000 /uInitrd'
  1. Now save the u-boot environment and reboot:
saveenv
reboot

Adapted from http://info4admins.com/sheevaplug-boot-from-sd-card-fails-on-power-up/ and http://www.fredshack.com/docs/sheevaplug.html

Loopback mounting an image file with partitions

When you use ‘dd’ to image an entire disk to an image file you need to calculate an offset to loopback-mount a specific partition. Steps:

  1. Use fdisk to print the relevant info
fdisk -ul disk.img

Look for this line:

Units = sectors of 1 * 512 = 512 bytes
  1. Suppose we want to mount the second partition, look at the ‘Start’ column of the fdisk output:
   Device Boot      Start         End      Blocks   Id  System
disk.img1              16        8255        4120   83  Linux
disk.img2            8256    15659007     7825376   83  Linux
  1. To mount the second partition execute this command:
mount -o loop,ro,offset=$((512*8256)) disk.img /mnt/tmp

The trick here is to calculate the offset as UNIT_SIZE*PARTITION_START. Happy restoring!

.ecryptfs recovery

I’m experimenting with ecryptfs, a tool that uses transparant file-level encryption.

What I was wondering about is: what happens when you delete the “$HOME/.ecryptfs” directory? As it happens, the recovery is easy (as long as you have the mount passphrase safely recorded somewhere):

  1. Optional: move $HOME/.ecryptfs dir out of the way
  2. mv $HOME/.Private $HOME/.OldPrivate
  3. ecryptfs-setup-private
  4. Enter your login passphrase (to unlock the keyring)
  5. Enter your old mount passphrase
  6. Move all files from $HOME/.OldPrivate into $HOME/.Private
  7. ecryptfs-mount-private

And there are your files again!

Undeleting a partition

I recently made the mistake of accidentally deleting the wrong partition in the Windows disk management feature. This tool allowed me to scan/recover the partition without incident: http://www.cgsecurity.org/wiki/TestDisk

They have binaries for almost every operating system available and it works great if you haven’t touched the disk after you deleted the partion. Phew…