preseed是一个自动安装的工具,通过脚本配置OS的locale、mirror、rootpasswd、package和一些个性化配置。

以下是一个可正常运行的preseed,在centos6.5 + kvm + oz-install中测试通过。

### Location
d-i debian-installer/language string en
d-i debian-installer/locale string en_US.UTF-8
d-i debian-installer/country string US
d-i localechooser/supported-locales en_US.UTF-8, zh_CN.UTF-8

### keyboard setup
d-i console-setup/ask_detect boolean false
d-i console-setup/layoutcode string us
d-i debian-installer/keymap select us
d-i keymap select us

### Network configuration
#d-i netcfg/enable boolean false
d-i netcfg/choose_interface select auto

# If you have a slow dhcp server and the installer times out waiting for
# it, this might be useful.
#d-i netcfg/dhcp_timeout string 60

# If you prefer to configure the network manually, uncomment this line and
# the static network configuration below.
d-i netcfg/disable_dhcp boolean true

# If you want the preconfiguration file to work on systems both with and
# without a dhcp server, uncomment these lines and the static network
# configuration below.
#d-i netcfg/dhcp_failed note
#d-i netcfg/dhcp_options select Configure network manually

# static address config
d-i netcfg/get_nameservers string <nameserver> (1)
d-i netcfg/get_ipaddress string <ip-address> (2)
d-i netcfg/get_netmask string <netmask> (3)
d-i netcfg/get_gateway string <gateway> (4)
d-i netcfg/confirm_static boolean true
# Disable that annoying WEP key dialog
d-i netcfg/wireless_wep string

# hardware, etc.
d-i hw-detect/load_firmware boolean false
#d-i debian-install/add-kernel-opts string elevator=deadline console=tty0 console=ttyS0,115200

# hostname config
d-i netcfg/get_domain string <domain.name>
d-i netcfg/get_hostname string <hostname>
d-i netcfg/hostname string <hostname>

### Clock and time zone setup
d-i clock-setup/utc boolean true
d-i time/zone select Asia/Shanghai
d-i clock-setup/ntp boolean true

### Partitioning
d-i partman-auto/expert_recipe string           \
    root ::                                     \
        10000 20 11000 ext4                     \
          $primary{ } $bootable{ }              \
          method{ format } format{ }            \
          use_filesystem{ } filesystem{ ext4 }  \
          mountpoint{ / }                       \
        .
d-i partman-auto/method string regular
d-i partman-auto/choose_recipe select atomic
d-i partman/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
d-i partman/mount_style select traditional
d-i partman-basicfilesystems/no_swap boolean false
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true

### Account setup
d-i passwd/root-login boolean true
d-i passwd/make-user boolean false
d-i passwd/root-password password %ROOTPW%
d-i passwd/root-password-again password %ROOTPW%

### APT setup
d-i mirror/country string manual

d-i mirror/http/hostname string ftp.jaist.ac.jp (5)
d-i mirror/http/directory string /pub/Linux/debian
#d-i mirror/suite string wheezy

d-i apt-setup/non-free boolean true
d-i apt-setup/contrib boolean true
d-i apt-setup/backports boolean true
d-i apt-setup/security-updates boolean true

#d-i apt-setup/services-select multiselect security, updates
d-i apt-setup/security_host string security.debian.org
#d-i apt-setup/volatile_host string
d-i debian-installer/allow_unauthenticated boolean true

## custom repository: jaist.ac.jp
d-i apt-setup/comment string jaist.ac.jp
d-i apt-setup/local0/repository string \
    http://ftp.jaist.ac.jp/debian/ wheezy main contrib non-free
d-i apt-setup/local0/source boolean true

d-i apt-setup/comment string jaist.ac.jp - backports
d-i apt-setup/local1/repository string \
    http://ftp.jaist.ac.jp/debian/ wheezy-backports main contrib non-free
d-i apt-setup/local1/source boolean true


### Package selection
tasksel tasksel/first multiselect
#d-i pkgsel/include install-recommends boolean true
d-i pkgsel/include string openssh-server openssh-client vim wget iftop tcpdump mtr htop mosh
d-i pkgsel/upgrade select none
#popularity-contest popularity-contest/participate boolean false

### Misc options
#d-i cdrom-detect/eject boolean false
d-i finish-install/reboot_in_progress note
#d-i finish-install/keep-consoles boolean true

d-i preseed/late_command string \
  sed -i 's/^deb cdrom/# deb cdrom/g' /target/etc/apt/sources.list; \
  echo "UseDNS no" >> /target/etc/ssh/sshd_config; \
  rm /target/etc/udev/rules.d/70-persistent-*.rules; \
  ln -s /dev/null /target/etc/udev/rules.d/70-persistent-net.rules; \
  ln -s /dev/null /target/etc/udev/rules.d/70-persistent-cd.rules; \
  sed -i '/GRUB_CMDLINE_LINUX/d' /target/etc/default/grub; \
  sed -i '/GRUB_TERMINAL/d' /target/etc/default/grub; \
  echo 'GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"' >> /target/etc/default/grub; \
  echo 'GRUB_TERMINAL=serial' >> /target/etc/default/grub; \
  echo 'GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"' >> /target/etc/default/grub; \
  in-target update-grub; \
  sed -i '/#T0/d' /target/etc/inittab; \
  echo "T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100" >> /target/etc/inittab;
关于late_command

late_command是在系统安装完毕,提示用户重启之前所执行的操作,in-target的含义相当于chroot /,这时候,可以直接使用全路径。 譬如: in-target echo "UseDNS no" >> /etc/ssh/sshd_config; 否则: echo "UseDNS no" >> /target/etc/ssh/sshd_config; 前提是echo这个命令包含在debian-installer中,具体debian-installer包含了哪些命令,不得而知。

sed替换 在late_command中写sed的替换有些痛苦,特别是有-、=、/等需要转义的符号时,可读性差。所以我决定直接删除,然后再添加到文件末端。 譬如:

sed -i '/GRUB_CMDLINE_LINUX/d' /target/etc/default/grub; \
sed -i '/GRUB_TERMINAL/d' /target/etc/default/grub; \
echo 'GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"' >> /target/etc/default/grub; \
echo 'GRUB_TERMINAL=serial' >> /target/etc/default/grub; \
echo 'GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"' >> /target/etc/default/grub; \