mobileRoot - Linux auf USB-Festplatte mit USB-Boot-CD


Live-CDs gibt es viele. Umfangreiche, wie z. B. www.knopper.net/knoppix zeigen, was Linux alles kann. Kleinere, wie damnsmalllinux.org und www.puppyos.com zeigen, wie klein man ein Linux machen kann.

 

Hier wird eine Möglichkeit vorgestellt, wie man genau sein eigenes Linux mit seinen persönlichen Einstellungen, dem gewählten Benutzernamen und den richtigen Rechten an den Dateien im Homeverzeichnis in der Tasche mitnehmen kann. Die richtigen Rechte sind z. B. eine Voraussetzung, um sein mobiles Homeverzeichnis mit dem Rechner zuhause synchronisieren zu können, z. B. mit unison. Und natürlich möchte man unterwegs die gleichen Pakete wie zu Hause nutzen. Dazu packen wir unser Linux, so wie es sein soll, auf eine USB-Festplatte - es gibt ja fast überall genügend Rechner, die man mit einem solchen Live-System booten kann. Nicht alle Rechner können allerdings direkt von USB-Devices booten und so benötigen wir eine zusätzliche Boot-CD.

 

mobileRoot-0.4

 

Schritt 1 - Installation eines Linux Systems


Man kann auf einer freien Partition oder auch direkt auf der USB-Festplatte ein weiterse Linux installieren. Oder man kopiert das derzeitige System einfach rüber. Zum Kopieren sollte sollte das System nicht in Benutzung sein; also dazu z. B. Knoppix booten, alles beimounten und los (Beispiel ggf. anpassen):

 

# mkdir /mnt/meinSystem

# mkdir /mnt/usbplatte

# mount /dev/hda3 /mnt/meinSystem

# mount /dev/sda1 /mnt/usbplatte

# cd /mnt/meunSystem

# tar -cvpf - . | tar -C /mnt/usbplatte -xpf -

# umount /mnt/meinSystem

# umount /mnt/usbplatte

 

Damit wir später beim Booten die device wiederfinden geben wir ihr das Label mobile Root:

 

# tune2fs -L mobileRoot /dev/sda1

 

 

Schritt 2 - Kernel anpassen


Wir verwenden den Origalkernel der Installation. Er sollte folgendes einkompiliert enthalten:

- RAM Disk support und initrd support mit 8192 kB Größe

- Loopback Block Device Support

- ext2/ext3 Filesystem Support

- tmpfs Filesystem Support

 

 

Schritt 3 - Das linuxrc Script


Das Booten von der CD in die USB-Festplatte läuft so ab, dass wir ein Mini-Linux in die Ramdisk laden und über das linuxrc Script dann auf das System der Platte wechseln. Hier das Script:

 

#!/bin/sh

 

# linuxrc Skript

# wird vom kernel von der cd ausgefuehrt und startet das umhaengen zum usb-device

 

# wir suchen das device mit diesem label

SUCHE="mobileRoot"

# also vorher label mit tune2fs auf der richtigen partition am usb device setzen

 

echo

echo "+++++++++++++++++++++++++++++"

echo "+ starte usb-device +"

echo "+ (c) Linux User Group Saar +"

echo "+++++++++++++++++++++++++++++"

echo

 

export PATH="/bin:/sbin:/usr/bin"

 

 

# Speichern der Kernel CMDLINE

mount -t proc none /proc

CMDLINE=`cat /proc/cmdline`

umount /proc

 

# cd-rom Laufwerk suchen und baum der module mounten

for x in hda hdb hdc hdd

do

mount -t iso9660 -r /dev/${x} /cdrom >/dev/null 2>&1

if [ "$?" = "0" ]

then

CDROM="${x}"

echo "cdrom Laufwerk als /dev/$CDROM gefunden"

FIND="1"

echo "lade module von der cd"

mount --bind /cdrom/modules /lib/modules

