2005/12/28

Beautiful dirty trick: Embedded only-file bash filesystem

This is a beautiful trick. Tip includes: making an empty 10Mb file; creating a filesystem on it; creating basic tree; creating devices; copying bash and required libs; unmounting it; making it a boot file and adding a grub entry.

Put this on an executable-file-script and run it (tested on Fedora Core 4):

#!/bin/bash
#Script start ----------------------
#Bash only filesystem on a file - rodolfoap@hotmail.com

#Create an empty 10Mb file
dd if=/dev/zero of=/tmp/embed bs=1k count=10k

#Make an ext2fs
/sbin/mkfs.ext2 -q /tmp/embed

#Mount it as a loop
mkdir /mnt/embed
mount /tmp/embed /mnt/embed -o loop
cd /mnt/embed

#Create basic structure and fill it with needed files
mkdir bin dev lib
cp -a /dev/tty /dev/console /dev/ram dev/
cp /bin/bash bin/
#to found libs needed by bash, use # ldd /bin/bash
cp /lib/libtermcap.so.2 lib/
cp /lib/libdl.so.2 lib/
cp /lib/libc.so.6 lib/
cp /lib/ld-linux.so.2 lib/

#Create boot file
cd /tmp
umount /mnt/embed
gzip < embed > /boot/embed.gz
echo "
title Bash only filesystem
root (hd0,2)
kernel /boot/vmlinuz-2.6.14-1.1644_FC4 ro root=/dev/ram init=/bin/bash
initrd /boot/embed.gz" >> /etc/grub.conf
#Script end ------------------------

Reboot and choose the "Bash only filesystem" option.