Linux tips and tricks

This page contains a few tips and tricks for Linux. I spent some time on solving a few specific problems, and I decided to put the solutions online, in hope they may be useful for somebody else, too.

Copy a hard drive partition with bad sectors

If you have a large external disk then it is a great idea to make a backup copy of the entire partition(s) from the hard drive in your laptop computer. However, in the case of a partition with bad sectors, and all my attempts to copy it using standard Linux tools ended up with an error message and a cancellation of the copying process. Therefore, I wrote a simple program myself that would skip the bad sectors and replace their contents with some dummy data indicating the problem. You are welcome to download the source code of the program, compile it (note that you may need to install the GNU C++ compiler package in some Linux distributions in which it is not available by default). Step-by-step instructions are below:

  1. Download copydisk.cpp
  2. Install the GNU C++ compiler if necessary, e.g., in a Debian-derived distro type something like this:
    sudo apt-get install g++
  3. Compile:
    g++ -o copydisk copydisk.cpp
  4. Copy the partition ("sda1" in this example):
    ./copydisk /dev/sda1 imagefile.bin logfile.txt
  5. At a later time you can mount the image, for example, in order to retrieve copies of individual files (replace "ntfs" with the appropriate file system type):
    mkdir /mnt/diskimg; mount -o loop,ro -t ntfs imagefile.bin


Fix a USB stick Ubuntu installation

After having installed Ubuntu 12.04 LTS on a USB memory stick using the recommended Universal USB Installer program, it turns out that there are two dead links in the root directory of the installation: "/vmlinuz" and "/initrd.img". The first one causes problems when installing some packages, but is fortunately very easy to fix:

sudo rm /vmlinuz
sudo ln -s /cdrom/casper/vmlinuz /vmlinuz

The other one is apparently more difficult to fix, because there seems to be no file with the requested name in any directory near-by. However, there is a file with a similar name, but the extension ".lz" instead of ".img". If this is a good replacement (which I guess might be the case) then a suggested correction is as follows:

sudo rm /initrd.img
sudo ln -s /cdrom/casper/initrd.lz /initrd.lz

Note: The same applies to Ubuntu 10.10, 11.04, and probably some other versions, too. However, Ubuntu 13.04 has a different layout of some files, so this correction doesn't work for that version.


Use x11vnc in Knoppix 6.7.0

In the DVD distribution of Knoppix 6.7.0, a menu item for running x11vnc is missing. Unfortunately, just typing the command "x11vnc" in a text terminal window doesn't help much, because a bunch of command-line arguments are necessary in order to run this program in a useful way. So here is the command line from the menu item which I found in Knoppix 6.2.1 (I think that you don't need to copy the last argument "-o..." if running from a text terminal window):

x11vnc -gui tray=setpass -rfbport PROMPT -bg -o %%HOME/.x11vnc.log.%%VNCDISPLAY