Automatic installation of Debian Squeeze from a USB flash drive

  • Sharebar

debian logo

In this post you are going to learn how to build a USB Flash Drive that contains a fully automated Debian installation system. At Wallix this installation system is used in addition to our traditional PXE/preseed system to deploy our products Wallix LogBox and Wallix AdminBastion.

This installation media will be based on:
The method described in this post is easy to deploy and maintain:
  • deploy a custom Debian system without external resources like eg. a remote repository.
  • based on standard Debian iso and debian installer images.
  • preseed file is stored on the USB flash drive filesystem.
  • additionnal packages are stored on the USB filesystem.

Prepare the USB flash drive to start the Debian Installer at boot

The first step is to prepare the USB flash drive partitions, create a filesystem and install grub2. If you don’t want to wipe your USB flash drive you can use an existing partition as long as Grub2 supports the filesystem on it.

Using fdisk, cfdisk or your favorite partition tool, create a primary partition of at least 2G, and set the bootable flag (we assumes the USB flash drive is attached to /dev/sdb)

# cfisk /dev/sdb

Create the filesystem on the first partition

# mkfs.ext2 /dev/sdb1

Then install Grub2

# mkdir /mnt/usb
# mount /dev/sdb1 /mnt/usb
# grub-install --root-directory=/mnt/usb /dev/sdb

Grub2 need to write on the MBR (Master Boot Record), the first 512 bytes of the flash drive, to copy the boot.img image. Furthermore it will create a directory that contains a set of grub modules. Later in this directory we will create the grub.cfg file.

Now we’ll use the hd-media method of the Debian Installer. This method will look for an iso of a valid Debian installation media. So let’s go

# mkdir /mnt/usb/hdmedia-squeeze
# wget http://ftp.debian.org/debian/dists/squeeze/main/installer-amd64/current/images/hd-media/vmlinuz -O /mnt/usb/hdmedia-squeeze/vmlinuz
# wget http://ftp.debian.org/debian/dists/squeeze/main/installer-amd64/current/images/hd-media/initrd.gz -O /mnt/usb/hdmedia-squeeze/initrd.gz
# mkdir /mnt/usb/isos
# cp debian-6.0.3-amd64-CD-1.iso /mnt/usb/isos/

To finish the preparation we need to provide a configuration file for grub

# This is to define colors in the grub menu
#set color_normal='green/black'
#set color_highlight='light-green/black'
 
# Define some paths
# The / is the root of the installation media (/dev/sdb1)
set isosdir='/isos'
set hdmediasqueeze='/hdmedia-squeeze'
 
# Below is where the running debian installer will find the preseed file
# Debian installer provided with hd-media images mounts the installation media filesystem
# under /hd-media
set preseed='/hd-media/preseed'
 
# Manual entry
menuentry 'Debian 6.0 amd64 manual install' {
    linux $hdmediasqueeze/vmlinuz iso-scan/filename=$isodir/debian-6.0.3-amd64-CD-1.iso priority=critical
    initrd $hdmediasqueeze/initrd.gz
}
 
# Automated entry
menuentry 'Debian 6.0 amd64 automatic install' {
    linux $hdmediasqueeze/vmlinuz iso-scan/filename=$isodir/debian-6.0.3-amd64-CD-1.iso preseed/file=$preseed/standard-squeeze.preseed auto=true priority=critical
    initrd $hdmediasqueeze/initrd.gz
}

The Manual entry will boot a normal Debian install and the Automated entry will boot a preseed installation. The iso-scan/filename option tell the installer where to find the full Debian Installer. The priority indicates which kind of questions will be asked during the installation. The preseed option tell the installer where the preseed file is stored. The auto option will delay some installer questions later after the installer retrieved and read the preseed file.

The Manual entry should work now, to try it you can use VirtualBox or even try to boot it on a real box. To boot a VirtualBox guest from a USB Flash drive you need to link the real device /dev/sdb to a special vmdk file and attach /tmp/usb.vmdk as first SATA disk in the guest configuration panel

# VBoxManage internalcommands createrawvmdk -filename /tmp/usb.vmdk -rawdisk /dev/sdb

Preseed the Debian installation

Assuming you have succeed booting the Debian installer with the manual entry we can now provide a preseed file.
With this preseed file, we’ll set responses for the debconf database. The debconf database is used during the Debian installer and asks the user for installation details. Using the preseed file we load pre-defined responses for the installer then the installer will not prompt the user.

