-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·484 lines (418 loc) · 14.5 KB
/
bootstrap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/usr/bin/env bash
#
# VNX installation script for Vagrant VMs
#
# Author: David Fernández (david.fernandez@upm.es)
#
# This file is part of the Virtual Networks over LinuX (VNX) Project distribution.
# (www: http://www.dit.upm.es/vnx - e-mail: vnx@dit.upm.es)
#
# Departamento de Ingenieria de Sistemas Telematicos (DIT)
# Universidad Politecnica de Madrid
# SPAIN
#
INSTALLDIR="/install"
if [ $( which apt-fast ) ]; then
echo "-- Using apt-fast"
APT_CMD=apt-fast
else
echo "-- Using apt-get"
APT_CMD=apt-get
fi
#
# Default values
# Can be changed adding command line args
GUI=no # -g gnome|lubuntu|lubuntucore|no
HNAME=vnx # -n hostname
NEWUSER=rdor # -u username
PASSWD='xxxx' # -p password
VMLANG=en_US.UTF-8 # -l xx
ARCH=32 # -a 64|32
DIST=trusty # -d trusty|vivid
VNX=no # -v yes|no
echo $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12}
while getopts ":g:u:p:n:l:a:d:v:" opt; do
case "$opt" in
g)
GUI="$OPTARG" ;;
u)
NEWUSER="$OPTARG" ;;
p)
PASSWD="$OPTARG" ;;
n)
HNAME="$OPTARG" ;;
l)
VMLANG="$OPTARG" ;;
a)
ARCH="$OPTARG" ;;
d)
DIST="$OPTARG" ;;
v)
VNX="$OPTARG" ;;
esac
done
echo "--"
echo "DIST=$DIST, ARCH=$ARCH, GUI=$GUI, NEWUSER=$NEWUSER, PASSWD=$PASSWD, HNAME=$HNAME, VMLANG=$VMLANG VNX=$VNX"
echo "--"
echo "--"
echo "---- Mount shared dir in /install:"
echo "--"
# Check if vboxsf kernel module is loaded/loadable
modprobe vboxsf
if [ $? -ne 0 ]; then
echo "--"
echo "---- ERROR: Cannot load vboxsf module to mount install directory. Aborting provision."
echo "--"
exit 1
fi
# mount the install directory
mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` install $INSTALLDIR
if [ $? -ne 0 ]; then
echo "--"
echo "---- ERROR: Cannot mount /install directory. Aborting provision."
echo "--"
exit 1
fi
# Check that we can access the content of install directory
ls -l $INSTALLDIR/Vagrantfile
if [ $? -ne 0 ]; then
echo "--"
echo "---- ERROR: Cannot access $INSTALLDIR directory content. Aborting provision."
echo "--"
exit 1
else
echo "---- $INSTALLDIR content:"
ls -al $INSTALLDIR
echo "----"
fi
# Add the mount to /etc/fstab
# We use the noauto option to avoid problems at startup. If the VM is started with the fstab entry
# (without the noauto option) and the shared directory is not configured in VirtualBox, the virtual
# machine does not start correctly, giving a "Welcome to emergency mode" message.
echo "install /install vboxsf uid=vagrant,gid=vagrant,noauto 0 0" >> /etc/fstab
# Add vboxsf module to /etc/modules
echo "vboxsf" >> /etc/modules
echo "--"
echo "---- Changing hostname:"
echo "--"
HNAME=$( echo $HNAME | sed -e 's/_//g' ) # Eliminate "_" from the hostname
echo $HNAME > /etc/hostname
hostname $HNAME
sed -i -e '/^127.0.1.1/d' /etc/hosts
sed -i -e "2i127.0.1.1\t$HNAME" /etc/hosts
#sed -i -e "s/127.0.1.1.*/127.0.1.1 $HNAME/" /etc/hosts
echo "--"
echo "-- Upgrading the system to latest packages versions and update locales:"
echo "--"
echo "--"
echo "-- Updating package list:"
echo "--"
export DEBIAN_FRONTEND=noninteractive
$APT_CMD update
LANG=${VMLANG}_${VMLANG^^}.UTF-8
echo "--"
echo "-- Setting locale to $LANG:"
echo "--"
$APT_CMD -y install language-pack-$VMLANG-base language-pack-$VMLANG
rm /etc/default/locale
update-locale LANG=$LANG LC_MESSAGES=POSIX
echo "--"
echo "-- Setting keyboard:"
echo "--"
sed -i -e "/exit 0/i/bin/loadkeys $VMLANG" /etc/rc.local
sed -i -e 's/XKBMODEL=.*/XKBMODEL="pc105"/' /etc/default/keyboard
sed -i -e 's/XKBLAYOUT=.*/XKBLAYOUT="es"/' /etc/default/keyboard
echo "--"
echo "-- Setting timezone:"
echo "--"
rm /etc/localtime
ln -s /usr/share/zoneinfo/Europe/Madrid /etc/localtime
echo "--"
echo "-- Upgrading packages:"
echo "--"
$APT_CMD -y dist-upgrade
echo "--"
echo "-- Creating new user $NEWUSER:"
echo "--"
useradd -m -p "$pass" -s "/bin/bash" $NEWUSER
[ $? -eq 0 ] && echo "User $NEWUSER added to the system" || echo "Failed to add $NEWUSER"
echo "$NEWUSER:$PASSWD" | chpasswd
adduser $NEWUSER sudo
echo "$NEWUSER ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$NEWUSER
# Create ~/bin directory and add it to the PATH
mkdir -p /home/$NEWUSER/bin
sudo bash -c "echo 'PATH=\$PATH:/home/$NEWUSER/bin' >> /home/$NEWUSER/.bashrc "
#
# Installing GUI if requested
#
echo "-- GUI: $GUI"
if [ "$GUI" == "gnome" -o "$GUI" == "lubuntu" -o "$GUI" == "lubuntucore" ]; then
echo "--"
echo "-- Installing GUI: $GUI"
echo "--"
#$APT_CMD -y install --no-install-recommends lubuntu-desktop
if [ "$GUI" == "gnome" ]; then
#
# GUI configuration for GNOME
#
$APT_CMD -y --no-install-recommends install ubuntu-desktop
# Set autologin to the new created account
sed -i -e 's/.*AutomaticLoginEnable =.*/AutomaticLoginEnable = true/' -e "s/.*AutomaticLogin =.*/AutomaticLogin = $NEWUSER/" /etc/gdm3/custom.conf
# Disable screensaver (does not work)
# su $NEWUSER gsettings set org.gnome.desktop.screensaver lock-enabled false
elif [ "$GUI" == "lubuntu" -o "$GUI" == "lubuntucore" ]; then
#
# GUI configuration for LXDE (lubuntu)
#
if [ "$GUI" == "lubuntu" ]; then
$APT_CMD -y --no-install-recommends install lubuntu-desktop
elif [ "$GUI" == "lubuntucore" ]; then
$APT_CMD -y --no-install-recommends install lubuntu-core
fi
$APT_CMD -y remove network-manager
# Disable screensaver
# mkdir -p /home/$NEWUSER/.config/autostart/
# cat >> /home/$NEWUSER/.config/autostart/screensaver-settings.desktop <<EOF
#[Desktop Entry]
#Name=Salvapantallas
#Comment=Configurar los tiempos de espera del salvapantallas
#Exec=xset s 0 dpms 0 0 0 -dpms
#EOF
# cat /home/$NEWUSER/.config/autostart/screensaver-settings.desktop
chown -R $NEWUSER.$NEWUSER /home/$NEWUSER/
# xset -dpms;xset s noblank in my .xinitrc
# Set wallpaper
WALLPAPER=vnx-dit-upm-fondo-1024.png
WALLPAPERDIR=/usr/share/lubuntu/wallpapers/
cd $WALLPAPERDIR
wget http://idefix.dit.upm.es/vnx/logos/$WALLPAPER
#mv $WALLPAPER lubuntu-default-wallpaper.png
#mkdir -p /home/$NEWUSER/.config/pcmanfm/lubuntu/
#sed -i -e 's/wallpaper=.*/wallpaper=/usr/share/lubuntu/wallpapers/vnx-fondo-1200.png/' /home/$NEWUSER/.config/pcmanfm/lubuntu/desktop-items-0.conf
# Create script to config some desktop issues
CFGDESK=/usr/local/bin/config_desktop
CFGDESKFILE=/home/$NEWUSER/.config/pcmanfm/lubuntu/desktop-items-0.conf
CFGLIBFMFILE=/home/$NEWUSER/.config/libfm/libfm.conf
CFGSTORE=/home/$NEWUSER/tmp
mkdir -p /home/$NEWUSER/tmp
cp -a $INSTALLDIR/config/ $CFGSTORE
mkdir -p /home/$NEWUSER/.config/lxqt/
cp -r /home/$NEWUSER/tmp/config/* /home/$NEWUSER/.config/
#cp -r /home/$NEWUSER/tmp/config/pcmanfm-qt /home/$NEWUSER/.config/
chown -R $NEWUSER.$NEWUSER /home/$NEWUSER/.config/
cp -a $INSTALLDIR/bin/config_desktop /usr/local/bin
cp -a $INSTALLDIR/bin/clean_and_halt /usr/local/bin
chmod +x /usr/local/bin/config_desktop /usr/local/bin/clean_and_halt
apt remove -y xscreensaver
# Set autologin to the new created account
# and enable tcp connections to Xorg
cat >> /etc/sddm.conf <<EOF
[Autologin]
Session=Lubuntu
User=$NEWUSER
[X11]
ServerArguments=-listen tcp
EOF
fi
echo ""
echo "Installing VBoxGuestAdditions..."
echo ""
echo "Installing packages required:"
echo ""
$APT_CMD -y install linux-headers-generic build-essential dkms
VER=$( curl -s http://download.virtualbox.org/virtualbox/LATEST-STABLE.TXT )
echo ""
echo "Getting latest version of VBoxGuestAdditions for Linux: $VER"
echo ""
wget -nv http://download.virtualbox.org/virtualbox/$VER/VBoxGuestAdditions_$VER.iso
mkdir /media/VBoxGuestAdditions
mount -o loop,ro VBoxGuestAdditions_$VER.iso /media/VBoxGuestAdditions
sh /media/VBoxGuestAdditions/VBoxLinuxAdditions.run
rm VBoxGuestAdditions_$VER.iso
umount /media/VBoxGuestAdditions
rmdir /media/VBoxGuestAdditions
# Add new user to vboxsf group to allow shared folders
usermod -a -G vboxsf $NEWUSER
# Install sublime text editor
wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -
apt-add-repository "deb https://download.sublimetext.com/ apt/stable/"
$APT_CMD -y install sublime-text
echo ""
echo "Installing open-vm-tools (for VMware):"
echo ""
$APT_CMD -y install open-vm-tools-desktop
fi
# Disable apport to avoid "System program problem detected"
sed -i -e 's/^enabled=1/enabled=0/' /etc/default/apport
# Create script to config some desktop issues
CLEANHALT=/usr/local/bin/clean_and_halt
echo "#!/bin/bash" > $CLEANHALT
echo "" >> $CLEANHALT
echo "sudo deluser vagrant" >> $CLEANHALT
echo "sudo apt-get autoremove" >> $CLEANHALT
echo "sudo apt-get clean" >> $CLEANHALT
echo "sudo dd if=/dev/zero of=/zerofile bs=1M" >> $CLEANHALT
echo "sudo rm -f /zerofile" >> $CLEANHALT
echo "sudo rm -f /var/crash/*" >> $CLEANHALT
echo "sudo bash -c 'history -c'" >> $CLEANHALT
echo "history -c" >> $CLEANHALT
echo "sudo halt -p" >> $CLEANHALT
chmod +x $CLEANHALT
# remount the install directory. The mount seems to be lost after VBoxGuestAdditions are installed
mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` install $INSTALLDIR
if [ $? -ne 0 ]; then
echo "--"
echo "---- ERROR: Cannot mount /install directory. Aborting provision."
echo "--"
exit 1
fi
echo "----"
echo "---- $INSTALLDIR content:"
ls -al $INSTALLDIR
echo "----"
# Add sentences to /etc/profile to set DISPLAY variable to host ip address
# (needed for windows machines).
#cat >> /etc/profile <<EOF
#if [ -z \$DISPLAY ]; then
# export DISPLAY="\$(ip route show default | head -1 | awk '{print \$3}'):0"
# #echo "Setting DISPLAY to \$DISPLAY"
#fi
#EOF
# In order to have X windows working after changing to root with "sudo su"
# we have to use the "-p" option of su, that is, "sudo su -p".
# We create an alias:
cat >> /etc/bash.bashrc <<EOF
alias sudosu='sudo su -p'
EOF
echo "--"
echo "-- Configuring vim:"
echo "--"
VIMCFG=$(cat <<EOF
set tabstop=4
set shiftwidth=4
set expandtab
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("\$") | exe "normal! g'\"" | endif
endif
colorscheme elflord
EOF
)
echo "$VIMCFG" >> /etc/vim/vimrc
tail -12 /etc/vim/vimrc
# enable bash completion in interactive shells
cat << EOF >> /etc/bash.bashrc
# enable bash completion in interactive shells
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
EOF
#
# VNX Instalation
#
echo "--"
echo "-- Installing VNX:"
echo "--"
echo "--"
echo "---- Installing required packages:"
echo "--"
export DEBIAN_FRONTEND=noninteractive
#$APT_CMD -y install \
# bash-completion bridge-utils curl eog expect genisoimage gnome-terminal \
# graphviz libappconfig-perl libdbi-perl liberror-perl libexception-class-perl \
# libfile-homedir-perl libio-pty-perl libmath-round-perl libnetaddr-ip-perl \
# libnet-ip-perl libnet-ipv6addr-perl libnet-pcap-perl libnet-telnet-perl \
# libreadonly-perl libswitch-perl libsys-virt-perl libterm-readline-perl-perl \
# libvirt-bin libxml-checker-perl libxml-dom-perl libxml-libxml-perl \
# libxml-parser-perl libxml-tidy-perl lxc lxc-templates net-tools \
# openvswitch-switch picocom pv qemu-kvm screen tree uml-utilities virt-manager \
# virt-viewer vlan w3m wmctrl xdotool xfce4-terminal xterm \
# linux-image-extra-virtual eog
$APT_CMD -y install \
bash-completion bridge-utils curl eog expect genisoimage gnome-terminal \
graphviz libappconfig-perl libdbi-perl liberror-perl libexception-class-perl \
libfile-homedir-perl libio-pty-perl libmath-round-perl libnetaddr-ip-perl \
libnet-ip-perl libnet-ipv6addr-perl libnet-pcap-perl libnet-telnet-perl \
libreadonly-perl libswitch-perl libsys-virt-perl libterm-readline-perl-perl \
libvirt-clients libxml-checker-perl libxml-dom-perl libxml-libxml-perl \
libxml-parser-perl libxml-tidy-perl lxc lxc-templates net-tools \
openvswitch-switch picocom pv qemu-kvm screen tree uml-utilities virt-manager \
virt-viewer vlan w3m wmctrl xdotool xfce4-terminal xterm lsof
echo "--"
echo "---- Installing VNX application:"
echo "--"
mkdir /tmp/vnx-update
cd /tmp/vnx-update
rm -rf /tmp/vnx-update/vnx-*
wget -nv http://vnx.dit.upm.es/vnx/vnx-latest.tgz
tar xfvz vnx-latest.tgz
cd vnx-*-*
./install_vnx
echo "--"
echo "---- Modifiying VNX config file (/etc/vnx.conf):"
echo "--"
mv /usr/share/vnx/etc/vnx.conf.sample /etc/vnx.conf
# Set svg viewer to eog
sed -i -e '/\[general\]/{:a;n;/^$/!ba;i\svg_viewer=eog' -e '}' /etc/vnx.conf
# Set console to xfce4-terminal
sed -i -e '/console_term/d' /etc/vnx.conf
sed -i -e '/\[general\]/{:a;n;/^$/!ba;i\console_term=xfce4-terminal' -e '}' /etc/vnx.conf
# Set exe_host_cmd to yes
sed -i -e 's/^exe_host_cmds.*/exe_host_cmds=yes/' /etc/vnx.conf
# Disable aa_unconfined
#sed -i -e 's/aa_unconfined=.*/aa_unconfined=no/' /etc/vnx.conf
# Set union_type to overlayfs
sed -i -e 's/^union_type.*/union_type = overlayfs/' /etc/vnx.conf
# Enable overlayfs_workdir_option
sed -i -e 's/^overlayfs_workdir_option.*/overlayfs_workdir_option=yes/' /etc/vnx.conf
echo "--"
echo "-- Installing additional packages:"
echo "--"
add-apt-repository -y ppa:webupd8team/y-ppa-manager
$APT_CMD update
$APT_CMD -y install yad nmap tinc file-roller gedit wireshark tshark traceroute firefox
echo "--"
echo "---- Setting Wireshark capture permission for vagrant and $NEWUSER:"
echo "--"
addgroup wireshark
chgrp wireshark /usr/bin/dumpcap
chmod 750 /usr/bin/dumpcap
setcap cap_net_raw,cap_net_admin+eip /usr/bin/dumpcap
adduser $NEWUSER wireshark
#adduser vagrant wireshark
#echo "--"
#echo "---- Other $NEWUSER configs:"
#echo "--"
#echo "-- Changing vim color scheme..."
#echo "colorscheme elflord" >> /home/$NEWUSER/.vimrc
#
# Copy customizedir to VM
#
if [ -d "$INSTALLDIR/customizedir" ]; then
cp -a $INSTALLDIR/customizedir /home/$NEWUSER/customizedir
chown -R $NEWUSER.$NEWUSER /home/$NEWUSER/customizedir
fi
#
# Execute customization script if exists
#
if [ -f $INSTALLDIR/customize.sh ]; then
echo "--"
echo "---- Executing customization script"
echo "--"
source $INSTALLDIR/customize.sh
else
echo "--"
echo "---- No customization script found"
echo "--"
fi
echo "--"
echo "-- Cleaning package caches:"
echo "--"
$APT_CMD -y autoremove
$APT_CMD clean
echo "-- Rebooting to finish installation..."
reboot
echo "----"