Emacs Undo

Did not know that instead of the finger breaking C-_, undo on emacs can also be done using C-/ and C-x u, though I don't like the last one.

[Emacs] Using emacsclient

I have been using Emacs over gnu screen so that I can have my buffers and windows from work when I connect from home. The only problem is that over gnu screen, the Control-Up/Down/... does not work and instead I get the 5A, 5B, ... characters. The same is true for Alt and Shift. However, if I use Emacs over bash rather than gnu screen,  it works fine but I can't get my old buffers as it creates a new instance. Enter Emacsclient for the rescue.

Emacsclient can not only be used to tell a running Emacs session to visit a file but it can also be used to open a new Emacs frame and have your buffers from the other frame shown here. (Viola)

In order to get this to work, you need to start the Emacs server. This can be either done at run-time by
M-x server-start
OR, add it to your .emacs file
;; Start the emacs server
(server-start)

Now, create a few aliases in your .bashrc.
### Open emacs in console mode, non-gui
alias em='emacs -nw'
### Tell running emacs to visit a file
alias ecf='emacsclient -n'
### Start emacs with new frame
alias ecn='emacsclient -nw -c'

So, now I can run ecn and it would start a new Emacs instance but still show me the buffers from the existing one.

To close this frame you can type C-x 5 0

X11Vnc Shift Key Fix

There is a known bug with X11VNC where the Shift key does not work (ref). The quick fix is to add -xkb to the command string as suggested here.

My X11Vnc string looks like:

$ x11vnc -usepw -forever -xkb -display :0

Finding read throughput using "dd"

If you are trying to find the read throughput of a device (like an SD card), you can do the following:

1. If possible unmount the device but keep it connected. Let's say you are looking at an SD card at sdd. (Don't worry about the volumes)
2. Run the following:
sudo dd if=/dev/sdd of=/dev/null bs=50M count=1

This will print the read throughput, something like this:
1+0 records in
1+0 records out
52428800 bytes (52 MB) copied, 2.62946 s, 19.9 MB/s


Now, if the device is mounted, you might see a variability in the throughput because of file system page caching between runs. To avoid that, free the page caches using:
sync; echo 1 | sudo tee /proc/sys/vm/drop_caches

NOTE: To free deentries and inodes the following can be used:
sync; echo 2 | sudo tee /proc/sys/vm/drop_caches

If you want to free up everything (page caches, deentries and inodes), do the following:
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches

Use tee to set values in proc or sysfs

There are times when you can't do a "echo something > /proc/somewhere", especially when you got to use "sudo".

Using tee helps:

echo value | sudo tee /proc/somewhere

Control-S freezes puttty