The preseed file below contains the minimal preseed responses for installing a Debian Squeeze. For complete details about the preseed file and the whole preseed system have a look to complete documentation.

Create a preseed directory at the root of the USB Flash disk and copy the preseed file

# mkdir /mnt/usb/preseed
# cat << EOF > /mnt/usb/preseed/standard-squeeze.preseed
d-i debian-installer/locale string en_US
d-i console-tools/archs select skip-config
d-i time/zone string US/Eastern
 
# We don't need network as all stuff is contained in the USB flash drive
d-i netcfg/enable boolean false
d-i netcfg/wireless_wep string
 
# Don't prompt for firmware
d-i hw-detect/load_firmware boolean true
 
# We only create a root user
d-i passwd/make-user boolean false
d-i passwd/root-password password rootpasswd
d-i passwd/root-password-again password rootpasswd
 
# We assume the target computer has only one harddrive.
# In most case the USB Flash drive is attached on /dev/sda
# but sometimes the harddrive is detected before the USB flash drive and
# it is attached on /dev/sda and the USB flash drive on /dev/sdb
# You can set manually partman-auto/disk and grub-installer/bootdev or
# used the early_command option to automatically set the device to use.
d-i partman/early_command string \
   USBDEV=$(mount | grep hd-media | cut -d&quot; &quot; -f1 | sed &quot;s/\(.*\)./\1/&quot;);\
   BOOTDEV=$(list-devices disk | grep -v $USBDEV | head -1);\
   debconf-set partman-auto/disk $BOOTDEV;\
   debconf-set grub-installer/bootdev $BOOTDEV;
#d-i partman-auto/disk string /dev/sdb$
#d-i grub-installer/bootdev  string /dev/sdb
d-i grub-installer/only_debian boolean false
d-i grub-installer/with_other_os boolean false
 
# Here we set the partition layout using a predefined recipe (atomic)
# Refer to preseed documentation to create custom recipes
d-i partman-auto/method string regular
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
# Uncomment the line below to preseed the disk layout confirmation
#d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/mount_style select uuid
 
# We don't want use a remote mirror (all stuff we need is on the USB flash drive)
d-i base-installer/install-recommends boolean false
d-i apt-setup/non-free boolean false
d-i apt-setup/contrib boolean false
d-i apt-setup/use_mirror boolean false
# We will use a local repo for our packages (this repo has not been signed)
d-i debian-installer/allow_unauthenticated boolean true
 
# Install a standard debian system (some rocommended packages) + openssh-server
tasksel tasksel/first multiselect standard
d-i pkgsel/include string openssh-server
d-i pkgsel/upgrade select none
popularity-contest popularity-contest/participate boolean false
 
d-i grub-installer/only_debian boolean false
d-i grub-installer/with_other_os boolean false
 
# Avoid that last message about the install being complete
#d-i finish-install/reboot_in_progress note
 
EOF

When you have copied the preseed file on the key you can validate the automated entry. Be sure to uncomment "d-i partman/confirm boolean true" once you have validated that "partman/early_command" has chosen the expected device to install the target system and the bootloader.

Automatically install additional packages from a local repository

You may want to automatically install your own custom packages to the target system. This is pretty easy as the hd-media images mount the boot device filesystem on /hd-media during the installation. So we’ll create a local repository on the USB flash drive and via the preseed file tell the installer to deploy our packages on the target system.

To create the local repository

# mkdir /mnt/usb/repo
# cp mytool_1.0.0-1_amd64.deb /mnt/usb/repo
# cd /mnt/usb
# dpkg-scanpackages repo/ /dev/null | gzip > repo/Packages.gz

Now we have one package in our repository we’ll use the late_command option to bind the repo directory on the target system and then install the mytool package. Add the following line to the preseed file:

d-i preseed/late_command string \
    mkdir /target/tmp/repo;\
    mount -o bind /hd-media/repo /target/tmp/repo;\
    echo "deb file:///tmp/ repo/" >> /target/etc/apt/sources.list;\
    in-target aptitude update;\
    in-target aptitude install mytool;\
    echo "deb http://ftp.debian.org/debian squeeze main" > /target/etc/apt/sources.list;

Article contributed by Fabien Boucher, Technical leader at Wallix.

