Monday, July 18, 2011

Spotify vs. pulseaudio: workaround

I use to run Spotify on linux under wine. It's quite esasy to install, and it works great, and almost out of the box. At least sometimes. I've been running spotify over wine on several installations and sometimes it works just fine, sometimes not, I still don't know what's the difference (didn't have the time to investigate any further). And even sometimes it stops working suddenly, again, who knows why exactly.
Anyway, it doesn't seem to integrate fine with pulseudio. I mean wine, not spotify itself. So the workaround I am using is to start Spotify killing the pulseaudio server (I'm always running it on a per user instance, not daemonized by root).
So the startup command is something like:

Exec=pulseaudio --kill; env WINEPREFIX="" wine "C:\\Program files\\Spotify\\spotify.exe"


Check out the desktop icon that wine creates when you make a regular install; edit the file and change/add the pulseaudio --kill sentence.

As I said, sometimes nevertheless Spotify stops working. I have no workaround for that yet, just have to restart Spotify again. It can be a little annoying. I'm pretty sure there's already a definite solution for that out there (please, somebody tell me if you know ;)).

Tuesday, July 12, 2011

Sound problems on kubuntu 11.04

...yeah, suddenly I had no more sound on Kubuntu 11.04. It had been working up until now, a couple of months after a fresh install, but now sound is gone. I hate this! Yeah, I know KDE on Kubuntu is somewhat buggy, but I like it nonetheless.
However, on Debian I use to take a wonderful script for such situations (yes, sometimes it happened on Debian too), which was /usr/sbin/alsaconf shipped with alsa-utils. But for some reasons I don't know it isn't there anymore, at least not in Kubuntu 11.04. Uninstalling every single kernel module with modprobe -r is a bad idea. What to do?
Hey, there's this other tool, simply /sbin/alsa:

# alsa
Usage: /sbin/alsa {unload|reload|force-unload|force-reload|suspend|resume}


and this works fine, but for one reason: if fails to unload some modules because they're in use, even if I use the force-unload option. Why is that? Because for some reason the '/usr/bin/pulseaudio' is being restarted very fast, before it manages to unload all modules. The trick, although a little ugly:

# chmod a-x /usr/bin/pulseaudio
# alsa force-reload


...And there they are again my sound devices in System Preferences -> Multimedia!

Of course, don't forget to make pulseaudio executable again.

Monday, March 14, 2011

Linux software raid: mdadm

Recently I had to start using linux software raid. I was more used to lvm, but software raid can come pretty handy in certain circumstances. A friend of mine had a dual boot installation linux-windows, and he wanted to be able to access the linux stuff from the windows environment, plus some kind of redundance in order to not lose important data. So I thought about a basic raid-1 in linux (2 partitions mirrored) to provide redundancy. Eventually he could access one of the partitions from the windows install. But than again, I didn't want to use NTFS file systems (I don't really know I they can be mirrored with linux mdadm, haven't tried that out yet). So I decided to use ext3, and use the ext2/ext3 driver available for windows here.
It's a nice tool for accessing linux ext3 file systems, and though it's perfectly able to work in read/write mode, I recommended to mount the partitions only in read mode. But there is an important limitation: in order to be able to use that driver in windows, the file system has to be created with an inode-size of 128, otherwise it won't work (it's a driver limitation) and the partition will not be accessible from windows (it will be reported to not have been formatted). This can (and, in fact has to be done, at file system creation time), with e.g:
mke2fs -i 128 /dev/sda1
Also check out /etc/mke2fs.conf for defaults for ext3/ext4 file system creation.

But my point here is not how to access ext3 file systems from windows. I'm more interested in mdadm in linux. I think it's very esasy to use and more straight forward than lvm. I think it may be easier to understand for the beginner. Of course it lacks the full power of lvm. But I decided to use it also because the windows driver mentioned above has one more limitation: it can't access lvm extents. And one of the things I like most of mdadm is that you can create an array using an already existing file system without harm: it's not necessary to create a new file system on both disks (but be careful with the order you add the disks).

Well, here's my cheat sheet, which btw is based roughly on this wiki. There are a lot of software raid tutorials and howto's out there, many of them probably much better and more detailed then this one, but I deliberately want to keep it short.

