Wednesday, May 28, 2008

automount with sshfs and sshpass

sshfs is great for mounting a remote filesystem and having it appear as a local drive. The major drawback is that it is difficult to automatically mount with sshfs at startup without using ssh keys. Using sshpass, I have worked out a solution.

For this example, let's assume the following parameters:
  • username on remote host: user
  • password on remote host: password
  • remote host: host.com
  • directory: /directory
  • mount point: /media/ssh
In order to accomplish the mount, you will need to create two files:
  1. A file containing just your password, followed by a newline. In the example, I will assume this file is located at /home/user/.pwd. If your password is "password", it should look like this:

    password

  2. A bash script that indicates how to use sshpass. In the example, I will assume that this file is located at /home/user/ssh.sh. Note that the location of your .pwd file must be included after the -f option. This file must be made executable.

    #!/bin/bash
    sshpass -f /home/user/.pwd ssh $*

To mount this from the command line, you could issue the following command:

sudo mount -t fuse sshfs#user@host.com:/directory /media/ssh -o ssh_command=/home/user/ssh.sh

However, the title of this post is automounting, so let's do that. To automount this ssh server, you can add a line like this to your /etc/fstab:

sshfs#user@host.com:/directory /media/ssh fuse ssh_command=/home/user/ssh.sh 0 0

That's it. Pretty cool. Hope you find it as useful as I do.

Create an alias for your mysql database

I administer a mysql database that is updated with new snapshots (i.e. completely overwritten) every week and have users that generally need to access the latest snapshot. However, occasionally they will need to access an older version of the database, so all versions must be kept online. In the past, when I received a snapshot, I would create a database with the date of the snapshot in the title and send out an email to the users with the new connection information. However, since mysql database names are simply the name of the directory that they are stored in, it is easy to create an alias for the latest snapshot of the database simply by creating a symbolic link. For example, on a linux system the databases are generally stored under /var/lib/mysql. So, if you have a database named db_20080304 you can create a symbolic link to this database like:

ln -s /var/lib/mysql/db_20080304 /var/lib/mysql/db_latest

Now you should have a database called db_latest. You can repeat this process each time you receive a new snapshot of the database. Note that mysql views this as another database so you will need to set the permissions on db_latest separately from db_20080304. The nice thing about this is that you will only need to set permission once. The users do not need to have permissions on the actual databases; only on db_latest. I'm not sure how this will work on other operating systems, but would be glad to hear comments.

Thursday, May 15, 2008

Locking root account in Ubuntu 8.04 (Hardy) has nasty side effects

When I installed Hardy on my machine, I enabled the root account and then decided to disable it again. I did this by following the instructions I found on the web:

# Enabling the account
$ sudo passwd -u root

# Disabling the account
$ sudo passwd -l root

However, I quickly ran into an issue where the root's cron jobs would not run because the account was locked. After some fiddling, I fixed the problem by first re-enabling the root account:

$ sudo passwd -u root

and then, disabling the account properly:

$ sudo passwd -d root

After this, all works as it should.

Sunday, May 11, 2008

VMware keyboard annoyances on Ubuntu 8.04 (Hardy)

Like several other people I've had issues with my keyboard modifiers (shift, alt, ctrl, etc.) when switching from the guest OS (Windows) back to the host (Linux). Apparently, the easiest fix for this is to use the gnome keyboard layout gui to briefly switch the keyboard map to something random, then back to the original layout. So, here is a short perl script that does the same using the setxkbmap utility. If you want to use it, make sure to change the $orig_kb_map variable to match your keyboard model. You can create an application launcher on your Desktop or panel and simply click it regain control of your keyboard modifiers. Until there is an official fix for this bug, this is at least bearable.

#!/usr/bin/perl

# vmkbfix: a script to reset the keyboard layout after switching out of
# a virtual machine.

use strict;
use warnings;

my $orig_kb_model = "pc101"; # The keyboard model
my $switch_kb_model = "pc104"; # The model to switch to briefly
my $SETXKBMAP = "/usr/bin/setxkbmap"; # location of setxkbmap

# briefly switching to a different keyboard model, then switching back
`$SETXKBMAP -model $switch_kb_model`;
`$SETXKBMAP -model $orig_kb_model`;

Saturday, May 10, 2008

sqlite rocks!

I feel like I'm late to the party on this one, but I just discovered sqlite and the associate Perl module, DBD::sqlite. For those that are later than me, it is an implementation of SQL that resides in a single file, with no setup of any kind involved. I'm using it to store data that I frequently need only portions of in my Perl program. This provides a powerful & elegant way of doing just that. I hope you find it as useful as I do!

Friday, May 9, 2008

Ubuntu 8.04 (Hardy) has a gdm ssh client!

Hardy apparently has an ssh client built into gdm, making it easy to login to a remote machine and see your desktop. Seems to work pretty well at the moment, but it would be nice if you could log in to your local machine simultaneously. Something ala NoMachine NX.

Hello World

This is simply a place to post my random thoughts for all the world's glory.