Page extracted from (now defunct) Przemoc's wiki: http://wiki.przemoc.net/tips/linux.
Apparently it's still useful for some of you. :-)
Return to Przemoc's network.

linux tips

Problems

NetBeans 6 - empty window in installer

Turn of Compiz/Beryl and restart installer.

Debian/Ubuntu and VirtualBox with host interface networking and bridging

Have you followed instructions and now you have network problems with host (but not with guest)?

# << start of lacking part
auto eth0
iface eth0 inet manual
    up ifconfig $IFACE 0.0.0.0 up
    down ifconfig $IFACE down
# >> end of lacking part
 
auto br0
iface br0 inet dhcp
    bridge_ports eth0

Without lacking part eth0 interface conflicts with br0, because they have the same address.

UPDATE: Above information is suitable only for VirtualBox < 2.1

Tasks

GRUB reinstallation

mount /dev/sdXY /mnt/sys
mount -t proc none /mnt/sys/proc   # or --bind
mount -o bind /dev /mnt/sys/dev    # or --bind
chroot /mnt/sys
#mount /dev/sdXZ /boot             # if needed
grub-install /dev/sdX

Setting processor affinity

taskset -p 0x00000001 13545
taskset -cp 1 13545
taskset -cp 3,4 13545

Mounting partition from VDI fixed-size image

WARNING: You must have little-endian machine and this how-to works only with primary partitions.

UPDATE: fdisk was permanently replaced with more reliable sfdisk.

UPDATE2: I divided this how-to into sections and wrote about usage of my new vdiwrap shared library.

Locate partition - manual way

First test if your VDI image is fixed-size.

$ od -j76 -N4 -td4 image.vdi | awk 'NR==1{print $2;}'    # read 4-byte unsigned int from offset 76
2                                                        # "2" means that this is a fixed-size image

You must find out where virtual disk exactly starts.

$ od -j344 -N4 -td4 image.vdi | awk 'NR==1{print $2;}'   # read 4-byte unsigned int from offset 344
33280                                                    # your data offset

Next step is copying beginning of the virtual disk to some file. I named it vdstart

$ dd if=image.vdi of=vdstart bs=1 skip=<data offset> count=1b

Now look at partition table.

$ /sbin/sfdisk -luS vdstart                              # list partition table (units = sectors)
Disk vdstart: cannot get geometry

Disk vdstart: 0 cylinders, 255 heads, 63 sectors/track
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
 vdstart1   *        63  16771859   16771797  83  Linux
 vdstart2             0         -          0   0  Empty
 vdstart3             0         -          0   0  Empty
 vdstart4             0         -          0   0  Empty

Locate partition - half-automatic way

If you want to mount logical partition you have to use this method. Create new file vdiwrap.c with code available in my gist: 571086. Compile it accordingly to instructions, i.e.

$ gcc -fPIC -c               -o vdiwrap.o  vdiwrap.c &&
  gcc -nostdlib -shared -ldl -o vdiwrap.so vdiwrap.o

Run sfdisk directly on VDI fixed-size image with LD_PRELOAD equal to ./vdiwrap.so:

$ LD_PRELOAD="./vdiwrap.so" /sbin/sfdisk -qluS image.vdi

Look at the printed partition table. In output you will find VDI data offset needed in the next step.

Math

Time for some math. Calculate offset (and length if you plan to mount encrypted partition) of chosen partition:

offset = <data offset> + <start sector> * 512
length = <number of sectors> * 512

In my image there is only one partition, so I have no choice: offset = 65536, length = 8587160064.

Mounting

Last part is mounting (turn off VM which uses your VDI image) and you must be superuser to do this (I'm using sudo, but if you haven't configured it, try su -c):

$ sudo mount image.vdi <mount point> -t <filesystem> -o loop,offset=<offset>  # add sizelimit=<length>
                                    # if partition is encrypted; your losetup must support this option

In my case I ended with:

$ sudo mount image.vdi /mnt/vd -t ext3 -o loop,offset=65536,ro

Maybe I should write some script for automating this? 8-)

Mounting partition from VMDK image

