Posts tagged ‘linux’

Dealing with life in Haskell

I bet you have at least one silly little thing at work that, whenever it happens, you let out a sigh, maybe roll your eyes and whish that everyone would use a proper operating system. A few days I finally decided to do something about one of my things like that. At work, Windows users will at times for some strange reason, manually create directories inside their work area, even though the directories actually are under version control. Invariably they get the case wrong and due to an onfortunate combination of case insensitive filesystem on the client (Windows) and a version control system the cares about case (Perforce). This results in files ending up all over the place even though they belong in the same directory. The Windows users are none the wiser, they simply don’t see the problem. Since I use a sane system (Linux) I do notice, and when I see it I sigh and roll my eyes.

Here’s my take on solving the problem:

module Main where

import Control.Monad
import Data.Char
import System.Directory
import System.Environment
import System.FilePath
import System.IO.HVFS.Utils
import System.Posix.Files

data IFile = IFile
    { iFileIPath :: FilePath
    , iFileIName :: FilePath
    , iFileFull :: FilePath
    } deriving (Show)

toIFile :: FilePath -> IFile
toIFile fp =  IFile path file fp
    where
        path = map toLower $ takeDirectory fp
        file = map toLower $ takeFileName fp

listFilesR :: FilePath -> IO [IFile]
listFilesR path =
    recurseDir SystemFS path >>=
    filterM doesFileExist >>=
    mapM (return . toIFile)

linkFile :: FilePath -> IFile -> IO ()
linkFile dest ifile = do
        createDirectoryIfMissing True newDir
        createLink (iFileFull ifile) newFile
    where
        newDir = normalise $ dest </> (iFileIPath ifile)
        newFile = newDir </> (iFileIName ifile)

main :: IO ()
main = do
    args <- getArgs
    listFilesR (args !! 0) >>= mapM_ (linkFile $ args !! 1)

Yes, this is the code I wrote in Literate Haskell, but I think I’d better not disclose my rant against clueless Windows users publically ;-)

LVM rocks!

I knew my persistence with using LVM would pay off one day. Despite the little mishap I had last year :-)

For a shiny new install of 64-bit Debian I chose to let the installer partition up my entire harddisk and instructed it to use LVM. This morning I noticed that the root partition was down to only 25% free space and during an upgrade it ran out of space. Not really a good thing. So, shut down the machine and out with the extra harddisk I’ve been putting off sticking in the machine. Here’s what I did after booting:

  1. Create a single large partition and make it of type Linux LVM (8e) using cfdisk.

  2. Prepare the new partition for use with LVM:

    # pvcreate /dev/sdb1
    
  3. Add the new ‘physical volume’ into the ‘virtual group’:

    # vgextend mainvg /dev/sdb1
    
  4. Extend the ‘logical volume’ where root lives:

    # lvextend --extents +50%LV /dev/mainvg/root
    
  5. Then it turns out that Ext3 has no problem with extending a mounted filesystem so the last step was easy, but a little nerve wrecking since it was my root partition:

    # resize2fs /dev/mainvg/root
    

All done!

One-track thinking (unlocking root)

It seems I never quite learn. I like a clean system, so when I get a chance I remove unused packages. This practice has gotten me in trouble before. It got me in trouble again just the other day.

My now second machine used to be my primary. When it was demoted I left GNOME installed on it since you never know when it might come in handy. Over the last few months I’ve had no use for a GUI on it at all so last Friday I decided to remove GNOME. That got me in trouble because sudo is installed as a dependency of GNOME’s, and it’s marked “automatic” in aptitude. Couple that with my habit of locking the root account and I ended up with a system that I don’t have full access to anymore. Not good!

My immediate thought was to boot a live CD, chroot to the root filesystem of the installed system and unlock the root password. Except the damn box refused to boot from CD. I tried all my live and install CDs, Ubuntu (Breezy and Dapper), Knoppix, STD, Debian install (Woody and Sarge). Nothing worked.

A short search later and I found muLinux. A one-floppy live system that on paper seemed capable of doing what I needed. Now I had another problem, where do I get a floppy nowadays? The system admins downstairs had one they could spare. Good! Next problem—where to find a machine with a floppy drive that I can use to create the floppy?

That’s when it hit me. This plan wasn’t the best one, it just happened to be the first one that popped into my mind. I had been too focused on my first idea to take the time to stop and think of other ways of getting my root account back.

In the end I didn’t need to use a live CD/floppy, I could just use the system already present on the box:

  1. Boot straight into bash by sticking init=/bin/bash on the boot line in GRUB
  2. Remount the root filesystem, mount -o remount,rw /
  3. Unlock the password, passwd -u root

