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