#!/bin/sh # this script will create a bootable debian system on the specified device (normally for an usb-key) # this script is provided AS IS, without any WARRANTY. # written by bomboclat@malasystem.com # this script is published under the GPL licence # thanks for all the ideas published on http://feraga.com/node/25 DEVICE="$1" DEST="/mnt/buildroot" USBNAME="$2" modprobe ehci_hcd &>/dev/null modprobe ohci_hcd &>/dev/null modprobe usbhid &>/dev/null modprobe usb_storage &>/dev/null function help() { echo -e "Usage:\t$0 devicename systemname\n\twhere devicename is where your usb key is (/dev/sda for example)\n\tand devicename is to set the hostname for your new system on usb-key." exit 1 } function muori() { if [ "$?" != "0" ]; then echo -e "$1\n" exit 1 fi } if [ -z "$DEVICE" ] ; then help echo -e "\nHint!\ton which device?\n" fi if [ -z "$USBNAME" ] ; then help echo -e "\nHint!\twouldn't you give an hostname to your usb-system?\n" fi for i in "parted" "debootstrap" ; do dpkg -l | awk '{print $2}' | grep -x "$i" &>/dev/null if [ $? != "0" ] ; then echo -e "install $i first!\n" exit 1 fi done echo "***************************************************************************************************" echo " this script will create a bootable debian system on the specified device (normally for an usb-key)" echo " this script is provided AS IS, without any WARRANTY." echo " written by prando@malasystem.com" echo " this script is published under the GPL licence" echo " thanks for all the ideas published on http://feraga.com/node/25" echo "***************************************************************************************************" echo -e "\n*** WARNING!! this script will DESTROY all the data contained in $DEVICE.\n*** exit now pressing ctrl+C if not sure. you have 5 seconds to decide.\n" sleep 5 echo -e "*** Ok, let's go!\n" mount | grep "$DEVICE"1 &>/dev/null [ "$?" = "0" ] && umount "$DEVICE"1 mount | grep "$DEVICE"2 &>/dev/null [ "$?" = "0" ] && umount "$DEVICE"2 parted -s $DEVICE "mklabel msdos mkpartfs primary ext2 0.0 21.0 mkpartfs primary ext2 21.0 -0 set 1 boot on" muori "error creating partitions. is there the usb key , or is it working?" sleep 1 echo -e "*** formatting partitions\n" mke2fs "$DEVICE"1 &>/dev/null muori "couldn't format the boot partition on your key. exiting." sleep 1 mke2fs -m1 -j "$DEVICE"2 &>/dev/null muori "couldn't format the root partition on your key. exiting." sleep 1 mkdir $DEST &>/dev/null mount "$DEVICE"2 /mnt/buildroot &/dev/null mkdir -p $DEST/boot &>/dev/null mount "$DEVICE"1 /mnt/buildroot/boot mount | grep "$DEVICE"1 [ "$?" != "0" ] && muori "partitions not correctly mounted. exiting." echo -e "*** installing base debian system\n" debootstrap --arch i386 etch $DEST http://ftp.it.debian.org/debian muori "couldn't install the debian base system. do you have a valid connection to the internet? exiting." # system details echo -e "*** writing some config files.\n*** fstab, hosts, hostname, interfaces, resolv.conf in their locations\n" cat $DEST/etc/fstab #/etc/fstab: static file system information. # /dev/sda2 / ext2 defaults,errors=remount-ro,noatime 0 1 /dev/sda1 /boot ext2 defaults,noatime 0 1 proc /proc proc defaults 0 0 tmpfs /etc/network/run tmpfs defaults,noatime 0 0 tmpfs /tmp tmpfs defaults,noatime 0 0 tmpfs /var/lock tmpfs defaults,noatime 0 0 tmpfs /var/log tmpfs defaults,noatime 0 0 tmpfs /var/run tmpfs defaults,noatime 0 0 tmpfs /var/tmp tmpfs defaults,noatime 0 0 EOF echo "127.0.0.1 localhost.localdoman localhost $USBNAME" > $DEST/etc/hosts echo $USBNAME > $DEST/etc/hostname # networking cat $DEST/etc/network/interfaces auto lo iface lo inet loopback allow-htoplug eth0 iface eth0 inet dhcp EOF cat $DEST/etc/resolv.conf search nameserver 208.67.222.222 EOF chroot "$DEST" /usr/bin/apt-get update chroot "$DEST" /usr/bin/apt-get -qq -y --force-yes grub CHECK="NOTOK" while [ "$CHECK" != "OK" ] ; do chroot "$DEST" /usr/bin/apt-cache search linux-image | grep ^linux-image echo -e "*** which one of the images would you install? (cut and paste the complete name of the package chosen)\n" read apt-cache search "$REPLY" | grep ^linux-image | awk '{print $1}' | grep -x "$REPLY" if [ $? != "0" ] ; then echo -e "*** the name provided is not a valid name for the kernel package\n" CHECK="NOTOK" else CHECK="OK" chroot $DEST LC_ALL=C apt-get install $REPLY if [ $? != "0" ] ; then muori "something went wrong with your kernel installation.\nYou can check this later, by chrooting into the key and re-run the install, BUT do this before rebooting your usb-key, becouse it will not work with a wrong kernel install." fi fi done VERSION="`ls $DEST/boot | grep vmlinuz | sed -e 's/vmlinuz-//'`" mkdir $DEST/boot/grub &>/dev/null touch $DEST/boot/grub/menu.lst cat $DEST/boot/grub/menu.lst # default num default 0 # timeout sec timeout 5 # pretty colours color green/black black/green title Debian GNU/Linux-$VERSION root (hd0,0) kernel /vmlinuz-$VERSION root=/dev/sda2 init=/sbin/init initrd /initrd.img-$VERSION savedefault boot title Debian GNU/Linux-$VERSION (Rescue/Single) root (hd0,0) kernel /vmlinuz-$VERSION root=/dev/sda2 init=/sbin/init single initrd /initrd.img-$VERSION boot EOF grub-install --no-floppy --recheck --root-directory=$DEST $DEVICE [ $? = "0" ] && echo -e "*** will now set the root password for your new system." \ || muori "I'm sorry. something went wrong with grub installation.\nChroot into the usb-key and try yourself." chroot $DEST passwd root echo -e "*** would you run localepurge to delete some useless locales and free some space on your installed usb-key system?\n\n[Y/n]" read if [ "$REPLY" == "n" ] ; then umount "$DEVICE"1 && umount "$DEVICE"2 [ "$?" = "0"] && echo -e "*** we have now fineshed succesfully the installation. Try and reboot.\n" \ || muori "check your mounted partitions on $DEST. I couldn't umount them.\nis there some program running in that partitions?\nfuser -m $DEST should provide some infos and fuser -k -m $DEST should do the work for you." else chroot $DEST apt-get install localepurge chroot $DEST localepurge [ "$?" = "0"] && echo "*** we have now fineshed succesfully the installation. Try and reboot.\n" \ || muori "something went wrong, but this step should be optional and the system should work anyway. Good luck!" fi exit 0