Sunday, May 31, 2009

Turning a dead laptop into a snappy workstation with sidux

I have a dell C610 laptop with a dead hard drive and 512MB memory. It had been collecting dust for a while and I felt that I could use it as a work station. On the other hand, I do not feel like investing in a new hard drive.

First, I tried live cd. The problem is that I could not persist my changes. Then I found ubuntu remastersys, which allows me to burn a cd that representing my running system. For storage, I had to use a usb hard disk, although with usb 1.1 it is a bit slow. Accessing files on the cd is alos a bit slow. Another thing that bothered me is that the dvd/cdrw drive was busy and could not be used for anything else, such as burning another live cd.

Then I tried to find a livecd with toram option. Most of them require more than 512MB. Some required less but did not work quite write or had peculiar formats. Finall, I found sidux, which needs about 450MB. Almost perfect, because programs run all from RAM with lightening speed, and the dvd/cdrw drive is now free. Being based on debian, I can install any debian packages to extend my system as I see fit.

One problem remains, however. As I added programs, eventually the memory ran out and kernel started killing them (OOM, or out of memory kills). This is not good. I then connected a usb drive and create a swap partition on it. The usb drive is also used for persistent storage. Perfect!

Finally I have a speedy workstation with a nice desktop that is practically free but works just as good as a system costs hundreds of dollors, thanks to linux, especially sidux. I expect to use it until something else fails.

Friday, May 15, 2009

how to mount a disk image and its partition in linux

The image was created by dd. if I had copied a partition, it would have been easy, but sometimes I want to preserve the state of the entire disk, and literally I have to jump through loops to get to my files.

$ dd if=/dev/sdb of=sdb.img

to mount the image, you need losetup

$ losetup /dev/loop1 sdb.img

remember /dev/loop1 is the entire disk, not a partition, so you cannot mount it directly to your file system. now find out where its partition is

$ parted /dev/loop1 unit B print

Model: Unknown (unknown)
Disk /dev/loop1: 4005527551B
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32256B 4004904959B 4004872704B primary ext3

Information: Don't forget to update /etc/fstab, if necessary.


This tells us that the first partition starts at offset 32256

$ losetup -o 32256 /dev/loop2 /dev/loop1

mount the partition that we just setup as /dev/loop2

$ mount /dev/looop2 /mnt/tmp

You should see your file system, and you can modify it. Please remember to umount and sync so that the image can be copied back to a hard disk, which in my case is really a usb stick. This technique is quite useful for data recovery, virtualization, and so on.

Thursday, May 14, 2009

Install linux to a usb stick from an existing working install

This is not a live usb install that converts from a live cd to a live usb. I prefer a complete install, with the usb stick used a hard drive. Nowadays bios has no problem booting with usb, and treat it just like a hard disk.

The main advantage is that the stick can be updated and new packages can be installed just like any other hard disk install, and you can carry it around easily. Wherever you can plug it into, you will have your personal workstation.

I have used centos5 and ubuntu 8 and 9, but other installations should be the same.

Please note that a fresh install from dvd, pxe boot, net install etc. to a usb stick is not covered as these methods are no different from standard installation. Rather, I am installing from a working installation that I have already customized.

Here are the steps:


  1. insert the usb stick with enough capacity (twice the size of your current installation is usually good), and make sure you have both grub and lilo packages installed already in your linux installation.
  2. if it not automatically mounted yet, use fdisk -l to find out the new device name. otherwise umount /media/disk (which could vary), and use fdisk to delete all the existing partitions, create a new primary partition say /dev/sdb1
  3. make a file system and mount it
  4. copy your current installation to the mounted usb partition
  5. install boot loader (grub or lilo) , and one of them should work.


Here is an example:



sudo su - # become superuser


# create a single partition. if there are existing ones, keep use d to delete them.
$ fdisk /dev/sdb
n
p


w
# format the partition
$ mke2fs -j -m1 /dev/sdb1
# find out your current rootfs label, if your distribution uses label in grub menu
# and /etc/fstab, say your rootfs is /dev/sda1
$ e2label /dev/sda1
ROOTLABEL
$ e2label /dev/sdb1 ROOTLABEL
# otherwise if it uses UUID
$ vol_id /dev/sda1
uuid
$tune2fs -U uuid /dev/sdb1
# mount it
$ mount /dev/sdb1 /media/disk
# copy your current installation. i use cpio, but dump would work too.
# i assume everything in / filesystem. if you have additional partitions
# such as /boot, you need to repeat for all these. dump/restore works too
find / -xdev cpio -pmd /mnt/disk/
# now chroot to the usb partition
mount -o bind /dev /mnt/disk/dev
chroot /mnt/disk
mount -t proc none /proc
mount -t sysfs none /sys
# try grub install
$ grub
grub> root (hd1,0)
grub> setup (hd1)
grub> quit

# you might want to edit /boot/grub/menu.lst to change the root clauses

# to point to hd0,0 instead, as when you boot into usb, it should be the first disk

# but not always, depending on many factors. if you are wrong you can always

# use the grub e command to edit the root clause on the fly and fix it once you

# have booted successfully

# if you seen errors and cannot fix them, then try lilo
# change root device to /dev/sdb
$ vi /etc/lilo.conf
$ lilo

# either lilo or grub should install fine. if not,
# you should consult grub or lilo documentation.
$ umount /proc

$ umount /sys

$ exit

$ umount /media/disk/dev

$ umount /media/disk

$ sync


now you can boot into your usb stick and enjoy a full linux installation. usually with cli, a 1-2 GB stick suffices. with desktop, a 4GB would work.

This is just my notes. I welcome your comments and can revise it and add more explanations.