Incoming search terms:

  • debian usb install
  • debian squeeze da usb key
  • install debian from usb
  • debian usb installer
  • debian squeeze usb install
  • debian install from usb
  • debian usb
  • debian install usb
  • install debian squeeze from usb
  • debian squeeze usb
This entry was posted in linux and tagged , , , , . Bookmark the permalink.

15 comments on “Automatic installation of Debian Squeeze from a USB flash drive

  1. Pingback: Links 3/11/2011: Steam on PCLinuxOS, JavaFX | Techrights

  2. Pingback: Auto-install Squeeze from a flash drive « 0ddn1x: tricks with *nix

  3. Pingback: Automatic installation of Debian Squeeze from a USB flash drive | Debian-News.net - Your one stop for news about Debian

  4. Ram Natarajan on said:

    Thanks for the great HOWTO.
    BTW, where does the grub configuration file go?

  5. Thanks Ram, the grub.cfg file must be copied to ‘/mnt/usb/boot/grub/’ after the grub-install command. The grub-install command will create ‘boot/grub/’ directory at the root of the Flash drive filesystem for you.

  6. sentono on said:

    great and detail tutorial /how-to, i trying to find any tutorial for install debian from usb and i found your web, this is great and it’s working in my end thank youu :)

  7. Davis on said:

    where I write this?

    # This is to define colors in the grub menu
    #set color_normal=’green/black’
    #set color_highlight=’light-green/black’

    # Define some paths
    # The / is the root of the installation media (/dev/sdb1)
    set isosdir=’/isos’
    set hdmediasqueeze=’/hdmedia-squeeze’

    # Below is where the running debian installer will find the preseed file
    # Debian installer provided with hd-media images mounts the installation media filesystem
    # under /hd-media
    set preseed=’/hd-media/preseed’

    # Manual entry
    menuentry ‘Debian 6.0 amd64 manual install’ {
    linux $hdmediasqueeze/vmlinuz iso-scan/filename=$isodir/debian-6.0.3-amd64-CD-1.iso priority=critical
    initrd $hdmediasqueeze/initrd.gz
    }

    # Automated entry
    menuentry ‘Debian 6.0 amd64 automatic install’ {
    linux $hdmediasqueeze/vmlinuz iso-scan/filename=$isodir/debian-6.0.3-amd64-CD-1.iso preseed/file=$preseed/standard-squeeze.preseed auto=true priority=critical
    initrd $hdmediasqueeze/initrd.gz
    }

  8. The following command fails:

    “d-i partman/early_command string \
    USBDEV=$(mount | grep hd-media | cut -d" " -f1 | sed "s/\(.*\)./\1/");\
    BOOTDEV=$(list-devices disk | grep -v $USBDEV | head -1);\
    debconf-set partman-auto/disk $BOOTDEV;\
    debconf-set grub-installer/bootdev $BOOTDEV;

    I guess it fails because the command “list-devices” is not installed yet. According to “apt-file” it belongs to the bluez package.

    Steen

  9. The “list-devices” file referred to by bluez is “/usr/share/doc/bluez/examples/list-devices”. It is meant to report bluetooth devices. It won’t be the “list-devices” command that you have in mind.

    • I just noticed the debian-installer does indeed contain a “list-devices” command.

      Sorry about the confusion. I guess the error must have arisen because I was testing this within qemu, where I was unable to create a bootable usb device. So I did the testing with an ordinary bootable virtual harddisk, which apparently was not recognized by the “list-devices” command.

      Steen

  10. Anthony on said:

    Hi,

    Great tutorial for me, thank you very much !

    I do not succeed to install additional packages. How could what’s wrong ?

    Anthony

  11. Artur - usb stick on said:

    Thanks for info. I wonder if installing ubuntu is quite similiar cause as I know ubuntu is based on debian.

  12. Luiz on said:

    Where did “mytool_1.0.0-1_amd64.deb” come from? Thanks!

    • Fbo on said:

      Hi Luiz,
      The package mytool is not a real package i use it as example for this article.
      The example show the method to deploy home made packages in the target system. You can replace mytool by another packages.

      Fbo

  13. roofing contractor in clearwater on said:

    This is a great tip especially to those fresh to the
    blogosphere. Simple but very precise info… Appreciate your sharing this one.
    A must read article!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>