Reply to comment

I wrote a script for doing all this

  1. #!/bin/sh
  2.  
  3. # this script will create a bootable debian system on the specified device (normally for an usb-key)
  4. # this script is provided AS IS, without any WARRANTY.
  5. # written by bomboclat@malasystem.com
  6. # this script is published under the GPL licence
  7. # thanks for all the ideas published on http://feraga.com/node/25
  8.  
  9. DEVICE="$1"
  10. DEST="/mnt/buildroot"
  11. USBNAME="$2"
  12.  
  13. modprobe ehci_hcd &>/dev/null
  14. modprobe ohci_hcd &>/dev/null
  15. modprobe usbhid &>/dev/null
  16. modprobe usb_storage &>/dev/null
  17.  
  18. function help() {
  19. 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."
  20. exit 1
  21. }
  22.  
  23. function muori() {
  24. if [ "$?" != "0" ]; then
  25. echo -e "$1\n"
  26. exit 1
  27. fi
  28. }
  29.  
  30. if [ -z "$DEVICE" ] ; then
  31. help
  32. echo -e "\nHint!\ton which device?\n"
  33. fi
  34.  
  35. if [ -z "$USBNAME" ] ; then
  36. help
  37. echo -e "\nHint!\twouldn't you give an hostname to your usb-system?\n"
  38. fi
  39.  
  40. for i in "parted" "debootstrap" ; do
  41. dpkg -l | awk '{print $2}' | grep -x "$i" &>/dev/null
  42. if [ $? != "0" ] ; then
  43. echo -e "install $i first!\n"
  44. exit 1
  45. fi
  46. done
  47. echo "***************************************************************************************************"
  48. echo " this script will create a bootable debian system on the specified device (normally for an usb-key)"
  49. echo " this script is provided AS IS, without any WARRANTY."
  50. echo " written by prando@malasystem.com"
  51. echo " this script is published under the GPL licence"
  52. echo " thanks for all the ideas published on <a href="http://feraga.com/node/25"<br />
  53. echo" title="http://feraga.com/node/25"<br />
  54. echo">http://feraga.com/node/25"<br />
  55. echo</a> "***************************************************************************************************"
  56. 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"
  57. sleep 5
  58. echo -e "*** Ok, let's go!\n"
  59. mount | grep "$DEVICE"1 &>/dev/null
  60. [ "$?" = "0" ] && umount "$DEVICE"1
  61. mount | grep "$DEVICE"2 &>/dev/null
  62. [ "$?" = "0" ] && umount "$DEVICE"2
  63. parted -s $DEVICE "mklabel msdos mkpartfs primary ext2 0.0 21.0 mkpartfs primary ext2 21.0 -0 set 1 boot on"
  64. muori "error creating partitions. is there the usb key , or is it working?"
  65.  
  66. sleep 1
  67. echo -e "*** formatting partitions\n"
  68. mke2fs "$DEVICE"1 &>/dev/null
  69. muori "couldn't format the boot partition on your key. exiting."
  70.  
  71. sleep 1
  72. mke2fs -m1 -j "$DEVICE"2 &>/dev/null
  73. muori "couldn't format the root partition on your key. exiting."
  74.  
  75. sleep 1
  76. mkdir $DEST &>/dev/null
  77. mount "$DEVICE"2 /mnt/buildroot &/dev/null
  78. mkdir -p $DEST/boot &>/dev/null
  79. mount "$DEVICE"1 /mnt/buildroot/boot
  80. mount | grep "$DEVICE"1
  81. [ "$?" != "0" ] && muori "partitions not correctly mounted. exiting."
  82.  
  83. echo -e "*** installing base debian system\n"
  84. debootstrap --arch i386 etch $DEST <a href="http://ftp.it.debian.org/debian" title="http://ftp.it.debian.org/debian">http://ftp.it.debian.org/debian</a>
  85. muori "couldn't install the debian base system. do you have a valid connection to the internet? exiting."
  86.  
  87. # system details
  88. echo -e "*** writing some config files.\n*** fstab, hosts, hostname, interfaces, resolv.conf in their locations\n"
  89. cat << EOF > $DEST/etc/fstab
  90. #/etc/fstab: static file system information.
  91. #
  92. /dev/sda2 / ext2 defaults,errors=remount-ro,noatime 0 1
  93. /dev/sda1 /boot ext2 defaults,noatime 0 1
  94. proc /proc proc defaults 0 0
  95. tmpfs /etc/network/run tmpfs defaults,noatime 0 0
  96. tmpfs /tmp tmpfs defaults,noatime 0 0
  97. tmpfs /var/lock tmpfs defaults,noatime 0 0
  98. tmpfs /var/log tmpfs defaults,noatime 0 0
  99. tmpfs /var/run tmpfs defaults,noatime 0 0
  100. tmpfs /var/tmp tmpfs defaults,noatime 0 0
  101. EOF
  102.  
  103. echo "127.0.0.1 localhost.localdoman localhost $USBNAME" > $DEST/etc/hosts
  104. echo $USBNAME > $DEST/etc/hostname
  105.  
  106. # networking
  107. cat << EOF > $DEST/etc/network/interfaces
  108. auto lo
  109. iface lo inet loopback
  110.  
  111. allow-htoplug eth0
  112. iface eth0 inet dhcp
  113. EOF
  114.  
  115. cat << EOF > $DEST/etc/resolv.conf
  116. search
  117. nameserver 208.67.222.222
  118. EOF
  119.  
  120. chroot "$DEST" /usr/bin/apt-get update
  121. chroot "$DEST" /usr/bin/apt-get -qq -y --force-yes grub
  122.  
  123. CHECK="NOTOK"
  124. while [ "$CHECK" != "OK" ] ; do
  125. chroot "$DEST" /usr/bin/apt-cache search linux-image | grep ^linux-image
  126. echo -e "*** which one of the images would you install? (cut and paste the complete name of the package chosen)\n"
  127. read
  128. apt-cache search "$REPLY" | grep ^linux-image | awk '{print $1}' | grep -x "$REPLY"
  129. if [ $? != "0" ] ; then
  130. echo -e "*** the name provided is not a valid name for the kernel package\n"
  131. CHECK="NOTOK"
  132. else
  133. CHECK="OK"
  134. chroot $DEST LC_ALL=C apt-get install $REPLY
  135. if [ $? != "0" ] ; then
  136. 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."
  137. fi
  138. fi
  139. done
  140.  
  141. VERSION="`ls $DEST/boot | grep vmlinuz | sed -e 's/vmlinuz-//'`"
  142. mkdir $DEST/boot/grub &>/dev/null
  143. touch $DEST/boot/grub/menu.lst
  144. cat << EOF > $DEST/boot/grub/menu.lst
  145. # default num
  146. default 0
  147.  
  148. # timeout sec
  149. timeout 5
  150.  
  151. # pretty colours
  152. color green/black black/green
  153.  
  154. title Debian GNU/Linux-$VERSION
  155. root (hd0,0)
  156. kernel /vmlinuz-$VERSION root=/dev/sda2 init=/sbin/init
  157. initrd /initrd.img-$VERSION
  158. savedefault
  159. boot
  160.  
  161. title Debian GNU/Linux-$VERSION (Rescue/Single)
  162. root (hd0,0)
  163. kernel /vmlinuz-$VERSION root=/dev/sda2 init=/sbin/init single
  164. initrd /initrd.img-$VERSION
  165. boot
  166. EOF
  167.  
  168. grub-install --no-floppy --recheck --root-directory=$DEST $DEVICE
  169. [ $? = "0" ] && echo -e "*** will now set the root password for your new system." \
  170. || muori "I'm sorry. something went wrong with grub installation.\nChroot into the usb-key and try yourself."
  171.  
  172. chroot $DEST passwd root
  173.  
  174. 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]"
  175. read
  176. if [ "$REPLY" == "n" ] ; then
  177. umount "$DEVICE"1 && umount "$DEVICE"2
  178. [ "$?" = "0"] && echo -e "*** we have now fineshed succesfully the installation. Try and reboot.\n" \
  179. || 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."
  180. else
  181. chroot $DEST apt-get install localepurge
  182. chroot $DEST localepurge
  183. [ "$?" = "0"] && echo "*** we have now fineshed succesfully the installation. Try and reboot.\n" \
  184. || muori "something went wrong, but this step should be optional and the system should work anyway. Good luck!"
  185. fi
  186.  
  187. exit 0

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options