System bazowy do debugowania

Plik jest zmodyfikowaną wersją prostego skryptu budującego system bazowy umieszczonego na stronie: Bardzo szybki start.

sys-debug.sh
#!/bin/bash
#Distro builder.
#Copyright 2011 Marcin Bis <marcin@at@bis.org.pl>
 
#<Configuration>
#Toolchain prefix (Without ending -).
TARGET=arm-arm9-linux-gnueabi
 
#Directory, the cross-toolchain's sysroot is placed (needs to have "lib" directory, containing target libraries).
SYSROOT=$HOME/x-tools/$TARGET/$TARGET/sysroot
DEBUGROOT=$HOME/x-tools/$TARGET/$TARGET/debug-root
 
#Directory in which the tollchain is installed (assumming executables are in "bin" subdirectory)
TOOLS=$HOME/x-tools/$TARGET
 
#Number of jobs to run simultanously (saves a lot of time on multi-core systems)
JOBS=4
 
#Password for root account in target system:
PASSPHRASE=toor
 
#</Configuration>
 
export SYSROOT TARGET
export PATH=$TOOLS/bin:$PATH
 
function get_all () {
wget -c http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.1.tar.bz2
wget -c http://busybox.net/downloads/busybox-1.18.5.tar.bz2
wget -c http://zlib.net/zlib-1.2.5.tar.gz
wget -c http://matt.ucc.asn.au/dropbear/releases/dropbear-0.52.tar.bz2
}
 
#This depends on versions of downloaded software.
function clean_sources() {
	rm -rf \
		linux-2.6.39.1 \
		busybox-1.18.5 \
		dropbear-0.52 \
		dropbear-target \
		dropbear-host \
		zlib-1.2.5
}
 
function run() {
	echo "Press ENTER to execute virtual system."
	echo "While it is running: Ctrl-a x to quit."
	read
	exec qemu-system-arm \
		-M versatilepb \
		-kernel sys-debug-2011.07.13.img \
		-nographic \
		-append "console=ttyAMA0" \
		-net nic \
		-net user,hostfwd=tcp::2221-:21,hostfwd=tcp::2222-:22,hostfwd=tcp::2345-:2345
}
 
if [ "$1" == "--clean" ]; then
	clean_sources
	exit $?
elif [ "$1" == "--build" ]; then
	#do nothing, just go to first build command
	true
elif [ "$1" == "--get" ]; then
	get_all
	exit $?
elif [ "$1" == "--run" ]; then
	run
	exit $?
else
	cat << EOF
"Usage: $0 <--get|--build|--clean|--run>"
 
$0 --get       #Fetch all the sources to the current directory (from they original locations).
$0 --build     #Build simple system.
$0 --clean     #Removes all build directories (safe after successfull build).
$0 --run       #Run simple system in emulator.
 
See script source for more information.
EOF
	exit 1
fi
 
#Make bash show every command before execution.
set -x
 
#Make staging directory
STAGDIR=$(mktemp -d /tmp/$0.XXXXXX)
 
#================================== Kernel config & modules ====================
tar -xf linux-2.6.39.1.tar.bz2
cd linux-2.6.39.1/
make ARCH=arm CROSS_COMPILE=$TARGET- versatile_defconfig
#Selects EABI and initramfs settings in some cryptic way.
sed -i  \
	-e "s/# CONFIG_AEABI is not set/CONFIG_AEABI=y\nCONFIG_OABI_COMPAT=y\nCONFIG_ARM_UNWIND=y/" \
	-e "s/CONFIG_INITRAMFS_SOURCE=\"\"/CONFIG_INITRAMFS_SOURCE=\"rootfs-spec.txt\"\nCONFIG_INITRAMFS_ROOT_UID=0\nCONFIG_INITRAMFS_ROOT_GID=0\nCONFIG_INITRAMFS_COMPRESSION_NONE=y\n# CONFIG_INITRAMFS_COMPRESSION_GZIP is not set\n# CONFIG_INITRAMFS_COMPRESSION_BZIP2 is not set\n# CONFIG_INITRAMFS_COMPRESSION_LZMA is not set\n# CONFIG_INITRAMFS_COMPRESSION_XZ is not set\n# CONFIG_INITRAMFS_COMPRESSION_LZO is not set/" \
	.config
 
make ARCH=arm CROSS_COMPILE=$TARGET- oldconfig
make -j$JOBS ARCH=arm CROSS_COMPILE=$TARGET- modules
make ARCH=arm CROSS_COMPILE=$TARGET- INSTALL_MOD_PATH=$STAGDIR modules_install
 
cd ..
#================================== Busybox ====================================
tar -xf busybox-1.18.5.tar.bz2
cd busybox-1.18.5
 
make ARCH=arm defconfig
 
sed -i -e "s/CONFIG_FEATURE_IPV6=y/# CONFIG_FEATURE_IPV6 is not set/" .config 
make -j$JOBS ARCH=arm CROSS_COMPILE=$TARGET-
make ARCH=arm CROSS_COMPILE=$TARGET- install
 
BBDIR=$(pwd)/_install
 
cd ..
#================================== Zlib =======================================
tar -xf zlib-1.2.5.tar.gz
cd zlib-1.2.5/
 
CC=$TARGET-gcc ./configure --prefix=$STAGDIR/usr --shared
make $J
make install
cd ..
#================================== Dropbear (target) ==========================
tar -xf dropbear-0.52.tar.bz2
mkdir dropbear-target
cd dropbear-target
 
CPPFLAGS="-I$STAGDIR/usr/include" \
LDFLAGS="-L$STAGDIR/usr/lib" \
../dropbear-0.52/configure \
        --prefix=/usr \
        --host=$TARGET
 
