Thursday, June 2, 2011

Test a serial port

Use a loopback cable: connect pin 2 and 3. Data sent out come back on the receive line.

optionally configure it to disable echoing: stty -F /dev/ttyS0 -echo
Otherwise the characters are sent again and again in an infinite loop...

in a console read from the line: cat /dev/ttyS0
in another console write to the line: echo "test message" > /dev/ttyS0

check if it is printed in the first console...


This is to install a minimum ubuntu. It can be used to install to any folder, i.e. some local folder on which to chroot later, or a mounted drive.

First install the debootstrap package
sudo apt-get install debootstrap

Then start the job:
sudo debootstrap --arch i386 hardy /mnt

This will install hardy on /mnt by downloading all required packages from the singapore archive.

Then chroot in the new created environment: sudo chroot /mnt
create a root passwd: passwd
write /etc/fstab, /etc/network/interfaces and /etc/hostname
mount proc and pts:
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devpts devpts /dev/pts
configure language: apt-get install language-pack-en
under Debian: apt-get install locales; dpkg-reconfigure locales
install a kernel and GRUB: apt-get install linux-image grub

write MBR and create GRUB images:
(from outside the chroot, i.e. from a normal console): sudo grub-install --root-directory=/mnt /dev/sdc
This will install the GRUB images in /mnt/boot/grub and write the MBR on disk sdc
Then from the chroot environment: update-grub
this will write a menu.lst (the GRUB config) for you

then install whatever you want using apt-get

Remove executable permission on all files except directories

find . -type f -print0 | xargs -0 chmod -x

find . : search the current directory recursively
-type f : select only regular files (-type d would select directories)
-print0 : (where 0 is a zero) print the list with each file separated by a null character rather than a space. This allow handling filenames with a space in it.

xargs : execute a cmd on each argument
-0 : (where 0 is a zero) instruct xargs that argument list is null charater separated rather than by a space. Corresponds to -print0 in find.
chmod -x : remove executable permission

password-less login with ssh

On the originating machine:
ssh-keygen -t rsaSave the key in the default file (~/.ssh/id_rsa) and do not use a passphrase. This will create a file ~/.ssh/

Add the contents of this file to the file ~/.ssh/authorized_keys on the remote machine (i.e., the machine on which you want to log on). Whereas the file is world readable, the authorized_keys file should only be readable by you. Use chmod to set the permissions accordingly.

Create a zip from a m3u playlist

grep -v "#" test.m3u | zip -j test -@ -

"-v #" is used to discard lines starting with # (comments maybe...)
-@ is to get input from the file, here -, i.e. stdin, i.e. the output of grep
-j is to junk the path and store only the file

mplayer / mencoder / ffmpeg recipes

Concatenate AVI files
cat file1.avi file2.avi > big.avi
mencoder -ovc copy -oac copy -o out.avi -forceidx big.avi

Split video
From a start pos (4 mins 20 secs) to end:
mencoder -ss 00:04:20 -oac copy -ovc copy in.avi -o out.avi
From beginning to an end pos (11 mins 44 secs):
mencoder -endpos 00:11:44 -oac copy -ovc copy in.avi -o out.avi
From a start pos (4 mins 20 secs), extract 5 mins of video:
mencoder -ss 00:04:20 -endpos 00:05:00 -oac copy -ovc copy in.avi -o out.avi

Extract audio from video
mplayer -dumpaudio videoFile.avi -dumpfile audioFile.mp3
mplayer -ao pcm -vo null videoFile.avi
it dumps the audio to audiodump.wav

Best is to use ffmpeg:
ffmpeg -i <input media> -ss <bgn time> -t <duration> -acodec copy -vn <output file>
  • -i input media is the source media (i.e. an avi file)
  • -ss bgn time: Seek to given time position in seconds.  "hh:mm:ss[.xxx]" syntax is also supported.
  • -t duration: duration of the segment to extract
  • output file: the output file, e.g. a mp3 file (depending on the input's audio encoding)
To encode the audio stream use a different -acodec option and see extra options (bitrate, etc.)

grep, sed, awk ...

Search TEX files for \label commands and check for double ref:

cat *.tex | grep "label{" | sed 's/.*{\(.*\)}.*/\1/' | uniq -c | sort -n

extract the IP address from ifconfig

$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:13:72:BE:FF:63
inet addr: Bcast: Mask:
inet6 addr: fe80::213:72ff:febe:ff63/64 Scope:Link
RX packets:4801968 errors:0 dropped:0 overruns:0 frame:0
TX packets:1088918 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2559338610 (2.3 GiB) TX bytes:90195114 (86.0 MiB)

$ ifconfig eth0 | awk '/inet addr/ {FS=" "; gsub("addr:","",$2); print $2}'

Internet access from chroot

I always had internet connection from a chroot environment. However, when using wireless, it seems that it's requires one more step: resolv.conf must contains the address of a DNS server (the gateway usually).

echo nameserver > /etc/resolv.conf

Setting a custom terminal prompt

In .bashrc, add:

PROMPT_COMMAND='DIR=`pwd|sed -e "s!$HOME!~!"`; if [ ${#DIR} -gt 30 ]; then CurDir=${DIR:0:12}...${DIR:${#DIR}-15}; else CurDir=$DIR; fi'
PS1="[\$CurDir] \$ "

Backup a linux system


Summary: the backup can be done from the system directly (i.e. no need to run from a live CD). We don't want to backup some directories: proc, sys and dev are virtual filesystems... lost+found is not useful. -p option in tar is to preserve permissions.

 sudo tar -cvpzf /backup.tar.gz -–exclude=/backup.tar.gz --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/dev --exclude=/mnt --exclude=/media /  

For restoration: this must be done from another system, typically a live cd. We will restore to /mnt:

 sudo tar -xvpzf backup.tar.gz -C /mnt  

then recreate the missing directories:
 mkdir /proc /lost+found /sys /mnt /media  

and install grub:
 sudo grub-install --root-directory=/mnt /dev/sdc  

Purge packages

The purge package (or the --purge) option in apt-get deletes configuration files along with the package content.

To identify packages that were removed but not purge, use pdkg -l to list all packages, and grep ^rc to find the lines that start with rc.

Finally, the whole command is:

sudo apt-get purge `dpkg -l | grep ^rc | cut -d' ' -f3 | xargs echo`

undelete files from a VFAT partition

Use testdisk:
sudo testdisk /dev/sdb

- select the partition and type (Intel)
- select advanced operation, then select undelete
- find the files to undelete and press c (copy), choose where to copy
- done!

More details here:

Wednesday, June 1, 2011

Checking out TI's EZ430-Chronos watch. Besides being a watch, it offers a 3-axis accelerometer, a temperature sensor, a pressure sensor, and acts as a wireless hub (to integrate with other wearable sensors), etc. It can be programmed too.

Here are the full specs along with a list of applications

Here is the demo video