home · contact · privacy
Refactor and differentiate between test and production server.
[config] / jessie_postinstall.sh
1 #!/bin/sh
2 set -x
3 set -e
4
5 if [ ! "$1" = "thinkpad" ] && [ ! "$1" = "server" ]; then
6     echo "Need argument."
7     false
8 fi
9
10 if [ "$1" = "thinkpad" ] && [ ! "$2" = "X200s" ] && [ ! "$2" = "T450s" ]; then
11     echo "Need Thinkpad type."
12     false
13 fi
14
15 if [ "$1" = "server" ] && [ ! "$2" = "plomlompom.com" ] && \
16         [ ! "$2" = "test.plomlompom.com" ]; then
17     echo "Need server type."
18     false
19 fi
20
21 # Some important variables
22 if [ "$2" = "plomlompom.com" ]; then
23     hostname="plomlompom"
24 elif [ "$2" = "test.plomlompom" ]; then
25     hostname="test.plomlompom.com"
26 elif [ "$2" = "X200s" ]; then
27     hostname="X220s"
28 elif [ "$2" = "T450s" ]; then
29     hostname="T450s"
30 fi
31
32 if [ "$1" = "server" ]; then
33     # Set root pw.
34     passwd
35 fi
36
37 # Post-installation reduction.
38 dpkg-query -Wf '${Package} ${Priority}\n' | grep ' required' | sed \
39     's/ required//' > list_white_unsorted 
40 echo 'ifupdown' >> list_white_unsorted 
41 echo 'isc-dhcp-client' >> list_white_unsorted
42 sort list_white_unsorted > list_white
43 dpkg-query -Wf '${Package}\n' > list_all_packages
44 sort list_all_packages > foo
45 mv foo list_all_packages
46 comm -3 list_all_packages list_white > list_black
47 apt-mark auto `cat list_black`
48 echo 'APT::AutoRemove::RecommendsImportant "false";' > /etc/apt/apt.conf.d/99mindeps
49 echo 'APT::AutoRemove::SuggestsImportant "false";' >> /etc/apt/apt.conf.d/99mindeps 
50 DEBIAN_FRONTEND=noninteractive apt-get -y --purge autoremove
51 rm list_all_packages list_white_unsorted list_white list_black 
52 echo 'APT::Install-Recommends "false";' >> /etc/apt/apt.conf.d/99mindeps
53 echo 'APT::Install-Suggests "false";' >> /etc/apt/apt.conf.d/99mindeps
54
55 # Set hostname and FQDN.
56 echo $hostname > /etc/hostname
57 hostname $hostname
58 if [ "$1" = "server" ]; then
59     echo '127.0.0.1 localhost' > /etc/hosts
60     ip=`hostname -I`
61     echo "$ip $2 $hostname" >> /etc/hosts
62
63     # Call dhclient on startup.
64     cat > /etc/systemd/system/dhclient.service << EOF
65 [Unit]
66 Description=Ethernet connection
67
68 [Service]
69 ExecStart=/sbin/dhclient eth0
70
71 [Install]
72 WantedBy=multi-user.target
73 EOF
74     systemctl enable /etc/systemd/system/dhclient.service
75 fi
76
77 # Package management config, system upgrade.
78 echo 'deb http://ftp.debian.org/debian/ jessie main contrib non-free' \
79     > /etc/apt/sources.list
80 echo 'deb http://security.debian.org/ jessie/updates main contrib non-free' \
81     >> /etc/apt/sources.list
82 echo 'deb http://ftp.debian.org/debian/ jessie-updates main contrib non-free' \
83     >> /etc/apt/sources.list
84 if [ "$1" = "thinkpad" ]; then
85     echo 'deb http://ftp.debian.org/debian/ jessie-backports main contrib' \
86 ' non-free' >> /etc/apt/sources.list
87     echo 'deb http://ftp.debian.org/debian/ testing main contrib non-free' \
88         >> /etc/apt/sources.list
89     echo 'deb http://security.debian.org/ testing/updates main contrib' \
90 ' non-free' >> /etc/apt/sources.list
91     echo 'deb http://ftp.debian.org/debian/ testing-updates main contrib' \
92 ' non-free' >> /etc/apt/sources.list
93     echo 'APT::Default-Release "stable";' \
94         >> /etc/apt/apt.conf.d/99defaultrelease
95 fi
96 dhclient eth0
97 apt-get update
98 apt-get -y dist-upgrade
99
100 # Set up manuals.
101 apt-get -y install man-db manpages less
102
103 if [ "$1" = "thinkpad" ]; then
104     # Power management as per <http://thinkwiki.de/TLP_-_Linux_Stromsparen>.
105     echo '' >> /etc/apt/sources.list
106     echo 'deb http://repo.linrunner.de/debian jessie main' \
107         >> /etc/apt/sources.list
108     apt-key adv --keyserver pool.sks-keyservers.net --recv-keys CD4E8809
109     apt-get update
110     apt-get -y install linux-headers-amd64 tlp tp-smapi-dkms
111     sed -i 's/^#START_CHARGE_THRESH_BAT0/START_CHARGE_THRESH_BAT0=10 '\
112 '#START_CHARGE_THRESH_BAT0/' /etc/default/tlp
113     sed -i 's/^#STOP_CHARGE_THRESH_BAT0/STOP_CHARGE_THRESH_BAT0=95 '\
114 '#STOP_CHARGE_THRESH_BAT0/' /etc/default/tlp
115     sed -i 's/^#DEVICES_TO_DISABLE_ON_STARTUP/DEVICES_TO_DISABLE_ON_STARTUP='\
116 '"bluetooth wifi wwan" #DEVICES_TO_DISABLE_ON_STARTUP/' /etc/default/tlp
117     tlp start
118 fi
119
120 # Don't clear boot messages on start up.
121 sed -i 's/^TTYVTDisallocate=yes$/TTYVTDisallocate=no/g' \
122     /etc/systemd/system/getty.target.wants/getty\@tty1.service
123
124 # Set up timezone.
125 echo 'Europe/Berlin' > /etc/timezone
126 cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime
127
128 # Locale config.
129 apt-get -y install locales
130 echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen
131 locale-gen
132
133 if [ "$1" = "thinkpad" ]; then
134     # Console config.
135     DEBIAN_FRONTEND=nointeractive apt-get -y install console-setup
136     echo 'ACTIVE_CONSOLES="/dev/tty[1-6]"' > /etc/default/console-setup
137     echo 'CHARMAP="UTF-8"' >> /etc/default/console-setup
138     echo 'CODESET="Lat15"' >> /etc/default/console-setup
139     echo 'FONTFACE="TerminusBold"' >> /etc/default/console-setup
140     echo 'FONTSIZE="8x16"' >> /etc/default/console-setup
141     echo 'export LC_ALL="en_US.UTF-8"' >> /etc/profile
142     sed -i 's/^XKBLAYOUT/XKBLAYOUT="de" # XKBLAYOUT/g' /etc/default/keyboard
143     service keyboard-setup restart
144 fi
145
146 # Clone git repository.
147 apt-get -y install ca-certificates
148 apt-get -y install git
149 git clone http://github.com/plomlompom/config
150 config/bin/symlink.sh
151
152 # Add user. Remove old user's config/ if it exists.
153 useradd -m -s /bin/bash plom
154 rm -rf /home/plom/config
155 su - plom -c 'git clone http://github.com/plomlompom/config /home/plom/config'
156 su plom -c '/home/plom/config/bin/symlink.sh '$1' '$2
157
158 # Set up editor.
159 mkdir -p .vimbackups
160 su plom -c 'mkdir -p /home/plom/.vimbackups/'
161 apt-get -y install vim
162
163 if [ "$1" = "server" ]; then
164     # Set up ssh-guard.
165     apt-get -y install sshguard rsyslog
166
167     # Set up openssh-server.
168     apt-get -y install openssh-server
169
170     # Set up mail system.
171     su plom -c 'mkdir -p /home/plom/mail/'
172     su plom -c 'mkdir -p /home/plom/mail/inbox/{cur,new,tmp}'
173     su plom -c 'mkdir -p /home/plom/mail/new_inbox/{cur,new,tmp}'
174     sed -i 's/^delete = true$/delete = false/g' \
175         /home/plom/config/dotfiles/user/server/getmail/getmailrc
176     DEBIAN_FRONTEND=noninteractive apt-get -y install getmail4 procmail mutt \
177         postfix maildrop
178     cp config/systemfiles/main.cf /etc/postfix/main.cf
179     cp config/systemfiles/aliases /etc/aliases
180     newaliases
181     service postfix restart
182
183     # Set up regular system update reminder.
184     apt-get -y install cron
185     su plom -c "echo '0 18 * * 0 ~/config/bin/simplemail.sh '\
186         '~/config/mails/update_reminder' | crontab -"
187
188     # Set up screen/weechat/OTR/bitlbee. Make bitlbee listen only locally.
189     apt-get -y install screen weechat-plugins python-potr bitlbee
190     sed -i 's/^# DaemonInterface/DaemonInterface = 127.0.0.1 '\
191 '# DaemonInterface/' /etc/bitlbee/bitlbee.conf
192     sedtest=`grep -E '^DaemonInterface = 127.0.0.1 #' \
193         /etc/bitlbee/bitlbee.conf | wc -l | cut -d ' ' -f 1`
194     if [ 0 -eq $sedtest ]; then
195         false
196     fi
197     cp config/systemfiles/weechat.service  /etc/systemd/system/weechat.service
198     systemctl enable /etc/systemd/system/weechat.service
199
200     # Send instructions mail.
201     config/bin/simplemail.sh config/mails/server_postinstall_finished
202
203 elif [ "$1" = "thinkpad" ]; then
204     # Set up networking (wifi!).
205     apt-get -y install firmware-iwlwifi
206     DEBIAN_FRONTEND=noninteractive apt-get -y install wicd-curses
207     sed -i 's/^wired_interface = .*$/wired_interface = eth0/g' \
208         /etc/wicd/manager-settings.conf
209     sed -i 's/^wireless_interface = .*$/wireless_interface = wlan0/g' \
210         /etc/wicd/manager-settings.conf
211     systemctl restart wicd
212
213     # Set up hibernation on lid close.
214     echo 'HandleLidSwitch=hibernate' >> /etc/systemd/logind.conf
215
216     # Set up sound.
217     usermod -aG audio plom
218     apt-get -y install alsa-utils
219     if [ "$2" = "X200s" ]; then
220         amixer -c 0 sset Master playback 100% unmute
221     elif [ "$2" = "T450s" ]; then
222         amixer -c 1 sset Master playback 100% unmute
223         # Re-order souncards so the commonly used one is the first one.
224         echo 'options snd_hda_intel index=1,0' >> /etc/modprobe.d/sound.conf
225     fi
226
227     # Set up window system, i3, redshift.
228     apt-get -y install xserver-xorg xinit xterm i3 i3status dmenu redshift
229
230     # Set up OpenGL and hardware acceleration.
231     if [ "$2" = "X200s" ]; then
232         apt-get -y install i965-va-driver
233     elif [ "$2" = "T450s" ]; then
234         apt-get -y -t jessie-backports install xserver-xorg-video-intel
235     fi
236     apt-get -y install libgl1-mesa-dri
237     usermod -aG video plom
238
239     # Install xrandr.
240     apt-get -y install x11-xserver-utils
241
242     # Set up pentadactyl. 
243     apt-get -y install iceweasel
244     apt-get -y -t jessie-backports install xul-ext-pentadactyl
245     apt-get -y install vim-gtk
246     su plom -c 'mkdir -p /home/plom/downloads/'
247
248     # Set up openssh-client.
249     apt-get -y install openssh-client
250 fi
251
252 # Set password for user.
253 passwd plom
254
255 # Clean up.
256 rm jessie_postinstall.sh
257
258 # Finalize everything with a reboot.
259 reboot