Saw a weird behavior yesterday. CTRL-S would freeze putty. It turned out to be problem related to XOFF which is triggered by CTRL+S (the terminal will accept keys but it won't show the result for that, weird!). A simple fix is CTRL+Q to trigger XON. However, that does not work well with emacs which relies a lot on CTRL+S (for searching and saving)

Just add the following to .bashrc file:

stty ixany
stty ixoff -ixon

.thumbnails eating up space in $HOME

I have a restriction of 50K # of files that can be created in the $HOME folder at work and for no apparent reason I reached that limit a couple of days back. So, there was something that had created 50K files in $HOME and I did not know about it.

Ran the following:

$ for x in `find $HOME -name "[.]*"`; do echo "$x has `find $x | wc -l` file"; done

The result listed a folder $HOME/.thumbnails that had around 47776 files. It appears that Nautilus creates thumbnails of every picture or PDF that you have opened. However, Nautilus does not delete the files.

If you have such a restriction and need to get rid of these files, add a crob job that does deletes the files older than 7 days.

$ find ~/.thumbnails -type f -atime +7 -exec rm {} \;

Another method is to change the Gnome settings. Run the following:

$ gconf-editor

Under desktop -> gnome -> thumbnail_cache change the values for maximum_age and maximum_size

Using diff

Remember: when using diff, use the following flags

diff -Naur


-N = Treat absent files as empty
-a = Treat all files as text
-u = Output 3 lines of unified content
-r = Recursively compare any subdirectories found

netconsole

Yet another mechanism to get the kernel debug messages is netconsole which sends the messages over Ethernet. You would not be able to get those early printks or crashes but it is useful nonetheless.

Setting up netconsole is fairly easy. You can either pass the netconsole parameters at the boot time or enable it after the machine has booted using modprobe.

Setting netconsole using boot parameters

Add the following parameter to the kernel boot string
netconsole=[s-port]@[s-ip]/[dev],[t-port]@[t-ip]/[t-mac]

s-port = source port, default is 6665
s-ip = source IP address
dev = device (eth0, eth1 ...)
t-port = target port, default is 6666
t-ip = target  IP address
t-mac = target MAC address

For example:
netconsole=1234@10.1.1.152/eth0, 1235@10.1.1.153/11:22:33:44:55

Setting netconsole using modprobe

It is preferred to use modprobe instead of insmod
$ sudo modprobe netconsole netconsole="@/,@t-ip/"

For example:
$ sudo modprobe netconsole netconsole="@/,@10.1.1.153/"

Target (receiver) side 

Use netcat to get the messages

$ nc -l -u [t-port]

The full documentation on using the netconsole can be found in the "Documentation/networking/netconsole.txt" file in the kernel folder.

Online resources -  1, 2, 3 (some of these might be dated ... )

Kernel debugging using KGDB

KGDB is full integrated with the latest kernel. These instructions are for linux-2.6.31.12. Not sure if these will work for any previous or later versions.
  1. make menuconfig and select the following
    1. Select Kernel Hacking → KGDB: kernel debugging with remote gdb
      1. Select KGDB: use kgdb over the serial console
  2. Build the kernel
     make bzImage; make modules; sudo make modules_install
  3. Install the kernel. Copy bzImage, System.map and .config into /boot
  4. On another machine (say remote) where the serial null modem cable is connected, copy the entire kernel folder that you compiled on the target machine. 
  5. Restart the target machine.  At the grub menu, select your kernel, edit it and add the following at the end of the kernel line:
     kgdboc=ttyS0,115200 kgdbwait
    This is assuming that you have a serial port and it is connected on ttyS0. Boot into your kernel. It will wait for gdb to connect.
  6. On the remote machine, go in the kernel folder you copied over and debug vmlinx by starting gdb
    1. gdb ./vmliux
    2. Set the baud rate - (gdb) set remotebaud 115200
    3. Connect to the target - (gdb) target remote /dev/ttyS0
    4. Run the kernel - (gdb) continue
For more details, refer to the docbook on KGDB in the kernel documentation folder. Just do a make htmldocs to convert into a readable format. You would have to install xmlto first. sudo aptitude install xmlto should help.

    Force fsck on reboot

    If you want to force an fsck check on your drives on reboot do the following:

    sudo touch /forcefsck
    sudo shutdown -r now

    Serial console debugging

    Setting up the serial console

    1. First find which devices are attached to the system

    dmesg | grep "tty"

    2. For the device found above (say ttyS0), create the /etc/event.d/ttyS0 file with the following contents

    # ttyS0 - getty
    #
    # This service maintains a getty on ttyS0 from
    # the point the system is started until it is
    # shut down again.

    start on runlevel 2
    start on runlevel 3
    start on runlevel 4
    start on runlevel 5

    stop on runlevel 0
    stop on runlevel 1
    stop on runlevel 6

    respawn
    exec /sbin/getty -L 115200 ttyS0 vt102


    3. Edit /etc/securetty and add ttyS0

    4. During reboot edit the kernel in grub menu (press 'e'). At the end of the kernel line add console=ttyS0,115200n8 tty1

    5. Boot into your kernel

    6. Once you log in, if you want all the dmesg output to go on the serial console do the following:
    sudo tail -f /var/log/kern.log > /dev/ttyS0

    Setting up the client

    1. Open minicom and set the port as the one you are talking on, it could be ttySn or ttyUSBn depending on if you are using a serial port or a USB to serial converter. (e.g. ttyS0, ttyUSB0)

    2. Set the setting as 115200 baud, 8 bits, 1 stop bit, no parity, no flow control

    Update: There is another program called gtkterm if you don't fancy the command line applications. Also I would recommend using konsole to run minicom within as it supports unlimited buffer, which can be very useful if you are trying to look at the /var/log/kern.log of the target machine, for example.

    Update (Apr 20, 2010): Only if you want to have a serial console (like a bash on the serial port) should you do the getty for ttyS0. If you just want all the kernel message sent to the terminals just do the following:

     # echo 8 > /proc/sys/kernel/printk

    Read this article for more details on kernel oops.