Want more features on Pastebin? Sign Up, it's FREE!
Guest

Minimal Debian on USB

By: a guest on Jan 2nd, 2013  |  syntax: Bash  |  size: 14.10 KB  |  views: 50  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash -e
  2.  
  3. if [[ "$UID" != "0" ]] ; then
  4.     echo 'This script can only be run as root'
  5.     exit 1
  6. fi
  7.  
  8. export DEV='/dev/sdc'
  9. export USB="${DEV}1"
  10. export CHROOT='/media/chroot'
  11. export ARCH='i386'
  12. export CODE_NAME='testing'
  13. export MNT='/mnt'
  14. export MIRROR='http://http.us.debian.org/debian/'
  15. export KERNEL="linux-image-686"
  16.  
  17. #These are extra packages that make using the USB for
  18. #non-installation tasks easier.
  19. export EXTRAS='xorg lxde-common iceweasel build-essential ntfs-3g rsync p7zip unzip unrar'
  20.  
  21. #This funtion checks that a given device or path is mounted, and if the
  22. #parameter is, this function then unmounts it.
  23. #param: A mount point or device file
  24. function unmount {
  25.     if mount | grep $1 > /dev/null ; then
  26.         if ! umount -v -f $1 ; then
  27.             #Try lazy unmounting as a last resort
  28.             umount -v -l $1
  29.         fi
  30.     fi
  31. }
  32.  
  33. #This function unmounts all the mount-points created in this script. It is used
  34. #to clean-up at the end or after an error occurs. This function does not check
  35. #"devices" it instead calls the unmount function using mount points. Hence the
  36. #$USB device may still be mounted after a call to this function if it is
  37. #mounted anywhere other then $MNT.
  38. function unmount_all {
  39.     unmount $CHROOT/proc
  40.     unmount $CHROOT/dev/pts
  41.     unmount $CHROOT/dev/shm
  42.     unmount $CHROOT/dev
  43.     unmount $CHROOT/sys
  44.     unmount $CHROOT/boot
  45.     unmount $MNT
  46. }
  47.  
  48. #This function is used to cleanly exit the script. It does this by unmounting
  49. #all devices, displaying a given error message, and exiting with an error
  50. #code.
  51. #param: The error message to display.
  52. #usage: die "This is an error"
  53. function die {
  54.     unmount_all
  55.     echo
  56.     echo "***************************************************"
  57.     echo "$@"
  58.     echo "***************************************************"
  59.     echo
  60.     exit 1
  61. }
  62.  
  63. #We may have been left in a bad state so unmount everything
  64. unmount_all
  65.  
  66. if [ ! -e $DEV ] ; then
  67.     die "USB device $DEV does not exist."
  68. fi
  69.  
  70. #Unmount the USB drive if it is mounted
  71. if mount | grep $USB >/dev/null ; then
  72.     echo "Unmounting device $USB"
  73.     if ! umount -v $USB ; then
  74.         die "Could not unmount $USB"
  75.     fi
  76. fi
  77.  
  78. echo "Remaking the file USB drive's partition table"
  79. if ! ( echo -e 'o\nn\np\n1\n\n\nt\n83\na\n1\nw\n' | fdisk -c -u "$DEV" ) ; then
  80.     die 'Could not fix the USB drives partition table'
  81. fi
  82.  
  83. echo "Formating and tuning the USB drive"
  84. if ! (mkfs.ext3 -L 'USB-Installer' "$USB" && tune2fs -c 0 -i 0 $USB) ; then
  85.    die 'Could not format drive'
  86. fi
  87.  
  88. #If the $CHROOT directory exits, destroy it so we have a clean slate.
  89. if [ -d $CHROOT ] ; then
  90.     echo "Clearing $CHROOT for a clean slate ..."
  91.     if ! rm -rf "$CHROOT" ; then
  92.         die "Could not remove the directory"
  93.     fi
  94. fi
  95.  
  96. #Create the $CHROOT directory
  97. if ! mkdir -vp "$CHROOT" ; then
  98.     die "Could not create the chroot directory at $CHROOT"
  99. fi
  100.  
  101. if [ ! -f /usr/sbin/debootstrap ] ; then
  102.     sudo apt-get -y install debootstrap
  103. fi
  104.  
  105. echo 'Bootstraping in a basic system'
  106. if ! /usr/sbin/debootstrap --arch "$ARCH" "$CODE_NAME" "$CHROOT" "$MIRROR" ; then
  107.     die 'Failed to bootstrap'
  108. fi
  109.  
  110. #Copy the timezone info from the system to the new USB drive
  111. if ! cp /etc/timezone "$CHROOT/etc/timezone" ; then
  112.     die 'Failed to update the /etc/timezone file'
  113. fi
  114.  
  115. #Set some imporant networking configuration files
  116. echo "Set $CHROOT/etc/hostname"
  117. if ! echo 'usb' > $CHROOT/etc/hostname ; then
  118.     die "Failed to update the /etc/hostname file"
  119. fi
  120.  
  121. if ! echo '127.0.0.1 localhost usb' > $CHROOT/etc/hosts ; then
  122.     die 'Failed to create the /etc/hosts file'
  123. fi
  124.  
  125. if ! echo "deb $MIRROR ${CODE_NAME} main contrib non-free
  126. deb http://security.debian.org ${CODE_NAME}/updates main contrib non-free
  127. " > $CHROOT/etc/apt/sources.list ; then
  128.     die 'Failed to create /etc/apt/sources.list file'
  129. fi
  130.  
  131. if ! echo "
  132. proc      /proc     proc    defaults          0 0
  133. tmpfs     /tmp      tmpfs   defaults,noatime  0 0
  134.  
  135. #Allow useless files to be automatically deleted
  136. tmpfs     /var/lock tmpfs   defaults,noatime  0 0
  137. tmpfs     /var/run  tmpfs   defaults,noatime  0 0
  138. tmpfs     /var/tmp  tmpfs   defaults,noatime  0 0
  139. " > $CHROOT/etc/fstab ; then
  140.     die 'Failed to create /etc/fstab file'
  141. fi
  142.  
  143. if ! echo "
  144. # Kernel Image management overrides
  145. # See kernel-img.conf(5) for details
  146. do_symlinks = yes
  147. relative_links = yes
  148. do_bootloader = no
  149. do_bootfloppy = no
  150. do_initrd = yes
  151. link_in_boot = no" > $CHROOT/etc/kernel-img.conf ; then
  152.     die 'Failed to create /etc/kernel-img.conf file'
  153. fi
  154.  
  155. if ! echo "syntax on
  156. set noai
  157. set expandtab
  158. set ts=2
  159. set sw=2" > $CHROOT/root/.vimrc ; then
  160.     die 'Failed to create /root/.vimrc file'
  161. fi
  162.  
  163. if ! cp $CHROOT/root/.vimrc $CHROOT/etc/skel/ ; then
  164.     die 'Copy the .vimrc file to /etc/skel'
  165. fi
  166.  
  167.  
  168. echo "Binding the needed directories under $CHROOT ..."
  169. if [ ! -d ${CHROOT}${MNT} ] ; then
  170.     mkdir ${CHROOT}${MNT}
  171. fi
  172.  
  173. if ! (mount -v --bind /proc $CHROOT/proc       &&
  174.       mount -v --bind /sys $CHROOT/sys         &&
  175.       mount -v --bind /dev $CHROOT/dev         &&
  176.       mount -v --bind /dev/pts $CHROOT/dev/pts &&
  177.       mount -v --bind /dev/shm $CHROOT/dev/shm ) ; then
  178.     die 'A mount failed!'
  179. fi
  180.  
  181. if ! mount -v -t ext3 $USB $MNT ; then
  182.     die "Failed to mount $USB at $MNT"
  183. fi
  184.  
  185. if ! mkdir -v $MNT/boot $MNT/live ; then
  186.     die "Could not create $MNT/boot and $MNT/live"
  187. fi
  188.  
  189. if ! mount -v -o bind $MNT/boot $CHROOT/boot ; then
  190.     die "Could not bind $MNT/boot to $CHROOT/boot"
  191. fi
  192.  
  193. #Install the .bashrc file for the new system
  194. cat > $CHROOT/root/.bashrc <<EOF
  195. #!/bin/bash
  196.  
  197. #Include the default bashrc file
  198. . /etc/skel/.bashrc
  199.  
  200. #Set the prompt
  201. export PS1="\\[\\033[0;31m\\]\\u:\\w#\\[\\033[0m\\] "
  202.  
  203. umask 022
  204.  
  205. #Make file removal interactive by default
  206. alias rm="rm -i"
  207.  
  208. if ! ifconfig eth0 2> /dev/null | grep -q 'inet addr' ; then
  209.   sed 's/allow-hotplug/auto/' /etc/network/interfaces > /tmp/interfaces
  210.   mv /tmp/interfaces  /etc/network/interfaces
  211.  
  212.   if ! ( /etc/init.d/networking stop && /etc/init.d/networking start ); then
  213.     echo "Networking is not functioning!!!!"
  214.   fi
  215. fi
  216.  
  217. EOF
  218.  
  219. #This should keep any prompts from showing up
  220. if ! echo "grub-pc grub-pc/kopt_extracted  boolean false
  221. grub-pc grub2/kfreebsd_cmdline  string
  222. grub-pc grub2/device_map_regenerated    note
  223. grub-pc grub-pc/install_devices multiselect     $USB
  224. grub-pc grub-pc/postrm_purge_boot_grub  boolean false
  225. grub-pc grub-pc/install_devices_failed_upgrade  boolean true
  226. grub-pc grub2/linux_cmdline     string
  227. grub-pc grub-pc/install_devices_empty   boolean false
  228. grub-pc grub2/kfreebsd_cmdline_default  string  quiet
  229. grub-pc grub-pc/install_devices_failed  boolean false
  230. grub-pc grub-pc/install_devices_disks_changed   multiselect
  231. grub-pc grub2/linux_cmdline_default     string  quiet
  232. grub-pc grub-pc/chainload_from_menu.lst boolean true
  233. grub-pc grub-pc/mixed_legacy_and_grub2  boolean true
  234. keyboard-configuration  keyboard-configuration/modelcode        string  pc105
  235. keyboard-configuration  keyboard-configuration/unsupported_config_options       booleantrue
  236. keyboard-configuration  keyboard-configuration/unsupported_config_layout        booleantrue
  237. keyboard-configuration  keyboard-configuration/toggle   select  No toggling
  238. keyboard-configuration  keyboard-configuration/compose  select  No compose key
  239. keyboard-configuration  keyboard-configuration/layout   select
  240. keyboard-configuration  keyboard-configuration/xkb-keymap       select  us
  241. keyboard-configuration  keyboard-configuration/variant  select  USA
  242. keyboard-configuration  keyboard-configuration/switch   select  No temporary switch
  243. keyboard-configuration  keyboard-configuration/unsupported_options      boolean true
  244. keyboard-configuration  keyboard-configuration/altgr    select  The default for the keyboard layout
  245. keyboard-configuration  keyboard-configuration/ctrl_alt_bksp    boolean false
  246. keyboard-configuration  keyboard-configuration/unsupported_layout       boolean true
  247. keyboard-configuration  keyboard-configuration/variantcode      string
  248. keyboard-configuration  keyboard-configuration/model    select  Generic 105-key (Intl) PC
  249. keyboard-configuration  keyboard-configuration/layoutcode       string  us
  250. keyboard-configuration  keyboard-configuration/store_defaults_in_debconf_db     booleantrue
  251. keyboard-configuration  keyboard-configuration/optionscode      string
  252. console-setup   console-setup/codeset47 select  . Combined - Latin; Slavic Cyrillic; Greek
  253. console-setup   console-setup/fontface47        select  Fixed
  254. console-setup   console-setup/fontsize-text47   select  16
  255. console-setup   console-setup/charmap47 select  UTF-8
  256. console-setup   console-setup/codesetcode       string  Uni2
  257. console-setup   console-setup/store_defaults_in_debconf_db      boolean true
  258. console-setup   console-setup/fontsize-fb47     select  16
  259. console-setup   console-setup/fontsize  string  16
  260. " > $CHROOT/root/conf ; then
  261.     die "Failed to create pre-selections file."
  262. fi
  263.  
  264. #Create a script that does installation under chroot
  265. if ! echo "#!/bin/bash -e
  266.  
  267. ln -sf /usr/share/zoneinfo/US/Central /etc/localtime
  268.  
  269. groupadd admin
  270. usermod -a -G 'users,plugdev,audio,cdrom,admin,disk' root
  271. passwd -d root
  272.  
  273. #/usr/sbin/locale-gen en_US.UTF-8
  274.  
  275. debconf-set-selections /root/conf
  276. rm /root/conf
  277. export DEBIAN_FRONTEND=noninteractive
  278.  
  279. apt-get clean
  280. apt-get update || apt-get update
  281. apt-get -y dist-upgrade && apt-get -y autoremove
  282.  
  283. apt-get -y install grub2
  284. apt-get -y install $KERNEL dhcpcd squashfs-tools  \
  285.                   dialog mingetty vim-nox wget jfsutils \
  286.                   parted gpm laptop-detect debootstrap less  \
  287.                   initramfs-tools live-boot locales \
  288.                   debconf-utils #network-manager
  289.  
  290. apt-get -y install $EXTRAS
  291.  
  292. apt-get -y clean
  293.  
  294. #Force a black listing of the old pc-speaker driver
  295. echo blacklist snd-pcsp >> /etc/modprobe.d/blacklist.conf
  296.  
  297.  
  298. echo '%admin ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
  299.  
  300. sed 's|\([0-9]\):.*getty.*|\1:23:respawn:/sbin/mingetty --autologin root --noclear tty\1|' /etc/inittab > /tmp/inittab
  301. mv /tmp/inittab /etc/inittab
  302.  
  303. chown root:root /root/.bashrc
  304.  
  305. " | tee $CHROOT/chroot-install > /dev/null ; then
  306.     die "Could not create the script that manages the chroot install."
  307. fi
  308.  
  309. if ! chmod u+x $CHROOT/chroot-install ; then
  310.     die "Could not make the chroot install script executable."
  311. fi
  312.  
  313. echo 'Performing chroot install ...'
  314. if ! LANG=C chroot $CHROOT /chroot-install ; then
  315.     die "Chroot install failed!"
  316. fi
  317.  
  318. #udev is attached to *this* machine's network card now.
  319. #Get rid of this rule to make udev think it is a brand new install
  320. if ! rm /media/chroot/etc/udev/rules.d/70-persistent-net.rules ; then
  321.     echo "WARNING: Could not fix network card udev rules"
  322. fi
  323.  
  324. #By default the interfaces file has allow-hotplug instead of
  325. #the auto keyword. The allow-hotplug won't allow the network
  326. #card to start. We need the interfaces file to be modified
  327. #before ifup is executed. So we will rewrite part of the
  328. #networking startup script to fix the interfaces file
  329. #just before ifup is executed.
  330. cat $CHROOT/etc/init.d/networking | awk "
  331. /ifup -a/ {
  332.    print \"\tsed 's/allow-hotplug/auto/' /etc/network/interfaces > /tmp/interfaces\" ;
  333.    print \"\tmv /tmp/interfaces  /etc/network/interfaces\"
  334. }
  335.  
  336. {print \$0}" > /tmp/networking
  337. mv /tmp/networking $CHROOT/etc/init.d/networking
  338. chmod 755 $CHROOT/etc/init.d/networking
  339.  
  340. if ! echo 'live/*
  341. proc/*
  342. sys/*
  343. tmp/*
  344. boot/*
  345. lost+found/*
  346. media/*
  347. mnt/*
  348. srv/*
  349. var/lock/*
  350. var/log/*
  351. var/tmp/*
  352. var/run/*
  353. var/lib/dhcp?/*
  354. var/lib/urandom/*
  355. var/cache/apt/archives/*.deb
  356. var/lib/dpkg/*-old
  357. var/cache/apt/archives/partial/*.deb
  358. etc/mtab
  359. chroot-install
  360. ' | tee $CHROOT/tmp/exclude > /dev/null ; then
  361.     die 'Could not create the squashfs exclusion file'
  362. fi
  363.  
  364. echo 'Discovering what files to zero out...'
  365. if ! (find $CHROOT/usr/share/doc -type f -size +1b > /tmp/commit_zerosize   &&
  366.       find $CHROOT/usr/share/man -type f -size +1b >> /tmp/commit_zerosize  &&
  367.       find $CHROOT/usr/share/info -type f -size +1b >> /tmp/commit_zerosize ) ; then
  368.     die "Could not find /usr/shar/doc files to zero out"
  369. fi
  370.  
  371. echo 'Making the man, doc, and info files have zero size ...'
  372. NFILES=`wc -l /tmp/commit_zerosize | awk '{ print $1 }'`
  373. i=1
  374. cat /tmp/commit_zerosize | while read file ; do
  375.     echo -n -e "\tZeroing $i/$NFILES files\r"
  376.     rm $file
  377.     touch $file
  378.     i=$[i + 1]
  379. done ; echo
  380.  
  381. echo 'Unmounting runtime filesystems'
  382. unmount $CHROOT/proc
  383. unmount $CHROOT/dev/pts
  384. unmount $CHROOT/dev/shm
  385. unmount $CHROOT/dev
  386. unmount $CHROOT/sys
  387. mount -v --bind ${MNT} $CHROOT${MNT}
  388.  
  389. echo 'Creating the file system file.'
  390. if ! chroot $CHROOT /usr/bin/mksquashfs / $MNT/live/filesystem.squashfs -noappend -always-use-fragments -wildcards -ef /tmp/exclude -processors 1; then
  391.     die "Failed to squash the file system."
  392. fi
  393.  
  394. echo "Rebinding the needed directories under $CHROOT ..."
  395. if ! (mount -v --bind /proc $CHROOT/proc       &&
  396.       mount -v --bind /sys $CHROOT/sys         &&
  397.       mount -v --bind /dev $CHROOT/dev         &&
  398.       mount -v --bind /dev/pts $CHROOT/dev/pts &&
  399.       mount -v --bind /dev/shm $CHROOT/dev/shm ) ; then
  400.     die 'A mount failed!'
  401. fi
  402.  
  403. ###############################################################################
  404. #
  405. # Install Extlinux
  406. #
  407. ###############################################################################
  408.  
  409. if ! mkdir $MNT/extlinux; then
  410.     echo 'Could not make extlinux directory'
  411. fi
  412.  
  413. pushd $MNT >/dev/null
  414. cat > $MNT/extlinux/extlinux.conf <<EOF
  415. prompt 0
  416. timeout 1
  417. default Debian
  418.  
  419. label Debian
  420.   kernel /$(echo boot/vmlinuz*)
  421.   append initrd=/$(echo boot/initrd.img*) -- boot=live quiet splash toram=filesystem.squashfs acpi=off vga=0x305
  422. EOF
  423. popd > /dev/null
  424.  
  425. if ! which extlinux > /dev/null ; then
  426.     echo 'Installing Extlinux'
  427.     apt-get -y install extlinux
  428. fi
  429.  
  430. if ! extlinux --install $MNT/extlinux 2> /dev/null ; then
  431.     die 'Could not install extlinux'
  432. fi
  433.  
  434. umount $MNT
  435. if ! dd if=/usr/lib/syslinux/mbr.bin of=$DEV 2> /dev/null; then
  436.     die 'Could not install mbr'
  437. fi
  438.  
  439. echo 'Cleanly unmounting everything before finishing'
  440. unmount_all
  441.  
  442. echo
  443. echo 'Installation Finished Successfully !!!'
  444. echo
clone this paste RAW Paste Data