make $J
fakeroot make DESTDIR=$STAGDIR install
 
cd ..
#================================== Dropbear (host) ============================
mkdir dropbear-host
cd dropbear-host
 
../dropbear-0.52/configure
make $J
 
cd ..
#================================== Config files ===============================
 
mkdir $STAGDIR/etc
 
dropbear-host/dropbearkey -t rsa -f $STAGDIR/etc/dropbear_rsa_host_key
 
cat << EOF > $STAGDIR/etc/inittab
null::sysinit:/etc/init.d/rcS
ttyAMA0::askfirst:/sbin/getty 115200 ttyAMA0
tty0::askfirst:/sbin/getty 38400 tty0
null::respawn:tcpsvd -E 0.0.0.0 21 ftpd -w /
EOF
 
mkdir $STAGDIR/etc/init.d
 
cat << EOF >> $STAGDIR/etc/init.d/rcS
#!/bin/ash
mount -t proc proc /proc
mount -t sysfs sys /sys
mount -t devpts devpts /dev/pts
echo "embedded2" > /proc/sys/kernel/hostname
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
ip link set lo up
ip link set eth0 up
ip addr add 10.0.2.15/24 dev eth0
ip route add default via 10.0.2.2
dropbear -r /etc/dropbear_rsa_host_key
EOF
 
cat << EOF >> $STAGDIR/etc/passwd
root:x:0:0:root:/root:/bin/ash
EOF
 
cat << EOF >> $STAGDIR/etc/group
root:x:0:
EOF
 
CPASS=$(echo "$PASSPHRASE" | openssl passwd -1 -stdin)
cat << EOF >> $STAGDIR/etc/shadow
root:$CPASS:14778:0:99999:7:::
EOF
 
cat << EOF >> $STAGDIR/etc/hosts
127.0.0.1 localhost
EOF
 
cat << EOF >> $STAGDIR/etc/resolv.conf
nameserver 8.8.8.8
EOF
 
cat << EOF >> $STAGDIR/etc/shells
/bin/ash
EOF
 
#================================== Kernel final ===============================
cd linux-2.6.39.1/
 
#Standard files
cat > rootfs-spec.txt << EOF
dir /dev 755 0 0
dir /dev/pts 755 0 0
dir /etc 755 0 0
dir /etc/init.d 755 0 0
nod /dev/console 644 0 0 c 5 1
nod /dev/null 644 0 0 c 1 3
dir /bin 755 0 0
dir /lib 755 0 0
dir /sbin 755 0 0
dir /proc 755 0 0
dir /sys 755 0 0
dir /mnt 755 0 0
dir /usr 755 0 0
dir /root 700 0 0
dir /usr/bin 755 0 0
dir /usr/sbin 755 0 0
dir /usr/lib 755 0 0
file /etc/inittab $STAGDIR/etc/inittab 644 0 0
file /etc/init.d/rcS $STAGDIR/etc/init.d/rcS 755 0 0
file /etc/passwd $STAGDIR/etc/passwd 644 0 0
file /etc/group $STAGDIR/etc/group 644 0 0
file /etc/shadow $STAGDIR/etc/shadow 600 0 0
file /bin/busybox $BBDIR/bin/busybox 755 0 0
slink /init bin/busybox 777 0 0
file /etc/hosts $STAGDIR/etc/hosts 644 0 0
file /etc/shells $STAGDIR/etc/shells 644 0 0
file /etc/resolv.conf $STAGDIR/etc/resolv.conf 644 0 0
file /usr/bin/gdbserver $DEBUGROOT/usr/bin/gdbserver 755 0 0
file /etc/dropbear_rsa_host_key $STAGDIR/etc/dropbear_rsa_host_key 600 0 0
file /usr/sbin/dropbear $STAGDIR/usr/sbin/dropbear 755 0 0
file /usr/bin/dbclient $STAGDIR/usr/bin/dbclient 755 0 0
slink /usr/bin/ssh dbclient 777 0 0
file /usr/lib/libz.so.1.2.5 $STAGDIR/usr/lib/libz.so.1.2.5 755 0 0
slink /usr/lib/libz.so.1 libz.so.1.2.5 777 0 0
EOF
 
#Busybox
for file in $(find $BBDIR -type l); do
	F=$(echo $file | sed -e "s%$BBDIR%%")
	L=$(readlink $file)
	echo "slink $F $L 777 0 0"
done >> rootfs-spec.txt
 
#Libraries
LIBDIR=$SYSROOT/lib
for file in $(find $LIBDIR -type f -name '*.so*'); do
	F=$(echo $file | sed -e "s%$LIBDIR%%")
	echo "file /lib$F $file 755 0 0"
done >> rootfs-spec.txt
 
for file in $(find $LIBDIR -type l); do
	F=$(echo $file | sed -e "s%$LIBDIR%%")
	L=$(readlink $file)
	echo "slink /lib$F $L 777 0 0"
done >> rootfs-spec.txt
 
#Modules
MODDIR=$STAGDIR/lib/modules
 
for file in $(find $MODDIR -type d); do
	F=$(echo $file | sed -e "s%$MODDIR%%")
	echo "dir /lib/modules$F 755 0 0"
done >> rootfs-spec.txt
 
for file in $(find $MODDIR -type f -name '*.ko'); do
	F=$(echo $file | sed -e "s%$MODDIR%%")
	echo "file /lib/modules$F $file 755 0 0"
done >> rootfs-spec.txt
 
#Build kernel with embedded ramdisk
make -j$JOBS ARCH=arm CROSS_COMPILE=$TARGET- zImage
cp arch/arm/boot/zImage ../sys-debug-2011.07.13.img
 
#================================== Clean ======================================
rm -rf $STAGDIR
ostatnio zmienione: 2011/07/18 12:51