WARNING: FUSE and libfuse are required.

  1. Download VMware Server 2 suitable for your machine (uname -m), e.g. VMware-server-2.0.2-203138.x86_64.tar.gz in my case.
  2. Extract 3 files to some directory. It will be /opt/vmware-mount in this example.
    DIRECTORY=/opt/vmware-mount
    mkdir -p $DIRECTORY && cd $DIRECTORY
    echo "
    vmware-server-distrib/lib/lib/libcrypto.so.0.9.8/libcrypto.so.0.9.8
    vmware-server-distrib/lib/lib/libssl.so.0.9.8/libssl.so.0.9.8
    vmware-server-distrib/bin/vmware-mount
    " | tar -xT /dev/stdin --strip 2 -f PATH_TO_DOWNLOADED/VMware-server-2.0.2-203138.i386.tar.gz
    mv lib/*/* .
    rm -r lib
  3. List available partitions:
    $DIRECTORY/vmware-mount -p PATH_TO_YOUR/image.vmdk
  4. Mount chosen partition:
    $DIRECTORY/vmware-mount PATH_TO_YOUR/image.vmdk PARTITION_NUMBER MOUNT_POINT

Making SOCKS proxy transparent

UPDATE: transocks_ev is unstable, therefore using a lot better redsocks is safer choice. Sorry for this late update.

If we have a limited connectivity to the world from current location, but still can connect to a shell account fully open to the world (or open to the other non-public network), than dynamic port forwarding available in ssh can save us. This feature (accessible by -D option) in fact makes ssh acting as SOCKS server. OK, but what can we do if our application doesn't support SOCKS proxy? It's important question, because vast majority of software is unaware of such protocol. In Linux we have a great tsocks (http://tsocks.sourceforge.net/), shell wrapper which transparently allows an application to use SOCKS proxy. In Windows there are FreeCap (http://www.freecap.ru/eng/) and Vilongu HTTP SOCKS Tunneler http://www.connectionresume.com/vilongu/ - they do the same thing, but in a different way. Nice, but what if we have dozens of machines to set up. Teaching all users how to use any of mentioned application can be also really inconvenient. Making SOCKS proxy transparent will solve (almost?) all our problems. Is it feasible? YES, but you must have an access to superuser account on a gateway server (it can be also any other server, but gateway is used here for simplicity).

I'm assuming that you already have SOCKS server bound to localhost on standard port 1080 (e.g. you started ssh with -D1080).

  1. Install libevent (http://www.monkey.org/~provos/libevent/). It can be already available in your distribution repository. This will be used for compiling in next step, so you must get development package (usually libevent-dev).
  2. Download redsocks (http://darkk.net.ru/redsocks/ - either use git to clone repository, get latest snapshot or get locally archived revision from 2009.05.21: redsocks-a37aa7a.tar.gz) and build it using make.
  3. Configure redsocks by editing redsocks.conf. I assume that it contains:
    base {
    	log_debug = on;
    	log_info = on;
    	log = "file:/home/users/przemoc/redsocks/redsocks.log";
    	daemon = on;
    	redirector = iptables;
    }
     
    redsocks {
    	local_ip = 0.0.0.0;
    	local_port = 12345;
    	ip = 192.168.0.1;
    	port = 1080;
    	type = socks5;
    }
  4. Configure, tune and run below script first time as root with one argument iptables and later as normal user.
    #!/bin/sh
     
    # Where to find iptables
    IPTABLES="/usr/sbin/iptables"
     
    # Where to find redsocks
    REDSOCKS_DIR="/home/users/przemoc/redsocks"
    REDSOCKS="$REDSOCKS_DIR/redsocks"
     
    # Port that is redsocks listening on
    REDSOCKS_PORT="12345"
     
    # Location of our SOCKS-server
    SOCKS_HOST="192.168.0.1"
    SOCKS_PORT="1080"
     
    # Start redsocks
    if [ "$USER" != "root" ]; then
    	echo -n 'Restarting redsocks... '
    	pkill -U $USER redsocks 2>/dev/null
    	sleep 1
    	cd $REDSOCKS_DIR && $REDSOCKS
    	if [ $? -eq 0 ]; then
    		echo Done
    	else
    		echo Error
    	fi
    	exit 0;
    elif [ "$1" != "iptables" ]; then
    	exit 0
    fi
     
    $IPTABLES -t nat -D PREROUTING -p tcp -j REDSOCKS_FILTER 2>/dev/null
    $IPTABLES -t nat -D OUTPUT     -p tcp -j REDSOCKS_FILTER 2>/dev/null
    $IPTABLES -t nat -F REDSOCKS_FILTER 2>/dev/null
    $IPTABLES -t nat -X REDSOCKS_FILTER 2>/dev/null
    $IPTABLES -t nat -F REDSOCKS 2>/dev/null
    $IPTABLES -t nat -X REDSOCKS 2>/dev/null
     
    # Create our own chain
    $IPTABLES -t nat -N REDSOCKS
    $IPTABLES -t nat -N REDSOCKS_FILTER
     
    # Do not try to redirect local traffic
    $IPTABLES -t nat -I REDSOCKS_FILTER -o lo -j RETURN
     
    ### Below whitelist and blacklist cannot operate together.
    ### If you want to change it, refactor the code. It's simple.
     
    # Redirect only specified addresses and do not try redirect other traffic. (whitelist option)
    $IPTABLES -t nat -A REDSOCKS_FILTER -m iprange --dst-range 192.168.0.10-192.168.0.30 -j REDSOCKS
    $IPTABLES -t nat -A REDSOCKS_FILTER -d 126.0.0.0/8 -j REDSOCKS
    $IPTABLES -t nat -A REDSOCKS_FILTER -j RETURN
     
    ## Do not redirect LAN traffic and some other reserved addresses. (blacklist option)
    #$IPTABLES -t nat -A REDSOCKS_FILTER -d 0.0.0.0/8 -j RETURN
    #$IPTABLES -t nat -A REDSOCKS_FILTER -d 10.0.0.0/8 -j RETURN
    #$IPTABLES -t nat -A REDSOCKS_FILTER -d 127.0.0.0/8 -j RETURN
    #$IPTABLES -t nat -A REDSOCKS_FILTER -d 169.254.0.0/16 -j RETURN
    #$IPTABLES -t nat -A REDSOCKS_FILTER -d 172.16.0.0/12 -j RETURN
    #$IPTABLES -t nat -A REDSOCKS_FILTER -d 192.168.0.0/16 -j RETURN
    #$IPTABLES -t nat -A REDSOCKS_FILTER -d 224.0.0.0/4 -j RETURN
    #$IPTABLES -t nat -A REDSOCKS_FILTER -d 240.0.0.0/4 -j RETURN
    #$IPTABLES -t nat -A REDSOCKS_FILTER -j REDSOCKS
     
    ## Do not redirect traffic for the SOCKS-Server
    ## Not needed if server is not on a whitelist or is already blacklisted.
    #$IPTABLES -t nat -I REDSOCKS -p tcp -d $SOCKS_HOST --dport $SOCKS_PORT -j RETURN
     
    # Redirect all traffic that gets to the end of our chain
    $IPTABLES -t nat -A REDSOCKS   -p tcp -j REDIRECT --to-port $REDSOCKS_PORT
     
    ## Filter all traffic from the own host
    ## BE CAREFULL HERE IF THE SOCKS-SERVER RUNS ON THIS MACHINE
    $IPTABLES -t nat -A OUTPUT     -p tcp -j REDSOCKS_FILTER
     
    # Filter all traffic that is routed over this host
    $IPTABLES -t nat -A PREROUTING -p tcp -j REDSOCKS_FILTER
     
    echo IPtables reconfigured.
  5. Now all hosts with your machine set as a gateway use SOCKS proxy accordingly to iptables rules. Transparently!
  6. Open another beer bottle and enjoy. 8-)

Preparing Ubuntu 9.10 for CUDA 2.3

  1. nVidia 190 drivers
    sudo add-apt-repository ppa:nvidia-vdpau/ppa
    sudo apt-get update
    sudo apt-get install nvidia-190-modaliases nvidia-glx-190 nvidia-settings-190
    
  2. GNU Compiler Collection 4.3
    sudo apt-get install gcc-4.3 g++-4.3
    sudo update-alternatives --remove-all gcc
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 43 --slave /usr/bin/g++ g++ /usr/bin/g++-4.3 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.3
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 44 --slave /usr/bin/g++ g++ /usr/bin/g++-4.4 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.4
    sudo update-alternatives --config gcc    # choose gcc 4.3
    
  3. CUDA SDK requirements
    sudo apt-get install libglut-dev libxi-dev libxmu-dev