# cryptomodule laden

#/sbin/modprobe aes

#/sbin/modprobe dm-crypt

break

fi

done

 

if [ "${CDROM}" == "" ]

then

echo "FEHLER: cdrom nicht gefunden, kann keine module laden"

#exec /bin/sh

#exit

fi

 

 

# warten bis der Kernel das usb-Laufwerk findet

echo

echo "partition mit label $SUCHE suchen"

sleep 8

 

for device in sda1 sda2 sda3 sdb1 sdb2 sdb3 sdc1 sdc2 sdc3 sdd1 sdd2 sdd3 sde1 sde2 sde3

do

echo "teste, ob $device das richtige device ist ..."

MYLABEL=`tune2fs -l /dev/$device 2>/dev/null | grep -c -e $SUCHE 2>/dev/null`

sleep 1

if [ "$MYLABEL" = 1 ]

then

echo "$device gefunden"

mount -t ext3 -o rw /dev/$device /new >/dev/null 2>&1

break

fi

 

if [ "$device" = sde3 ]

then

echo "kein usb-device mit label $SUCHE gefunden - starte mini shell"

exec /bin/sh

exit

fi

done

 

if [ "$FIND" = "1" ]

then

echo "cdrom auswerfen"

umount /cdrom/modules

umount /cdrom

eject /dev/$CDROM

fi

 

echo "pivot root ausfuehren"

# pivot root umhaengen und den eigentlichen bootprozess starten

 

cd /new

/sbin/pivot_root . newroot

exec chroot . /bin/sh -c "exec /sbin/init ${CMDLINE}"

 

 

Schritt 4 - initrd anlegen


Hier kommt ein Script, das aus dem bestehenden System dir nötigen Daten extrahiert und in ein initrd-File schreibt. Im Kopf des Scriptes müssen die Quell- und Zielverzeichnisse und der Kernel (so wie er in /boot heißt) angepasst werden.

 

#!/bin/bash

 

DIR=`pwd`

echo "Arbeitsverzeichnis $DIR"

Quelle=/mnt/usbplatte

echo "Quellverzeichnis $Quelle"

echo

Kernel=2.6.12-gentoo-r10

echo "Kernel $Kernel"

echo

echo "[enter] fuer weiter oder [ctr]-[c] fuer stop"

read user_reply

 

#cd $Quelle

 

# beim ersten Durchlauf dieses entkommentieren, damit leeres loop file angelegt wird

# dann wieder einkommentieren

 

#touch $DIR/initrd

#dd if=/dev/zero of=$DIR/initrd bs=1024k count=8

 

 

echo

echo "initrd als loopfile anlegen und ext2 formatieren"

losetup /dev/loop0 $DIR/initrd

mke2fs /dev/loop0

echo mounten

mount /dev/loop0 $DIR/mntpnt

 

echo

echo "das loop Abbild mit notwendigen Dateien und Verzeichnissen fuellen"

 

cd $DIR/mntpnt

 

mkdir etc dev lib bin proc new img usb tmpfs newroot base cdrom home sbin usr mnt sys rfsys

mkdir lib/modules

mkdir usr/bin

mkdir usr/lib

touch etc/mtab

touch etc/fstab

 

 

cp -a $DIR/linuxrc .

 

for x in sh ls cat mount umount dmesg mkdir chroot tar cp sleep echo cut grep cryptsetup

do

cp -v $Quelle/bin/${x} ./bin

done

 

 

for x in pivot_root modprobe tune2fs e2fsck grub fdisk dmsetup

do

cp -v $Quelle/sbin/${x} ./sbin

done

 

cp -v $Quelle/usr/bin/test ./usr/bin

cp -v $Quelle/usr/bin/mkdir ./usr/bin

cp -v $Quelle/usr/bin/eject ./usr/bin

 

 

# Hinweis: module sind 23MB gross

echo "/etc/modules.conf"

#cp -a $Quelle/lib/modules/ ./lib/modules

