Skip to content

Instantly share code, notes, and snippets.

@akatch
Created October 24, 2014 14:22
Show Gist options
  • Save akatch/3669d3044b7d04677704 to your computer and use it in GitHub Desktop.
Save akatch/3669d3044b7d04677704 to your computer and use it in GitHub Desktop.
Bash script for slipstreaming preseed file into Debian ISO
#!/bin/bash
# Usage:
# Download Debian ISO and set RAW_DEBIAN_ISO to the absolute path of the ISO location
# Create a Debian preseed file (the attached preseed file works but needs your root publickey specified in the last line) and set PRESEED_FILE to the absolute path of the preseed file location
# Run this script as root to slipstream the preseed file into the ISO
RAW_DEBIAN_ISO='~/debian-7.6.0-amd64-netinst.iso'
WORKDIR='~/DEBIAN_ISO_WORKDIR'
PRESEED_FILE='~/preseed.cfg'
PRESEED_ISO='~/debian-7.6.0-amd64-PRESEED.iso'
# Scrub workdir
rm -rf $WORKDIR
# Mount ISO
mkdir -p $WORKDIR/loopdir
mount -o loop $RAW_DEBIAN_ISO $WORKDIR/loopdir/
if ( ! ( mount | grep 'loopdir' ) ); then
echo "Couldn't mount iso to loopdir";
exit 1;
fi;
# Copy image
mkdir -p $WORKDIR/isodir
rsync -a -H --exclude=TRANS.TBL $WORKDIR/loopdir/ $WORKDIR/isodir
umount $WORKDIR/loopdir
# Hax the initrd
mkdir -p $WORKDIR/irmod
cd $WORKDIR/irmod
gzip -d < $WORKDIR/isodir/install.amd/initrd.gz | cpio --extract --make-directories --no-absolute-filenames
cp $PRESEED_FILE $WORKDIR/irmod/preseed.cfg
find . | cpio -H newc --create | gzip -9 > $WORKDIR/isodir/install.amd/initrd.gz
# Fix md5sum
md5sum `find $WORKDIR/isodir/ -follow -type f` > $WORKDIR/isodir/md5sum.txt
# Create ISO
# IMPORTANT: the arguments passed to the -b and -c flags should be RELATIVE paths to the last argument (which should be an absolute path)
genisoimage -o $PRESEED_ISO -r -J -no-emul-boot -boot-load-size 4 -boot-info-table -input-charset utf-8 -b isolinux/isolinux.bin -c isolinux/boot.cat $WORKDIR/isodir/
exit 0
#### Contents of the preconfiguration file (for wheezy)
# Including keyboards
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/layout select USA
d-i keyboard-configuration/variant select USA
d-i keyboard-configuration/modelcode string pc105
d-i localechooser/supported-locales en_US.UTF-8
### Localization
d-i debian-installer/language string en
d-i debian-installer/locale string en_US
d-i localechooser/preferred-locale string en_US.UTF-8
d-i keymap select us
### Network
d-i netcfg/choose_interface select auto
### Hostname
d-i netcfg/get_hostname string unassigned-hostname
d-i netcfg/get_domain string unassigned-domain
# Disable that annoying WEP key dialog.
d-i netcfg/wireless_wep string
### Mirror settings
d-i mirror/country string manual
d-i mirror/http/hostname string http.us.debian.org
d-i mirror/http/directory string /debian
d-i mirror/http/proxy string
### Account setup
d-i passwd/root-login boolean true
d-i passwd/make-user boolean false
d-i passwd/root-password-crypted password a71e568a11d2f137ea65913979a94644
d-i user-setup/allow-password-weak boolean true
### Clock and time zone setup
# Controls whether or not the hardware clock is set to UTC.
d-i clock-setup/utc boolean false
# You may set this to any valid setting for $TZ; see the contents of
# /usr/share/zoneinfo/ for valid values.
d-i time/zone string America/Chicago
### Apt setup
d-i apt-setup/non-free boolean true
d-i apt-setup/contrib boolean true
# Controls whether to use NTP to set the clock during the install
d-i clock-setup/ntp boolean true
### Partitioning
d-i partman-auto/disk string /dev/sda
d-i partman-auto/method string regular
#TODO: remove lvm volumes
# - atomic: all files in one partition
# - home: separate /home partition
# - multi: separate /home, /usr, /var, and /tmp partitions
#d-i partman-auto/choose_recipe select multi
### Magical recipe
#partman-auto/text/server_scheme ::
d-i partman-auto/expert_recipe string \
boot-root :: \
4096 4146 -1 ext4 \
$primary{ } \
$bootable{ } \
method{ format } \
format{ } \
use_filesystem{ } \
filesystem{ ext4 } \
mountpoint{ / } . \
\
512 522 -1 linux-swap \
method{ swap } \
format{ } . \
\
3000 3030 -1 ext4 \
method{ format } \
format{ } \
use_filesystem{ } \
filesystem{ ext4 } \
mountpoint{ /var } . \
\
2048 2058 -1 ext4 \
method{ format } \
format{ } \
use_filesystem{ } \
filesystem{ ext4 } \
mountpoint{ /tmp } .
### End magical recipe
# This makes partman automatically partition without confirmation, provided
# that you told it what to do using one of the methods above.
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
# Disable popularity-contest
popularity-contest popularity-contest/participate boolean false
#TODO: software selection
tasksel tasksel/first multiselect standard
### Package selection
d-i pkgsel/include string openssh-server
d-i pkgsel/upgrade select safe-upgrade
### Grub
d-i grub-installer/only_debian boolean true
### Finishing up the installation
d-i finish-install/reboot_in_progress note
### Preseed root ssh key so we can configure with Ansible
### REPLACE $PUBLICKEYFILE WITH THE CONTENTS OF YOUR ROOT PUBLICKEY (or whatever user you plan to perform configuration with)
d-i preseed/late_command string mkdir -p /target/root/.ssh && echo "$PUBLICKEYFILE" >> /target/root/.ssh/authorized_keys && chmod -R 600 /target/root/.ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment