aa671a425130ca2a4c3fba4b8f54eb88dae29b2a
[automated-distro-installer] / debian-preseed
1 #!/bin/bash
2
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
5
6 usage() {
7 cat <<EOF
8 Usage: ${0##*/} OPTIONS
9
10 Given a tftproot, setup a preseed in it.
11
12 -c Disable ssh network console. ssh user = installer. pw = test.
13 -d Do debian ubuntu 14.04, default is jessie.
14 -g GRUB_DISK Default is sda. Not used in interactive partitioning.
15 -h|--help Print this help
16 -i TFTP_IP Ip of tftp server. this is required.
17 -p Stop for interactive partitioning.
18 -t DIR Tftp root. Default is current dir.
19 -u USER Username for the os install. Default is ${SUDO_USER:-$USER}
20
21 EOF
22 exit $1
23 }
24
25 interactive_partition=false
26 user=${SUDO_USER:-$USER}
27 distro=debian-jessie
28 net_console=false
29 grub_disk=sda
30 while [[ $1 == -* ]]; do
31 case $1 in
32 -c) net_console=false; shift ;;
33 -d) distro=ubuntu-14.04; shift ;;
34 -g) grub_disk=$2; shift 2 ;;
35 -i) ip=$2; shift 2 ;;
36 -p) interactive_partition=true; shift ;;
37 -t) cd $2; shift 2;;
38 -u) user=$2; shift 2;;
39 --) shift; break ;;
40 -*|-h|--help) usage ;;
41 esac
42 done
43
44
45 shopt -s extglob
46 rm -rf !(netboot.tar.gz)
47 preseed=example-preseed.txt
48 neboot_path=main/installer-amd64/current/images/netboot/netboot.tar.gz
49 case $distro in
50 ubuntu-14.04)
51 wget -q https://help.ubuntu.com/lts/installation-guide/$preseed
52 wget -qN http://archive.ubuntu.com/ubuntu/dists/trusty/$neboot_path
53 sed -ri --follow-symlinks 's!^tasksel tasksel/first multiselect .*!#\0!' $preseed
54 echo 'tasksel tasksel/first multiselect ubuntu-server, openssh-server' >>$preseed
55 ;;
56 debian-jessie)
57 wget -q https://www.debian.org/releases/jessie/$preseed
58 wget -qN http://ftp.nl.debian.org/debian/dists/jessie/$neboot_path
59 cat >>$preseed <<'EOF'
60 tasksel tasksel/first multiselect ssh-server
61 EOF
62 if ! $interactive_partition; then
63 cat >>$preseed <<EOF
64 d-i grub-installer/bootdev string /dev/$grub_disk
65 EOF
66 fi
67 ;;
68 esac
69 tar xzf netboot.tar.gz
70
71
72 # if you set priority=critical, you can avoid a few of these questions. but
73 # then you need to set the hostname in dhcp options
74 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=755848
75 # questions you can avoid setting in boot parameters:
76 # console-setup/ask_detect=false keyboard-configuration/layoutcode=us
77 # hostname=$hostname
78 # also, it asks about partition size. I don't know the preseeded answer,
79 # as it just says "2.0 TB" in get-selections. I would need to figure out
80 # how to accept the default.
81 #
82 # you can also see what got configured on a system with this command:
83 # sudo apt-get install debconf-utils
84 # debconf-get-selections --installer
85 #
86
87 # keymap=us is only needed for debian.
88 pxe_cfg=${distro%-*}-installer/amd64/boot-screens/txt.cfg
89 sed="sed -ri --follow-symlinks"
90 $sed "s#^[[:space:]]*append[[:space:]]#\0auto priority=critical locale=en_US.UTF-8 netcfg/choose_interface=auto url=tftp://$ip/example-preseed.txt keymap=us#" $pxe_cfg
91 # various google results say timeout x will result in doing the default thing,
92 # but that doesn't happen. no idea why. Maybe it needed to be part of the label.
93 echo 'totaltimeout 1' | tee -a $pxe_cfg
94
95 if $interactive_partition; then
96 $sed 's/^d-i[[:space:]]partman.*/#\0/' $preseed
97 # at least in ubuntu, this does automatic selection of boot device,
98 # and on a server where we setup raid, it choose sda, and failed
99 # and the whole installation could not be salvaged.
100 $sed 's/^d-i[[:space:]]grub-installer.*/#\0/' $preseed
101 fi
102
103 $sed "s#(^d-i time/zone string US/).*#\1Pacific#" $preseed
104 $sed '/^xserver-xorg/,/[^\\$]/ s/.*/#\0/' $preseed
105 # we set the locale in kernel args. maybe we don't need to. this overrides it.
106 $sed 's!^d-i[[:space:]]debian-installer/locale[[:space:]].*!#\0!' $preseed
107
108 # for secure pass, set the shadow option with mkpasswd -s -m sha-512 < passfile
109
110 # the example config says this option shoudl work, but it doesn't. tried it with http too,
111 # and tried naming it authorized_keys.
112 #d-i network-console/authorized_keys_url tftp://tftp@10.0.0.107/id_rsa.pub
113
114 if $net_console; then
115 cat >> $preseed <<EOF
116 d-i anna/choose_modules string network-console
117 # this doesn't work. todo: ask debian about it
118 #d-i network-console/authorized_keys_url http://10.0.0.2/authorized_keys
119 d-i network-console/password password test
120 d-i network-console/password-again password test
121 EOF
122 fi
123
124 cat >> $preseed <<EOF
125 d-i hw-detect/load_firmware boolean true
126 d-i partman/default_filesystem string ext4
127 d-i passwd/user-fullname string $user
128 d-i passwd/username string $user
129 # cleartext password for testing.
130 d-i passwd/user-password password $user
131 d-i passwd/user-password-again password $user
132 d-i passwd/root-password password $user
133 d-i passwd/root-password-again password $user
134 d-i pkgsel/update-policy select unattended-upgrades
135 d-i preseed/late_command string \
136 in-target sed -i 's/^%sudo.*$/%sudo ALL=(ALL:ALL) NOPASSWD: ALL/g' /etc/sudoers; \
137 in-target mkdir -p /home/$user/.ssh; \
138 in-target /bin/sh -c "echo '$(cat ~/.ssh/id_rsa.pub)' >> /home/$user/.ssh/authorized_keys"; \
139 in-target chown -R $user:$user /home/$user; \
140 in-target chmod -R go-rwx /home/$user/.ssh/authorized_keys; \
141 in-target cp -r /home/$user/.ssh /root; \
142 in-target usermod -a -G sudo $user;
143 EOF