cp $Quelle/etc/modules.conf ./etc

 

 

echo

echo "benoetigte libraries hinzu"

 

cd $Quelle/lib

 

cp -v `ldd $Quelle/usr/bin/test |egrep lib|cut -d "=" -f1` $DIR/mntpnt/lib 2> $DIR/log

cp -v `ldd $Quelle/usr/bin/eject |egrep lib|cut -d "=" -f1` $DIR/mntpnt/lib 2> $DIR/log

cp -v `ldd $Quelle/usr/bin/mkdir |egrep lib|cut -d "=" -f1` $DIR/mntpnt/lib 2> $DIR/log

 

cp -v `ldd $Quelle/usr/bin/dmesg |egrep lib|cut -d "=" -f1` $DIR/mntpnt/lib 2> $DIR/log

 

for x in sh ls cat mount umount chroot tar cp sleep echo cut grep

do

cp -v `ldd $Quelle/bin/${x}|egrep lib|cut -d "=" -f1` $DIR/mntpnt/lib 2> $DIR/log

done

 

 

for x in pivot_root modprobe tune2fs e2fsck grub fdisk dmsetup

do

cp -v `ldd $Quelle/sbin/${x}|egrep lib|cut -d "=" -f1` $DIR/mntpnt/lib 2> $DIR/log

done

 

# cryptomodule fuer cryptsetup-luks

cp -a $Quelle/usr/lib/libcrypt* $DIR/mntpnt/usr/lib

cp -a $Quelle/lib/libcrypt* $DIR/mntpnt/lib

 

 

echo

echo "devices anlegen"

 

mknod $DIR/mntpnt/dev/console c 5 1

mknod $DIR/mntpnt/dev/null c 1 3

 

 

mknod $DIR/mntpnt/dev/hda b 3 0

mknod $DIR/mntpnt/dev/hda1 b 3 1

mknod $DIR/mntpnt/dev/hda2 b 3 2

mknod $DIR/mntpnt/dev/hda3 b 3 3

 

mknod $DIR/mntpnt/dev/hdb b 3 64

mknod $DIR/mntpnt/dev/hdb1 b 3 65

mknod $DIR/mntpnt/dev/hdb2 b 3 66

mknod $DIR/mntpnt/dev/hdb3 b 3 67

 

mknod $DIR/mntpnt/dev/hdc b 22 0

mknod $DIR/mntpnt/dev/hdc1 b 22 1

mknod $DIR/mntpnt/dev/hdc2 b 22 2

mknod $DIR/mntpnt/dev/hdc3 b 22 3

 

mknod $DIR/mntpnt/dev/hdd b 22 64

mknod $DIR/mntpnt/dev/hdd1 b 22 65

mknod $DIR/mntpnt/dev/hdd2 b 22 66

mknod $DIR/mntpnt/dev/hdd3 b 22 67

 

mknod $DIR/mntpnt/dev/hde b 33 0

mknod $DIR/mntpnt/dev/hde1 b 33 1

mknod $DIR/mntpnt/dev/hde2 b 33 2

mknod $DIR/mntpnt/dev/hde3 b 33 3

 

mknod $DIR/mntpnt/dev/tty c 4 0

 

mknod $DIR/mntpnt/dev/loop0 b 7 0

mknod $DIR/mntpnt/dev/loop1 b 7 1

mknod $DIR/mntpnt/dev/loop2 b 7 2

 

mknod $DIR/mntpnt/dev/sda1 b 8 1

mknod $DIR/mntpnt/dev/sda2 b 8 2

mknod $DIR/mntpnt/dev/sda3 b 8 3

 

mknod $DIR/mntpnt/dev/sdb1 b 8 17

mknod $DIR/mntpnt/dev/sdb2 b 8 18

mknod $DIR/mntpnt/dev/sdb3 b 8 19

 

mknod $DIR/mntpnt/dev/sdc1 b 8 33