· Build the raid:
    You can define the raid array by just using one of the partitions (you can add the other one later):
    mdadm --create /dev/md1 --level=1 --raid-disks=2 missing /dev/sda1

    · Add a disk to the disk array

      Successive partitions can be added any time like this:
      mdadm --add /dev/md1 /dev/sdb1

      · Check out status:
        You can check out the status of your array any time by editing the file /proc/mdstat. Here's some an example of the file content:

        # cat /proc/mdstat Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
        md1 : active raid1 sdb1[0] sda1[1] 104320 blocks [2/2] [UU] 
        unused devices:
         
        where, as you can see, we got one raid1 array (md1) consisting of 2 partitions, /dev/sda1 and /dev/sdb1, and the array status is ok. Of course, in order to both partitions in a raid1 array must be equal in size (check out sfdisk to clone partitions).

        · Mount the array
        The raid array can be mounted just like any other formatted partition, but now the partition is something like /dev/md1 or /dev/md/d1 or so, instead of something like /dev/sdc3. So mount it like:

        mount -t ext3 /dev/md1 /mnt/myraid

        And, if you like, add it to your /etc/fstab. But relative to this, I had a problem with Ubuntu 10.10. Seems like the device wasn't available yet when the mounting script was executed by init, so I put it off the /etc/fstab file and mounted the device from the /etc/init.d/mdadm file that is shipped with the mdadm package (at least in Debian like distros).

        · Raid info:
        Run:
        mdadm --detail --scan
        or
        mdadm --examine --scan
        or, when you already know which array you want to get info about (e.g. size):
        mdadm --query /dev/md1

        · Start the array:
        With commands like the ones listed above you could have been informed that, surprisingly, your array is not active; you can activate it with:
        mdadm -R /dev/md1
        But usually it will be necessary to reassemble the array.


        · Reassemble / restart the array:

        mdadm -Ac partitions -m 1 /dev/md1

        If you check out /proc/mdstat now for array status, you may see something like this:

        md_d1 : active raid1 sdb1[2] sda1[1]
        104320 blocks [2/1] [_U]
        [=========>;...........] recovery = 45.0% (47872/104320) finish=0.0min speed=15957K/sec

        unused devices:



        This is useful if you've run into trouble with the array.

        · Disassemble, modify and reassemble:
        Let's assume you want for some reason be able to access one of the partitions from the array separately, introduce some changes, and you want these changes to be persistent after you reassemble the array again. Here's he only way I found to do that, if e.g. we want to introduce changes in partition /dev/sda1:
        1. detach the other disks from the array:
        mdadm /dev/md1 --fail /dev/sdb1
        2. stop the array:
        mdadm -S /dev/md1
        3. mount the other disk outside the array (disk /dev/sda1)
        4. add or remove files from disk /dev/sda1 mounted
        5. umount disk /dev/sda1
        6. re-arrange the raid array:
        mdadm -Ac partitions -m 1 /dev/md1
        7. re-add the previously marked as failed disk (dev/sdb1) to the array again:
        mdadm --re-add /dev/md/d1 /dev/sdb1
        8. watch /proc/mdadm resync files in the array
        9. mount the disk array again (and watch in /proc/mdstat how they sync)


        I put that into the /etc/init.d/mdadm script in order to be able to access one of the partitions from the windows installation I mentioned in the introduction, make modifications to the file system, and make them persistent after reassembling the array. I append it here, just in case somebody find it interesting (or for my retrieval of it). It's just the file delivered with the package with a few modifications highligthed in blue (sorry for the poor indentation).

        #!/bin/sh
        #
        # Start the MD monitor daemon for all active MD arrays if desired.
        #
        # Copyright © 2001-2005 Mario Jou/3en

        # Copyright © 2005-2008 Martin F. Krafft

        # Distributable under the terms of the GNU GPL version 2.
        #
        ### BEGIN INIT INFO
        # Provides: mdadm
        # Required-Start: checkroot
        # Required-Stop: umountroot
        # Should-Start: module-init-tools
        # Default-Start: S
        # Default-Stop: 0 6
        # Short-Description: MD monitoring daemon
        # Description: mdadm provides a monitor mode, in which it will scan for
        # problems with the MD devices. If a problem is found, the
        # administrator is alerted via email, or a custom script is
        # run.
        ### END INIT INFO
        #
        set -eu

        MDADM=/sbin/mdadm
        RUNDIR=/var/run/mdadm
        PIDFILE=$RUNDIR/monitor.pid
        DEBIANCONFIG=/etc/default/mdadm

        # my variables
        ARRAY=/dev/md1
        DISK=/dev/sdb1
        RAIDMOUNT=/mnt/raid
        MOUNT=/bin/mount
        UMOUNT=/bin/umount

        test -x "$MDADM" || exit 0

        test -f /proc/mdstat || exit 0

        START_DAEMON=true
        test -f $DEBIANCONFIG && . $DEBIANCONFIG

        . /lib/lsb/init-functions

        stop_my_array () {
            $MDADM $ARRAY --fail $DISK
            $UMOUNT $RAIDMOUNT
            $MDADM -S $ARRAY
        }

        start_my_array () {
            $MDADM -A -m1 $ARRAY
            $MDADM --re-add $ARRAY $DISK
            $MOUNT $RAIDMOUNT
        }


        is_true()
        {
           case "${1:-}" in
           [Yy]es|[Yy]|1|[Tt]|[Tt]rue) return 0;;
           *) return 1;
           esac
        }

        case "${1:-}" in
        start)
            if is_true $START_DAEMON; then
            log_daemon_msg "Starting MD monitoring service" "mdadm --monitor"
            mkdir -p $RUNDIR
            set +e
            start-stop-daemon -S -p $PIDFILE -x $MDADM -- \
            --monitor --pid-file $PIDFILE --daemonise --scan ${DAEMON_OPTIONS:-}
            log_end_msg $?
            set -e
            start_my_array
        fi
        ;;
        stop)
            if [ -f $PIDFILE ] ; then
            log_daemon_msg "Stopping MD monitoring service" "mdadm --monitor"
            stop_my_array
            set +e
            start-stop-daemon -K -p $PIDFILE -x $MDADM
            rm -f $PIDFILE
            log_end_msg $?
            set -e
        fi
        ;;
        restart|reload|force-reload)
        ${0:-} stop
        ${0:-} start
        ;;
        *)
        echo "Usage: ${0:-} {start|stop|restart|reload|force-reload}" >&2
        exit 1
        ;;
        esac

        exit 0

        Thursday, March 3, 2011

        translate atax:xx to actual disk (and partition?)

        What if we're getting a message like:

        ata4.01: error: { UNC }

        or

        ata4.01: failed command: READ DMA

        one day in our messages or via dmesg? Well, maybe it's a bug somewhere in the kernel or in a kernel module, but most probably something's wrong with one of your hard drives. Let's assume the latter, and anyway it's a good idea to check it out. If we've got several drives, which one is it?
        That's what happened to me recently and it took me quite a while to find out which disk and partition the messages where referring to. I'm sure there are some great scripts out there to help us out in such a situation, but I didn't find them.

        Well, the steps that could summarize how to find that out could be:
        1. find out in the bootup message what is said about it, something like:

        # dmesg |grep ata4.01|head
        [ 0.962250] ata4.01: ATA-7: ST3320620AS, 3.AAK, max UDMA/133
        (...)

        2. cross compare that with the output from lshw -businfo, e.g.
        # lshw -businfo|grep ST3320620AS
        scsi@3:0.1.0 /dev/sdd disk 320GB ST3320620AS

        (nice option btw this -businfo from lshw)
        There it is, we already got the disk! Now, which partition is it that we're having trouble with?
        Actually, the messages listed above don't refer to partitions at all, but they target the whole disk: ata4.01 is the same as scsi notation 3:0:1:0, which is again (in my system, not everywhere) the same as /dev/sdd.
        This can also be seen with the tool lsscsi:

        # lsscsi
        [0:0:0:0] disk ATA Maxtor 6Y120L0 YAR4 /dev/sda
        [0:0:1:0] cd/dvd TSSTcorp CD/DVDW SH-S162A TS01 /dev/sr0
        [2:0:0:0] disk ATA ST3250824AS 3.AA /dev/sdb
        [3:0:0:0] disk ATA ST3250310AS 3.AA /dev/sdc
        [3:0:1:0] disk ATA ST3320620AS 3.AA /dev/sdd

        as long as we agree (I'm not sure if this is correct) that we can identify:
        ata1 -> 0:0:0:0
        ata2 -> 1:0:0:0
        ata3 -> 2:0:0:0
        and e.g.
        ata2.01 -> 1:0:1:0

        So, if we want to know which partition is having trouble, we need to check out something else in the messages, and look for lines like:
        [24649.946461] Buffer I/O error on device sdd1, logical block 280748933
        or[743.150286] lost page write due to I/O error on sdd1
        or maybe[743.232014] JBD: Detected IO errors while flushing file data on sdd1
        which finally tells us what partition should be a concern to us.

        It still remains to determine if it's a hardware failure or if it's just a corrupt file system (at least to the point, I can't tell). What to do now?
        •  Try to access your data and back them up
        •  Run a file system check. If it's an ext3 or ext4 file system, maybe run a e2fsck -c /dev/sdd1 to check for bad blocks (or reiserfsck --rebuild-tree, but be careful with that, read the man page), but take your time, it's pretty slow.
        But again, first try to access and backup your data.

        Wednesday, January 26, 2011

        One on Pulseaudio

        Well, what if there are several sound cards? In my case I have two sound cards. The thing is up until now I was only using one of then (the other one was blacklisted in /etc/modprobe.d/blacklist. But now I wanted to use it and I managed to make both of them work, supposedly in the way I like: one of them as the default output card, and the other one for recording. But, since the one I now use for recording is the one I used to use for playback, some applications doesn't seem to have been aware of the change (e.g. flashplugin, vlc, amarok (this one needed a restart). Why?
        I use KDE4 (Kubuntu) and I've the 'right' configuration chosen in 'System Configuration'-> 'Multimedia'. But somehow still that doesn't seem to work ...well, it doesn't.

        At least in my case it was an, to me, odd Pulseaudio issue. The solution or workaround for me was quite simple:
        1. create, if it doesn't already exist, a $HOME/.pulse/default.pa file and put a content like:

        alsa_input.pci-0000_01_0b.0.analog-stereo

        where you have to put your correctly identified sound card.
        2. How to identify the sound card? Well you have at least to know how many you have and, from the ones available, which you want to use for playback purposes. That you can do with the command: lspci -v. In my case I get:

        ~$ lspci -v|grep -i audio
        00:1b.0 Audio device: Intel Corporation N10/ICH 7 Family High Definition Audio Controller (rev 01)
        01:02.0 Multimedia audio controller: Creative Labs SB Live! EMU10k1 (rev 07)

        which would translate to ether:

        alsa_input.pci-0000_01_02.0.analog-stereo

        in the case of the SB Live! card, or
        alsa_input.pci-0000_01_0b.0.analog-stereo

        for the Intel sound card.
        3. restart pulse (well, if it's not configured to run as a system-wide sound server, just kill the current instance):

        $ puslseaudio -k


        Try now www.youtube.com with Firefox or GoogleChrome! (maybe restart you browser, of course)

        Thursday, December 23, 2010

        Tarfile with exclude: Linux vs. Solaris

        There are differences, yes, and not only in tar syntax. But I'm going to stick to tar (for the moment).
        I sometimes have to make a tar file excluding certain directories or files, and it's subtly different in linux from solaris.
        For example, we want to create a tar file from a directory called ./mydir, name the output file mydir.tar(.gz) and we put into ./mydir/Exclude a list of files to be excluded from the resulting tar file.
        Solaris version:

        tar cvfX mydir.tar ./mydir/Exclude ./mydir

        will create a tar file called mydir.tar containing all subdirectories of ./mydir and at extraction time it will create a directory ./mydir as well

        Linux version:

        tar -cvf  mydir.tar --exclude=Exclude ./mydir

        or, if you directly want a gzip file:

        tar -zcvf  mydir.tar --exclude=Exclude ./mydir


        where the contents of the exclude file ./mydir/Exclude are in both cases identical, and for example like (it's assumend to be the relative path to ./mydir):

        Exclude
        foo
        bar


        So that's pretty much it. It's very easy, but I always get confused about it, and the differences existing between linux and the Solaris only make all worse to remember.

        Wednesday, December 15, 2010

        Recursively rename files

        I know, I know that's a very easy one... but just when I should have known how to do that in a shot, I didn't. And of course there are a lot of references for such things out there, so I'm absolutely not claiming to be original, but sorry if I don't post any references.
        So, here is the thing: imaging you want to rename all files in a certain directory that are *.txt with *.back for example, including any subdirectories:

        find . -type f -name "*.txt" -print| rename -n 's/(.*)txt$/$1back/g'

        Remarks:
        - this works only on linux (or where you have the command 'rename')
        - rename accepts the perl regexp syntax, hence all this $1 for the substitution of the previous match (.*)
        - tip: it's good to try using rename with -n option, which actually does nothing but showing what it would do without the -n option
        - careful: if you fear that something nasty could happen with very depth directories and the find consuming lots of CPU or for whatever reason, you can include a -maxdepth 10 (or any other reasonable number), like this:

        find . -maxdepth 10  -type f -name "*.txt" -print| rename -n 's/(.*)txt$/$1back/g'


        - another thing that confused me as I tried it out: don't include the 'usual' *.[file_type] (in my case *.txt) at the end, after the rename 's/.../.../' part; it will prevent find from really diving into the subdirectories

        Wednesday, November 10, 2010

        Real time audio and Ubuntu Studio

        I've have been trying to work with the Linux audio recording and editing software for some time now, but it's not been quite a very satisfying experience. One of the problems I had and wasn't able to solve was the real time kernel issue: the soundserver jackd needs for some things to work properly, to run on a real time enabled kernel. And the thing is I couldn't find the right information in the internet to help me out with this (in fact it's surprisingly easy).

        So recently I read about Ubuntu Studio, a Linux distro based obviously on Ubuntu which focuses mostly on audio and video recording and editing stuff. And I read about the kind of tuned kernel the distro was shipped with... and I decided to give a try.
        I download the latest one (10.10) and, though it's not a live distro, which I didn't expect and I thing it's a pity that you can't try it without installing it, I nevertheless had a spendable partition on which I could install it. It's a pretty normal Debian installer, quite straight forward, and … congratulations to the Ubuntu Studio guys, they've done a great job! There's lots of interesting software, some of which I never heard (or read) of before.
        The point of all this is actually, you don't have to really install a Ubuntu Studio if you already have an Ubuntu (or any other Linux installed) and you don't want to have more than one distro. Just install it to a virtual image (maybe there are already some virtual images available out there), try the software they ship with it, and copy their configuration settings.
        And, as for what was my original problem, the real time linux kernel, just put the appended file (the Ubuntu Studio guys named it audio.conf) into /etc/security/limits.d (if the directory limits.d doesn't already exist, just create it), and put this lines into it:
        # Provided by the jackd package.
        #
        # Changes to this file will be preserved.
        #
        # If you want to enable/disable realtime permissions, run
        #
        # dpkg-reconfigure -p high jackd
        @audio - rtprio 95
        @audio - memlock unlimited
        #@audio - nice -19

        and, of course, check that your user (the user you start the audio programs with) belongs to the 'audo' group.

        Actually, it' easier than that: on a debian-like system, if jackd is already installed, and as the previous audio.conf file suggests, just execute:
        dpkg-reconfigure -p high jackd
        Or choose 'yes' in a fresh jackd install.

        Another possibility is, if you already have a Ubuntu up and running, install the Ubuntu Studio related packages and you don't need the whole new Ubuntu Studio distro.

        Thursday, April 1, 2010

        Hardware acceleration again lost... and found!

        On my laptop I use mainly Kubuntu (love kde4) and I've a radeon r600 chipset, which by the moment of writing this is not completely supported: (K)Ubuntu 9.10 uses kernel 2.6.31 and the experimental dri drivers for hardware acceleration to run need kernel >= 2.6.32.
        So to get hardware acceleration with r600 chipset I:
        1. installed a 2.6.32 ubuntu kernel
        2. added the proper apt repositories (for ubuntu karmic):

        ### ati open source bleeding edge drivers
        deb http://ppa.launchpad.net/xorg-edgers/drivers-only/ubuntu karmic main
        deb-src http://ppa.launchpad.net/xorg-edgers/drivers-only/ubuntu karmic main
        deb http://ppa.launchpad.net/xorg-edgers/ppa/ubuntu karmic main
        deb-src http://ppa.launchpad.net/xorg-edgers/ppa/ubuntu karmic main

        3. aptitude update && aptitude safe-upgrade (visual via kpackagetkit is also possible)
        4. reboot with the new kernel
        ... everything is fine now, I can now enable composite for kdm, the visual effects are running, great, I love it!

        But somehow recently it got all messed up with an innocent update: After a reboot the gray kde bar told me composite was disabled. Xorg.0.log reported some ugly stuff about disabling dri:

        (EE)RADEON(0):
        [dri] RADEONDRIGetVersion failed because of a version mismatch.

        [dri] This chipset requires a kernel module version of 1.17.0,
        [dri] but the kernel reports a version of 2.0.0.
        [dri] If using legacy modesetting, upgrade your kernel.
        [dri] If using kernel modesetting, make sure your module is
        [dri] loaded prior to starting X, and that this driver was built
        [dri] with support for KMS.
        [dri] Disabling DRI


        After some googling around I found this where the exact same message is reported, together with some possible solutions, with didn't work for me. But what became clear is that maybe the problem was, as suggested by the message log message itself, that the kernel module was being loaded to late (somehow, after X was started).

        The solution was quite simple. Just add to the /etc/modules file the lines:
        drm
        radeon modset=1

        Why? Kubuntu 9.10 uses upstart as init daemon, and I found that /etc/init/module-init-tools.conf is the file responsible for loading all kernel modules listed in /etc/modules. So if this is executed (or read) before running the /etc/init/kdm.conf file than it should work, which it actually does.
        But, to be honest, I'm not quite sure if it always would (run module-init-tools.conf before kdm.conf). So to really be sure, I modified my kdm.conf file, adding a line:
        start on (filesystem
        and started hal
        and tty-device-added KERNEL=tty7
        and started module-init-tools
        and (graphics-device-added or stopped udevtrigger))

        which now guarantees that kdm is not started until module-init-tools file has been run.

        Monday, March 8, 2010

        Virtualbox: network confguration

        Virtualbox comes out of the box with Ubuntu and it's really very easy to use. Why not use VMware Server (free edition)? I used to use it, but it has some disadvantages compared to Virtualbox:
        - if you have a slightly non-standard linux distro, it doesn't install unless you compile the kernel modules, and this is very likely to fail (at least in my experience)
        - they moved to an application server based implementation (tomcat) in version 2, which is much more resource consuming (in a computer with only 512MB RAM it is really not usable)

        So, I finally switched over to VirtualBox, but one of the disadvantages of it is the networking: it's much more transparent to me in VMware than in VirtualBox. I want to be able to connect:
        host (Kubuntu) <---- > guest (Solaris 10)
        guest -----> outside world
        world -----> guest (this is optional to me, and still not implemented, but shouldn't be too difficult)

        So, based o this wiki and a few other sites/tutorials, I sum the steps I had to do to make it work:
        Host:
        1. enter this lines in my /etc/network/interfaces file (change user_name with the user name you're going to run VirtualBox):
        iface br0 inet static
        address 10.1.1.1
        netmask 255.255.0.0
        pre-up /usr/sbin/tunctl -t tap0 -u user_name
        pre-up ip link set up dev tap0
        pre-up brctl addbr br0
        pre-up brctl addif br0 tap0
        post-up ip route add 10.1.1.0/24 dev br0
        post-up iptables -A FORWARD -i wlan0 -o br0 -j ACCEPT
        post-up iptables -A FORWARD -i br0 -o wlan0 -j ACCEPT
        post-up iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
        post-up /bin/echo "1" > /proc/sys/net/ipv4/ip_forward
        pre-down ip link set down tap0
        pre-down /usr/sbin/tunctl -d tap0
        post-down brctl delbr br0

        2. run the command (as root) ifup br0
        3. start virtualbox guest choosing from the configuration gui net -> adapter1 -> attached to: select 'bridge' -> select 'tap0', which should appear if you have configured it before

        Guest:
        Yeah, that really depends on which guest you're planning to run. In my case, a Solaris 10, the thing is basically (for static ip configuration):
        1. enter static ip address in /etc/hosts together with a loghost entry
        2. create the /etc/hostname.
        3. add default router in /etc/defaultrouter
        4. add the routing table (in my case, 10.1.1.0 is the bridge-tap net on the host, and 192.168.0.0 is the my real host OS subnet):
        route -p add 10.1.1.0 -netmask 255.255.0.0 10.1.1.1
        route add -net 192.168.0.1/16 10.1.1.1
        (and maybe I'm leaving something out here....)

        Enjoy

        Tuesday, February 9, 2010

        Remote Desktop: nomachine

        I know, nomachine is not exactly open source (they release lots of their stuff as open source and there are open source packages based on their open source released libraries), but hey, it's a great tool that works great out of the box. I use it to connect to my home box from work and it runs just great, this despite my home internet connection sends data only at about 30KB. I love it.
        So recently I needed to connect to a second home machine, through the same router, and since ssh server port 22 was already in use, I had to configure the whole thing to work on a different port. It's really very easy, but you do it once and a few months later you can't remember how. So, here it is for a Debian(like) system:
        A) client side:
        1. download from the nxclient package
        2. install it simply (as root or sudo su) with dpkg -i *.deb
        that's it (it doesn't get installed in your default path, so you have to run /usr/NX/bin/nxclient from a terminal or, more easy, from the application launcher menu, usually in the 'internet' section)

        B) server side:
        1. download de nxnode, nxserver and the nxclient from the link above (according to the nomachine site install instructions, they're all needed)
        2. install them in the this order: nxclient, nxnode, nxserver
        3. now check that the nxserver is running with the command:
        /usr/NX/bin/nxserver --status
        and you should see something like:
        NX> 900 Connecting to server ...
        NX> 110 NX Server is running.
        NX> 999 Bye.
        (Hint: it should be running, but if it's not, try /usr/NX/bin/nxserver --start or --restart and check again)

        Note: don't forget to configure your router, if you have one, to forward the ssh port to this new machine so you can connect at all!

        The server is up and running and it should just work now, but here are some extra tweaks that could be useful:
        1. Run the server on a different port:
        - edit (on the server) the file /usr/NX/etc/server.cfg and search for SSHDPort; uncomment it with the port you want (careful: there're 2 entries with SSHDPort: one for the server daemon and another one for authentication)
        - and, I don't know if this is necessary, but I also edited the /usr/NX/etc/node.cfg file, and uncommented the SSHDPort port entry to change it with my non-standard ssh server port

        2. Run your nxclient with a different desktop environment: let's say lxde (which is really very light)
        - in the session configuration window, go to 'General' tab -> section 'Desktop' and choose 'Unix' and 'Custom'; now click on 'Settings', mark 'Run the follwing command' and write 'startlxde' (of course you must have all necessary lxde packages installed).

        Enjoy nomachine, it's really fast.

        Tuesday, April 7, 2009

        Cheet Sheet on lvm

        Before adding some new space to a volume group, first check out if we already have some space left: command vgdisplay will do the job:
        testos:/root # vgdisplay
        --- Volume group ---
        VG Name lvm_data
        System ID
        Format lvm2
        Metadata Areas 4
        Metadata Sequence No 10
        VG Access read/write
        VG Status resizable
        MAX LV 0
        Cur LV 3
        Open LV 3
        Max PV 0
        Cur PV 4
        Act PV 4
        VG Size 312,53 GB
        PE Size 4,00 MB
        Total PE 80008
        Alloc PE / Size 69632 / 272,00 GB
        Free PE / Size 10376 / 40,53 GB
        VG UUID auAv2K-gvrp-tB2v-6ncv-ij6w-oMjd-tFGH3U



        Also, if we want to know which physical partitions we've associated to volume groups so far, pvdisplay is the command:
        testos:~# pvdisplay
        --- Physical volume ---
        PV Name /dev/sda8
        VG Name lvm_data
        PV Size 21,82 GB / not usable 0
        Allocatable yes (but full)
        PE Size (KByte) 4096
        Total PE 5586
        Free PE 0
        Allocated PE 5586
        PV UUID uPWS3f-NqcU-JZnm-iH6Y-skTC-95Qv-2W7EQB

        --- Physical volume ---
        PV Name /dev/sdb6
        VG Name lvm_data
        PV Size 191,89 GB / not usable 0
        Allocatable yes (but full)
        PE Size (KByte) 4096
        Total PE 49124
        Free PE 0
        Allocated PE 49124
        PV UUID d987rD-cSes-0erP-k6wg-gS9D-8bIC-hzJGl0

        --- Physical volume ---
        PV Name /dev/sda1
        VG Name lvm_data
        PV Size 18,54 GB / not usable 0
        Allocatable yes
        PE Size (KByte) 4096
        Total PE 4745
        Free PE 63
        Allocated PE 4682
        PV UUID M2Cz9P-eunF-1Rgv-A00j-sNtg-On8c-y3RqdQ

        --- Physical volume ---
        PV Name /dev/sda6
        VG Name lvm_data
        PV Size 80,29 GB / not usable 0
        Allocatable yes
        PE Size (KByte) 4096
        Total PE 20553
        Free PE 10313
        Allocated PE 10240
        PV UUID 6n3h9p-z0N0-xyb1-C8mF-f0Xn-XlEr-bfhYtA

        Steps to increase the size of a logical volume:
        1. I create a new partition (or reuse an old one), for example /dev/sda6 and mark it as lvm (label 8e, with fdisk)
        2. Before adding it to an existing volume group, 'format' it with lvm stuff:
        pvcreate /dev/sda6
        3. Add the partition to an existing volume group (VG): it must obviously be the VG where the logical volume (LV) that we want to expand resides, and we can check this with lvdisplay. This is done with the command:
        vgextend , in my case:
        vgextend lvm_data /dev/sdb6 (where lvm_data is the volume group name, not the logical volume name: don't be confused, I did't choose the name properly...)
        4. Now that we have some spare space left in our VG, expand the logical volume to the required size:
        lvextend -L20G /dev/lvm_data/data
        (-L20G indicates that the total new size shold be 20G; we can put -L+20G and that would increase the existing physical LV size in 20G; /dev/lvm_data/data is the path to the LV)
        5. And finally, we still have to increase the file system size. The safe way always includes a previous backup, but after that:
        a) unmount the file system
        b) run a fsch (resizing the file system will ask for it anyway):
        e2fsck -f /dev/lvm_data/data
        c) resize the file system with e2resize command (this is only for extX file systems):
        resize2fs /dev/lvm_data/data 20G
        d) mount the file system again and check with df -h that it has the new size

        Friday, January 16, 2009

        usb drives with udev

        Hey, finally I managed to actually get something out of udev, which I think is great but I don't really understand yet.
        I have an 2.5'' hard drive in an external usb box. I carry this from my workplace home and back again and some stuff in it needs to point to the same absolute path, so it has to be mounted on the same mountpoint. Well, the disk has 2 partitions and I'm actually interested only in the partition /dev/sdc5. It has to be mounted in /media/usb-5, but kde's automounter mounts it in /media/disk. Of course I can ignore kde's automount feature and mount it manually after putting the corresponding entry in /etc/fstab, but I like this kde pop-up, click accept and it's done.
        After reading this one, I figure out how to do it:
        1. I get the relevant information with udevinfo:
        fixdebian:/etc/udev/rules.d# udevinfo -a -p /sys/block/sdc/sdc5

        Udevinfo starts with the device specified by the devpath and then
        walks up the chain of parent devices. It prints for every device
        found, all possible attributes in the udev rules key format.
        A rule to match, can be composed by the attributes of the device
        and the attributes from one single parent device.

        looking at device '/block/sdc/sdc5':
        KERNEL=="sdc5"
        SUBSYSTEM=="block"
        DRIVER==""
        ATTR{dev}=="8:37"
        ATTR{start}=="26619831"
        ATTR{size}=="51520329"
        ATTR{stat}==" 537 537 0 0"

        looking at parent device '/block/sdc':
        KERNELS=="sdc"
        SUBSYSTEMS=="block"
        DRIVERS==""
        ATTRS{dev}=="8:32"
        ATTRS{range}=="16"
        ATTRS{removable}=="0"
        ATTRS{size}=="78140160"
        ATTRS{stat}==" 59 776 1263 747 0 0 0 0 0 636 747"
        ATTRS{capability}=="12"

        looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0/host4/target4:0:0/4:0:0:0':
        KERNELS=="4:0:0:0"
        SUBSYSTEMS=="scsi"
        DRIVERS=="sd"
        ATTRS{device_blocked}=="0"
        ATTRS{type}=="0"
        ATTRS{scsi_level}=="0"
        ATTRS{vendor}=="TOSHIBA "
        ATTRS{model}=="MK4025GAS "
        ATTRS{rev}=="0000"
        ATTRS{state}=="running"
        ATTRS{timeout}=="30"
        ATTRS{iocounterbits}=="32"
        ATTRS{iorequest_cnt}=="0x42"
        ATTRS{iodone_cnt}=="0x42"
        ATTRS{ioerr_cnt}=="0x0"
        ATTRS{modalias}=="scsi:t-0x00"
        ATTRS{evt_media_change}=="0"
        ATTRS{queue_depth}=="1"
        ATTRS{queue_type}=="none"
        ATTRS{max_sectors}=="240"


        (.... and a lot more output that I don't need now).
        I pick both lines: ATTRS{vendor}=="TOSHIBA " and
        ATTRS{model}=="MK4025GAS "

        2. I create a file in /etc/udev/rules.d/ called 010_myusb-disk-part5.rules. The man page says that files included there are read in lexical order, so I guess it could be useful to be one of the first rules that apply to avoid any 'interference' from another file already included by the default udev installation. (Actually, at least in debian, the files included in /etc/udev/rules.d are soft links to files in the parent directory: you don't need to remove the whole file if you don't like it, just remove the link). The file is:
        fixdebian:/etc/udev/rules.d# cat 010_myusb-disk-part5.rules
        ACTION=="add", KERNEL=="sdc5", ENV{vendor}=="TOSHIBA ", ENV{model}=="MK4025GAS ", RUN+="/home/user/bin/mount-usb-5.sh"


        3. The file /home/user/bin/mount-usb-5.sh is a simple executable script with the single line: mount /media/usb-5.
        4. Run the udev test:

        fixdebian:/etc/udev/rules.d# udevtest /sys/block/sdc/sdc5
        parse_file: reading '/etc/udev/rules.d/010_myusb-disk-part5.rules' as rules file
        parse_file: reading '/etc/udev/rules.d/020_permissions.rules' as rules file
        parse_file: reading '/etc/udev/rules.d/025_libgphoto2.rules' as rules file

        (...)
        This program is for debugging only, it does not create any node,
        or run any program specified by a RUN key. It may show incorrect results,
        if rules match against subsystem specfic kernel event variables.

        main: looking at device '/block/sdc/sdc5' from subsystem 'block'
        udev_rules_get_name: no node name set, will use kernel name 'sdc5'
        udev_db_get_device: found a symlink as db file
        udev_device_event: device '/block/sdc/sdc5' already in database, validate currently present symlinks
        udev_node_add: creating device node '/dev/sdc5', major = '8', minor = '37', mode = '0660', uid = '0', gid = '25'
        main: run: 'socket:/org/freedesktop/hal/udev_event'

        So this seems to work: at least the syntax of my new file isn't wrong. 5. Restart udev: in my debian box, I run /etc/init.d/udev restart 6. Now I plug in the usb drive, kde's mount pop-up window appears, I accect and, the drive is mounted in /media/usb-5, just as I wanted.

        Friday, January 2, 2009

        Environment variables a process runs with

        Recently I had a problem with a certain process that could be launched it two different ways: from command line and from a graphical client application. When launched from the command line it didn't quite work, but it did when started via the graphical client application. I knew that it was an environment variable issue, but I didn't know how I could find out with which environment variables a certain process was running.
        I guessed that I could find something it the /proc directory, so I got into and found that for each running process there's a file called 'environ', which can be found in /proc/PID/environ. Ok, so that's fine and it would be a great solution, provided my problem was in a linux box, but it was Solaris 5.9 machine, and in solaris the /proc file, though it exists, it's not so full of information as in linux. And there was no environ file.
        Fortunately I found on the web (I don't have the url anymore, sorry) that you can do something like:

        ps wwe
        to get the what I was looking for, and this works on linux and solaris. For example, to get the environment of my running bash session:

        testing@fixdebian:~$ ps wwe $$

        PID TTY STAT TIME COMMAND
        27857 pts/6 S 0:00 -su TERM=xterm PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games LANG=es_ES@euro MAIL=/var/mail/testing HOME=/home/testing SHELL=/bin/bash USER=testing LOGNAME=testing


        which is far more elegant. And this solved my problem in a more general way.

        Tuesday, December 30, 2008

        Reinstall grub from live CD

        Yesterday I had to install win$ XP again on my machine (until now I was using wine, which is great, or win$ as a virtual machine inside vmware server, but sometimes that's not enough...). As usual, the installation breaks the boot loader, whether you have lilo or grub, and you have to reinstall it again in order to be able to boot your linux (or whatever OS that's not a windows).
        This is not always as easy as it should be, or at least I always forget how to do it. So yesterday, what worked for me (twice) was:
        1. boot from a Knoppix Live-CD (it's just the one I have always lying around, and Knoppix is great)
        2. Don't need a graphical session, just type Ctrl+Alt+F1 and go to the terminal: you're already as root
        3. mount the file system you have your /boot files in, my case:
        mount /media/hda2 (once mounted, check that this really contains or is your boot directory)
        4. install grub:
        grub-install --no-floppy --root-directory=/media/hda2 /dev/hda
        where:
        - I have to put --no-floppy, otherwise it fails (guess my floppy drive is not working, I never use it ;) )
        - I always install grub on the Master Boot Record of the first drive: /dev/hda (if you have sata drives, then it should be /dev/sda
        - root-directory has to point to the mount point from where you can see your boot directory, not to the boot directory itself (e.g. if you put /media/hda2/boot the grub install works, but you will have a new boot dir inside the previous one, and you won't find your usual menu.lst file).

        Tuesday, December 9, 2008

        Which application is using port xxx?

        It can happen that you're playing with iptables, trying to configure your firewall, or for whatever reason you run nmap to see what ports are open like:

        ~$ nmap -sT localhost
        Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2008-12-09 11:25 CET
        Interesting ports on localhost (127.0.0.1):
        Not shown: 1671 closed ports
        PORT STATE SERVICE
        22/tcp open ssh
        111/tcp open rpcbind
        113/tcp open auth
        538/tcp open gdomap
        631/tcp open ipp
        902/tcp open iss-realsecure-sensor
        2628/tcp open dict
        3306/tcp open mysql
        18000/tcp open biimenu


        And you wonder, 'hey, whats going on on port 18000? No idea what this biimenu is'. To find out the PID of the process using this port, you can run fuser like this:

        fuser -n tcp 18000
        18000/tcp: 4628

        (Obviously, if it's an udp port, you have to put fuser -n udp port_number).
        There you have it. Now just run ps aux (or whatever options you want) and you'll get the process:

        ~$ ps aux | grep 4628
        user 4628 1.7 1.5 135956 56588 ? Dl 07:08 4:39 /usr/bin/amule
        user 13899 0.0 0.0 3976 776 pts/7 S+ 11:38 0:00 grep 4628


        Gotcha! It is my amule (changed the port configuration yesterday because of some problems with the kad protocol: It appeared as firewalled and I didn't really understand why, but that's a different point).

        To put it in a script this could be helpful:
        ps aux | grep `fuser -n tcp 18000 2>/dev/null`
        user 4628 1.9 1.6 138864 58568 ? Rl 07:08 6:35 /usr/bin/amule
        or:
        ps aux | grep $(fuser -n tcp 18000 2>/dev/null)
        user 4628 1.9 1.6 138864 58588 ? Sl 07:08 6:38 /usr/bin/amule