Banging my head against sparse files

Lately I’ve had use for sparse files to solve a problem. Simple, yes. Use lseek and write (or read). Even better, at least in this case, use pwrite and pread. Should be simple, right? Yes, should be.

I was banging my head against code equivalent to the following for quite a few hours:

#define _XOPEN_SOURCE 500
#define _FILE_OFFSET_BITS 64
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

char buf[1024];

int
main(int argc, char **argv)
{
    int n;
    off_t off;

    for(n = 0; n < 1024; n++) {
        buf[n] = ‘a’;
    }

    int fd1;

    if(-1 == (fd1 = open(”file1″, O_WRONLY | O_CREAT, 0666)))
        error(EXIT_FAILURE, errno, “open (test1)”);
    off = 0×80000000;
    printf(”size: %d\n”, sizeof(off));
    if(pwrite(fd1, buf, 1024, off) != 1024)
        error(EXIT_FAILURE, errno, “pwrite (%lli)”, off);

    return(0);
}

Can you spot the problem?

Well, I can tell you it doesn’t work. off_t becomes 64-bit due to the #define _FILE_OFFSET_BITS 64 so the off variable isn’t negative in my code. Somehow however it becomes negative on the way into pwrite, and without a single compiler or linker error at that!

Have you spot the problem yet?

Here’s the correctly working code:

#define _XOPEN_SOURCE 500
#define _FILE_OFFSET_BITS 64
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

char buf[1024];

int
main(int argc, char **argv)
{
    int n;
    off_t off;

    for(n = 0; n < 1024; n++) {
        buf[n] = ‘a’;
    }

    int fd1;

    if(-1 == (fd1 = open(”file1″, O_WRONLY | O_CREAT, 0666)))
        error(EXIT_FAILURE, errno, “open (test1)”);
    off = 0×80000000;
    printf(”size: %d\n”, sizeof(off));
    if(pwrite(fd1, buf, 1024, off) != 1024)
        error(EXIT_FAILURE, errno, “pwrite (%lli)”, off);

    return(0);
}

Don’t see any difference? Look at the list of included files!

Let me just say the C and I aren’t on speaking terms at the moment!

Being st00pid with LVM

Well, this is the first entry in my st00pid category. I make quite a few mistakes when using Linux, mistakes that take time and effort to fix. From now on I’ll swallow my pride and write down, in a public place like this, about what st00pid things I do. Most importantly I’ll also write down what I did to get my Linux system back to the state it was before my brain temporarily popped out for a coffee.

I like keeping my Debian systems minimal. When running Sid there ends up being quite a few upgrades to download and when dependencies change your system ends up having “orphaned” packages, i.e. packages that were pulled in to satisfy a dependency but by now the dependent package has since moved on (or been deleted). This is of course a worrying thought, packages are installed that don’t actually need to be present on the system. My stomach’s turning. Some people use aptitude to handle this, personally I’ve never really like aptitude and I use debfoster.

Yesterday, after a sizable upgrade of my system at work I ran debfoster to prune orphaned packages. This was in fact the first time I ran it after setting up the system which resulted in quite a few questions regarding whether to keep specific packages or not. This is about the time when my brain popped out for a coffee. When asked if I wanted to keep lvm2 or not there was no brain to consult and I answered no. Had the brain been around it would have reminded me that I opted to use logical volumes on that particular machine (for two reasons, 1) I had never done it before and it’s damn cool, and 2) it makes the system a bit more flexible and future proof).

After a reboot I was sitting there with a system that was fairly useless since the root partition couldn’t be mounted. A quick calculation of just how long it’d take me to re-install later, I popped in the only live CD I had available at work–Ubuntu 5.04, Hoary Hedgehog. Using that I found a page on the Knoppix Wiki on how to use LVM2 with Knoppix. Now armed with something that looked like a workable plan I did the following in a terminal

$ sudo su -
# modprobe dm-mod
# vgscan
# vgchange -a y
# cd /mnt; mkdir debsys

Then I went ahead and mounted the root partition on /mnt/debsys. I also mounted the other partitions I have on my installed system, /var, etc. Then to get into it and fix it I

# chroot /debsys
# apt-get install lvm2 lvm-common

Except lvm2 refused to install, it complained about the kernel being too old. Not surprising since Hoary is getting really old by now. to get around that I downloaded the source for lvm2, modified debian/preinst so that the kernel version check was skipped, re-built the package, and installed it. Worked like a charm.