mknod $DIR/mntpnt/dev/sdc2 b 8 34

mknod $DIR/mntpnt/dev/sdc3 b 8 35

 

mknod $DIR/mntpnt/dev/sdd1 b 8 49

mknod $DIR/mntpnt/dev/sdd2 b 8 50

mknod $DIR/mntpnt/dev/sdd3 b 8 51

 

mknod $DIR/mntpnt/dev/sde1 b 8 65

mknod $DIR/mntpnt/dev/sde2 b 8 66

mknod $DIR/mntpnt/dev/sde3 b 8 67

 

mkdir $DIR/mntpnt/dev/mapper

mknod $DIR/mntpnt/dev/mapper/control c 10 62

#mknod $DIR/mntpnt/dev/mapper/cryptpart b 253 0

 

umount $DIR/mntpnt

losetup -d /dev/loop0

echo

echo "fertig, abmounten"

 

# logeintrag mit cp: cannot stat `(0xb7fc6000)': No such file or directory

# ist nur ein kleiner schoenheitsfehler

rm $DIR/log

 

echo "jetzt kann die boot cd erstellt werden"

 

 

Schritt 5 - Boot-CD erstellen


Auch hier wieder im Kopf des Scriptes die Verzeichnisse usw. anpassen.

 

#!/bin/bash

 

Quelle=/mnt/usbplatte

Kernel=kernel-2.6.12-gentoo-r10

Kernelmod=2.6.12-gentoo-r10

 

DIR=`pwd`

 

echo

echo "boot cd anlegen aus der Quelle $Quelle"

echo "der kernel $Quelle/boot/$Kernel und die initrd (muss vorher erstellt sein!) werden reinkopiert"

echo "diese angaben koennen ueber die configzeilen in diesem script angepasst werden"

echo "[enter] fuer weiter oder [ctrl]-[c] fuer stop"

read user_reply

 

echo

echo "boot verzeichnis in target anlegen"

rm -rd target 2> $DIR/log

mkdir target

 

echo "kernel, grub verzeichnisse und initrd reinkopieren"

mkdir target/boot

cp -a $Quelle/boot/$Kernel target/boot

cp -a $Quelle/boot/grub target/boot

cp -a $DIR/initrd target/boot

cp -a $Quelle/lib/modules/ $DIR/target

 

echo "schreibe grubs menu.lst"

unlink target/boot/grub/menu.lst 2> $DIR/log

rm target/boot/grub/grub.conf 2> $DIR/log

# anmerkung iso9660 kann keine links

 

echo "default 0" > target/boot/grub/menu.lst

echo "timeout 3" >> target/boot/grub/menu.lst

echo "title=GNU-Linux live usb-device" >> target/boot/grub/menu.lst

echo "root (cd)" >> target/boot/grub/menu.lst

echo "kernel (cd)/boot/$Kernel video=vesafb-tng:1024x768-16@60 root=/dev/ram0 rw init=/linuxrc cdroot" >> target/boot/grub/menu.lst

echo "initrd (cd)/boot/initrd" >> target/boot/grub/menu.lst

 

echo

echo "bootCD.iso schreiben"

 

mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -iso-level 4 -hide boot.catalog -o bootCD.iso target

 

#rm -rd target 2> $DIR/log

#rm $DIR/log

echo

echo "fertig, jetzt etwa so brennen: cdrecord -v dev=/dev/hdc bootCD.iso"

 

 

Schritt 6 - Boot-CD Brennen


 

# cdrecord -v dev=/dev/hdc bootCD.iso"

 

 

Schritt 7 - Verschlüsseltes Filesystem


Als Erweiterung ist es geplant, von einem Verschlüsselten Filesystem zu booten. Demnächst mehr.

 

 

Schritt 8 - Danke


Ich danke allen für Erklärungen, Hilfen, Beschreibungen und freie Software.

 

mancas <at> lug-saar.de