VMWare server to the rescue

I complained earlier about VMWare Player not allowing me to replace my Windows boxen at work. It seems VMWare took my complaints seriously (yeah right), by releasing VMWare Server they’ve made my dream possible.

I have to admit that their releasing VMWare Server is surprising, and since it allows creation of new images it makes the limitation of VMWare Player rather strange. Well, I’m not complaining. I’ve finally been able to purge Windows from my desk. From today on all my Windows boxen will be VMWare images accessed from my Linux machine. Brilliant. Thank you VMWare!

Linux and explaining the Swedish(-speaking) people

I found the following in Linux Journal. It can be found on this page, I’ve copied it verbatim since I couldn’t find a link that leads straight to the interesting part!

Might Be Just Right

by Doc Searls

At LinuxWorld in Boston earlier this year, I got together with an old Swedish friend. She’s a nurse, not a technologist, but she was curious about my work and the conference that brought me to town. Somewhere in the midst of my explanation of Linux and its virtues, she said, “Ah, Linux is lagom”. She explained that lagom is a Swedish term that conveys a sense of balance, proportion and appropriateness. “Not too much, not too little…just right.”

When I told her that Linus Torvalds’ first language and surname were both Swedish, she said, “Well of course. There you go.” (I’m half-Swedish myself, though I’m not sure that matters.)

So I put the question “Is Linux logom?” to The Man Himself in an e-mail. He debugged my spelling and declined to commit:

Lagom, with an “a”.

And yes, it means “just right”, in the sense of “not too much, not too little”. See en.wikipedia.org/wiki/Lagom

Then he added, in a following e-mail:

They still end up confusing “lagom” with finding the “optimal” amount. That’s pretty much missing the point. It’s not that something is “lagom” because it’s the best possible or “optimal”. Quite the reverse. Something being “lagom” very much involves not caring too much about what the optimal amount even is. Or possibly questions where “optimal” simply doesn’t make sense.

So I began checking other sources. The best I found was from “In Other Words”, published in AskOxford, published by the Oxford English Dictionary (www.askoxford.com/worldofwords/wordfrom/otherwords). It lists lagom among a handful of “the most insightful, intriguing, and satisfying expressions on the planet-for which there are no English equivalents”. It says:

Swedish commentator Dr Bengt Gustavsson argued that the lagom mentality can be seen as the trait that gives Swedish society its characteristic stability and yet an openness to external influences. The word alludes subconsciously to the avoidance of both conspicuous success and humiliating failure, which is deeply ingrained in the Swedish psyche. It is the inclination among Swedes to shun ostentation, accept modest rewards, be good team players-to fly beneath the radar.

Beneath the Radar was also the title of Bob Young’s book about starting and guiding Red Hat to success. Coincidence?

Perhaps characteristically, Linus adds these final words to the matter: “but whether that applies to Linux I have no idea.”

Thank DOYC for Knoppix

All I needed to do was fill the hard disk with zeros. Simple right? It turned out not to be. My Ubuntu liveCD is broken, it hangs when I get to the keyboard chooser. My Phlak live CD didn’t cut it either. My minimal System Rescue CD couldn’t handle the SATA disk… Off to the Knoppix home page and one burnt and booted CD later I have a funtioning Linux system. Thank DYOC (Deity of Your Own Choice) for Knoppix!

Experiencing Arch Linux

I’ve just “finished” installing Arch Linux. (I don’t think I’ll ever be finished for real since Linux is such a dynamic place. However, I’ve installed enough of Arch Linux to be writing this using Epiphany, running on a slick GNOME desktop.)

So far Arch has proven to be a nice, well designed system with a lean feel to it. Being used to Debian (lately Ubuntu) and its extreme attention to detail, Arch does give a more rough impression. It’s a price I think is worth paying, and believe me, it’s not as bad as it sounds. I’ve spent about 2 hours getting the base system, postfix, X.org, and GNOME installed and configured properly. A few small details remain, but I would have to attend to those no matter what distro I choose (getting HAL to refrain from mounting one of the partitions on my USB stick isn’t something that comes pre-configured in any distro I know of :)).

The only thing left doing now is building proper Arch packages for muttng and keysafe.

Linux and vfat: mtools

When it’s not enough to just read and write files on a VFAT file system there’s a good change mtools can help. The only problem is that mtools work with drive letters. To assign a letter to your device you need to edit /etc/mtools.conf. This is the line I use to access the VFAT partition on my USB stick:

drive d: file="/dev/sdb1"

After this I can use mlabel to set the label of the partition:

mlabel d:MyLabel