-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathubuntu-installer.sh
executable file
·1426 lines (1134 loc) · 35.1 KB
/
ubuntu-installer.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
function main {
# declare local variables
local TASK
local LONG_OPTIONS
local OPTIONS_PARSED
# set default values and configuration
HOME="/tmp"
SELF_PATH="$(readlink -f "$0")"
SELF_NAME="$(basename "$SELF_PATH")"
NAME_REGEX='^[a-z][-a-z0-9]*$'
EXTRA_GROUPS='adm audio cdrom dialout dip docker floppy libvirt lpadmin plugdev scanner sudo users video wireshark'
SHOW_HELP=false
SHELL_LOGIN=false
USE_EFI=false
USE_SEPARATE_HOME=false
COPY_NETWORK_SETTINGS=false
# get settings from a dot-env file if available
[[ -f ".env" ]] && source ".env"
#define long options
LONG_OPTIONS='help,login,efi,separate-home,copy-network-settings'
LONG_OPTIONS="$LONG_OPTIONS"',username:,hostname:,codename:,dev-root:,dev-home:,dev-boot:'
LONG_OPTIONS="$LONG_OPTIONS"',bundles:,bundles-file:,debconf-file:,dconf-file:'
LONG_OPTIONS="$LONG_OPTIONS"',mirror:,locales:,time-zone:,user-gecos:,password:'
LONG_OPTIONS="$LONG_OPTIONS"',keyboard-model:,keyboard-layout:,keyboard-variant:,keyboard-options:'
# parse arguments
OPTIONS_PARSED=$(
getopt \
--options 'hlesku:n:c:x:y:z:b:' \
--longoptions "$LONG_OPTIONS" \
--name "$SELF_NAME" \
-- "$@"
)
# replace arguments
eval set -- "$OPTIONS_PARSED"
# apply arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
SHOW_HELP=true
shift 1
;;
-l | --login)
SHELL_LOGIN=true
shift 1
;;
-e | --efi)
USE_EFI=true
shift 1
;;
-s | --separate-home)
USE_SEPARATE_HOME=true
shift 1
;;
-k | --copy-network-settings)
COPY_NETWORK_SETTINGS=true
shift 1
;;
-u | --username)
USERNAME_NEW="$2"
shift 2
;;
-n | --hostname)
HOSTNAME_NEW="$2"
shift 2
;;
-c | --codename)
CODENAME="$2"
shift 2
;;
-x | --dev-root)
DEV_ROOT="$2"
shift 2
;;
-y | --dev-home)
DEV_HOME="$2"
shift 2
;;
-z | --dev-boot)
DEV_BOOT="$2"
shift 2
;;
-b | --bundles)
BUNDLES="$2"
shift 2
;;
--bundles-file)
BUNDLES_FILE="$2"
shift 2
;;
--debconf-file)
DEBCONF_FILE="$2"
shift 2
;;
--dconf-file)
DCONF_FILE="$2"
shift 2
;;
--mirror)
MIRROR="$2"
shift 2
;;
--locales)
LOCALES="$2"
shift 2
;;
--time-zone)
TZ="$2"
shift 2
;;
--user-gecos)
USER_GECOS="$2"
shift 2
;;
--password)
PASSWORD="$2"
shift 2
;;
--keyboard-model)
XKBMODEL="$2"
shift 2
;;
--keyboard-layout)
XKBLAYOUT="$2"
shift 2
;;
--keyboard-variant)
XKBVARIANT="$2"
shift 2
;;
--keyboard-options)
XKBOPTIONS="$2"
shift 2
;;
--)
shift 1
break
;;
*)
break
;;
esac
done
# either print the help text or process task
if "$SHOW_HELP"; then
# show help text
show_help
else
# check if there is a unassigned argument to interpret it as task
if [[ $# -eq 0 ]]; then
echo "$SELF_NAME: require a task to continue" >&2
exit 1
fi
# assign the task
TASK="$1"
shift 1
# check if there is no unassigned argument left
if [[ $# -ne 0 ]]; then
echo "$SELF_NAME: cannot handle unassigned arguments: $*" >&2
exit 1
fi
# select task
case "$TASK" in
create-user)
task_create_user
;;
modify-user)
task_modify_user
;;
manage-package-sources)
task_manage_package_sources
;;
install-packages-base)
task_install_packages_base
;;
install-packages-system-minimal)
task_install_packages_system_minimal
;;
install-packages-container-image-minimal)
task_install_packages_container_image_minimal
;;
install-system)
task_install_system
;;
install-lxc-image)
task_install_lxc_image
;;
install-docker-image)
task_install_docker_image
;;
configure-locales)
task_configure_locales
;;
configure-tzdata)
task_configure_tzdata
;;
configure-keyboard)
task_configure_keyboard
;;
configure-tools)
task_configure_tools
;;
*)
echo "$SELF_NAME: require a valid task" >&2
exit 1
;;
esac
fi
}
function verify_root_privileges {
if [[ $EUID -ne 0 ]]; then
echo "$SELF_NAME: require root privileges" >&2
exit 1
fi
}
function verify_username {
if [[ -n "${USERNAME_NEW:-}" ]]; then
if ! echo "$USERNAME_NEW" | grep -qE "$NAME_REGEX"; then
echo "$SELF_NAME: require username that matches regular expression $NAME_REGEX" >&2
exit 1
fi
else
# by default, use the name of the user who runs the script
USERNAME_NEW="$(get_username)"
fi
# make sure the username is different to root
if [[ "${USERNAME_NEW:-}" == "root" ]]; then
echo "$SELF_NAME: require username different to root" >&2
exit 1
fi
}
function verify_username_exists {
if getent passwd "${USERNAME_NEW:-}" >/dev/null; then
if ! "$1"; then
echo "$SELF_NAME: the username has already been taken" >&2
exit 1
fi
else
if "$1"; then
echo "$SELF_NAME: the username does not exist" >&2
exit 1
fi
fi
}
function verify_hostname {
# by default, use the hostname of the running system
if [[ -z "${HOSTNAME_NEW:-}" ]]; then
HOSTNAME_NEW="$HOSTNAME"
fi
}
function verify_codename {
if [[ -z "${CODENAME:-}" ]] || ! echo "${CODENAME:-}" | grep -qE '^[a-z]+$'; then
echo "$SELF_NAME: require valid Ubuntu codename" >&2
exit 1
fi
}
function verify_mounting_root {
# the block device file for "/" must exist and be unmounted
if [[ -z "${DEV_ROOT:-}" ]] || [[ ! -b "${DEV_ROOT:-}" ]] || mount | grep -q "${DEV_ROOT:-}"; then
echo "$SELF_NAME: require unmounted device file for /" >&2
exit 1
fi
}
function verify_mounting_home {
# only perform checks if a separate home partition is used
if "$USE_SEPARATE_HOME"; then
# the block device file for "/home" must exist
if [[ -z "${DEV_HOME:-}" ]] || [[ ! -b "${DEV_HOME:-}" ]]; then
echo "$SELF_NAME: require device file for /home" >&2
exit 1
fi
fi
}
function verify_mounting_boot {
if "$USE_EFI"; then
# use mounted UEFI partition by default
if [[ -z "${DEV_BOOT:-}" ]]; then
DEV_BOOT="$(cat /proc/mounts | grep -E /boot/efi | cut -d ' ' -f 1)"
fi
# the block device file for /boot/efi must exist
if [[ -z "${DEV_BOOT:-}" ]] || [[ ! -b "${DEV_BOOT:-}" ]]; then
echo "$SELF_NAME: require device file for /boot/efi" >&2
exit 1
fi
else
# the block device file for GRUB installation must exist
if [[ -z "${DEV_BOOT:-}" ]] || [[ ! -b "${DEV_BOOT:-}" ]]; then
echo "$SELF_NAME: require device file for GRUB installation" >&2
exit 1
fi
fi
}
function task_create_user {
# verify preconditions
verify_root_privileges
verify_username
verify_username_exists false
# create user and home-directory if not exist
if [[ -n "${PASSWORD:-}" ]]; then
adduser --add_extra_groups --disabled-password --gecos "${USER_GECOS:-}" "$USERNAME_NEW"
usermod --password "$PASSWORD" "$USERNAME_NEW"
else
adduser --add_extra_groups --gecos "${USER_GECOS:-}" "$USERNAME_NEW"
fi
}
function task_modify_user {
# verify preconditions
verify_root_privileges
verify_username
verify_username_exists true
# create home-directory if not exist
mkhomedir_helper "$USERNAME_NEW"
# add user to extra groups
for i in $EXTRA_GROUPS; do
if grep -qE "^$i:" /etc/group; then
usermod -aG "$i" "$USERNAME_NEW"
fi
done
}
function task_manage_package_sources {
# declare local variables
local TRUSTED_GPG
local SOURCES_LIST
local COMPONENTS
# verify preconditions
verify_root_privileges
# set variables
TRUSTED_GPG='/etc/apt/trusted.gpg.d'
SOURCES_LIST='/etc/apt/sources.list.d'
COMPONENTS='main universe multiverse restricted'
# by default, use the whole Ubuntu mirror list
if [[ -z "${MIRROR:-}" ]]; then
MIRROR='mirror://mirrors.ubuntu.com/mirrors.txt'
fi
# set OS variables
source /etc/os-release
# add package sources
add-apt-repository -y -s "deb $MIRROR $UBUNTU_CODENAME $COMPONENTS"
add-apt-repository -y -s "deb $MIRROR $UBUNTU_CODENAME-updates $COMPONENTS"
add-apt-repository -y -s "deb $MIRROR $UBUNTU_CODENAME-security $COMPONENTS"
add-apt-repository -y -s "deb $MIRROR $UBUNTU_CODENAME-backports $COMPONENTS"
# add package sources for sbt
wget -qO - 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823' |
gpg --no-default-keyring --keyring "gnupg-ring:$TRUSTED_GPG/scalasbt-release.gpg" --import
chmod 644 "$TRUSTED_GPG/scalasbt-release.gpg"
echo 'deb https://repo.scala-sbt.org/scalasbt/debian all main' >"$SOURCES_LIST/sbt.list"
# add package sources for chrome browser
wget -qO - 'https://dl-ssl.google.com/linux/linux_signing_key.pub' |
gpg --no-default-keyring --keyring "gnupg-ring:$TRUSTED_GPG/google-chrome.gpg" --import
chmod 644 "$TRUSTED_GPG/google-chrome.gpg"
echo 'deb https://dl.google.com/linux/chrome/deb/ stable main' >"$SOURCES_LIST/google-chrome.list"
# update package lists
apt-get update
}
function task_install_packages_base {
# verify preconditions
verify_root_privileges
# disable interactive interfaces
export DEBIAN_FRONTEND=noninteractive
# update installed software
apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
apt-get -y autoremove --purge
# install main packages
apt-get -y install ubuntu-server ubuntu-standard
apt-get -y install lxc debootstrap bridge-utils
apt-get -y install software-properties-common
apt-get -y install debconf-utils
apt-get -y install aptitude
# install version control system
apt-get -y install git
# install text editors and query tools
apt-get -y install vim
apt-get -y install emacs-nox
apt-get -y install nano
apt-get -y install jq
# install archiving and compression tools
apt-get -y install tar gzip bzip2 zip unzip p7zip
# install SSH support
apt-get -y install openssh-server openssh-client
# install SSL support
apt-get -y install openssl
# install GnuPG
apt-get -y install gnupg
# install support of snap packages
apt-get -y install snapd
# install OpenJDK JRE (headless)
apt-get -y install "openjdk-$(find_openjdk_lts_versions | sort -rn | head -n 1)-jre-headless"
# install everything else needed by a simple general purpose system
aptitude -y install ~pstandard ~pimportant ~prequired
# pre-seed the debconf database
configure_debconf
# install additional software
install_bundles
}
function configure_debconf {
local FILE
FILE="${DEBCONF_FILE:-"/var/local/ubuntu-headless-installer/debconf.txt"}"
# set default values for packages
debconf-set-selections "$FILE"
}
function install_bundles {
local FILE
local LINE
local CATEGORY_PATTERN
local CATEGORY_MAIN
local CATEGORY_SUB
local SYSLANG
local OPENJDK_VERSION
local INDEX
local BARRAY
local INSTALL_GRANTED
FILE="${BUNDLES_FILE:-"/var/local/ubuntu-headless-installer/bundles.txt"}"
CATEGORY_PATTERN="^\[([a-z0-9_]+)(:([a-z0-9_]+))?\]$"
INSTALL_GRANTED=false
source /etc/default/locale
SYSLANG="$(echo "$LANG" | grep -oE '^([a-zA-Z]+)' | sed -r 's/^(C|POSIX)$/en/')"
SYSLANG="${SYSLANG:-'en'}"
OPENJDK_VERSION="$(find_openjdk_lts_versions | sort -rn | head -n 1)"
if [[ -z "${BUNDLES:-}" ]]; then
# by default, use an empty array for bundle entries
declare -a BARRAY
else
# create an array with bundle entries
readarray -td ',' BARRAY <<<"$BUNDLES"
for INDEX in "${!BARRAY[@]}"; do BARRAY[$INDEX]="$(echo "${BARRAY[$INDEX]}" | tr -d '[:space:]')"; done
fi
while IFS= read -r LINE; do
# remove comments
LINE="$(echo "$LINE" | cut -d '#' -f 1)"
# remove leading and trailing whitespaces
LINE="$(echo "$LINE" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
# substitute LANG placeholder
LINE="${LINE//\%LANG\%/$SYSLANG}"
# substitute OPENJDK_VERSION placeholder
LINE="${LINE//\%OPENJDK_VERSION\%/$OPENJDK_VERSION}"
# skip empty lines
if [[ -z "$LINE" ]]; then
continue
fi
if [[ "$LINE" =~ $CATEGORY_PATTERN ]]; then
CATEGORY_MAIN="${BASH_REMATCH[1]}"
CATEGORY_SUB="${BASH_REMATCH[3]}"
if echo "${BARRAY[@]}" | grep -qw "$CATEGORY_MAIN" &&
{ [[ -z "$CATEGORY_SUB" ]] || echo "${BARRAY[@]}" | grep -qw "$CATEGORY_SUB"; }; then
INSTALL_GRANTED=true
else
INSTALL_GRANTED=false
fi
else
if "$INSTALL_GRANTED"; then
# install APT packages
echo "$LINE" | xargs apt-get -y install
fi
fi
done <"$FILE"
}
function task_install_packages_system_minimal {
# verify preconditions
verify_root_privileges
verify_mounting_boot
# disable interactive interfaces
export DEBIAN_FRONTEND=noninteractive
# update installed software
apt-get update
apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
apt-get -y autoremove --purge
# install main packages
apt-get -y install ubuntu-minimal
apt-get -y install debootstrap
apt-get -y install software-properties-common
# install GRUB bootloader
if "$USE_EFI"; then
apt-get -y install grub-efi
grub-install --target=x86_64-efi --efi-directory=/boot/efi
echo 'The boot order must be adjusted manually using the efibootmgr tool.'
else
apt-get -y install grub-pc
grub-install "$DEV_BOOT"
fi
# install Linux kernel
apt-get -y install linux-generic
# set GRUB_CMDLINE_LINUX_DEFAULT
sed -i 's/^GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT="quiet noplymouth"/' /etc/default/grub
# apply grub configuration changes
update-grub
}
function task_install_packages_container_image_minimal {
# verify preconditions
verify_root_privileges
# disable interactive interfaces
export DEBIAN_FRONTEND=noninteractive
# update installed software
apt-get update
apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
apt-get -y autoremove --purge
# install main packages
apt-get -y install ubuntu-minimal
apt-get -y install debootstrap
apt-get -y install software-properties-common
# install init scripts for cloud instances
apt-get -y install cloud-init
}
function task_install_system {
# verify preconditions
verify_root_privileges
verify_username
verify_hostname
verify_codename
verify_mounting_root
verify_mounting_home
verify_mounting_boot
# format $DEV_ROOT
mkfs.ext4 "$DEV_ROOT"
# mount "/", "/home" and "/boot/efi"
mounting_step_1
# execute debootstrap
install_minimal_system
# configuration before starting chroot
configure_hosts
configure_fstab
configure_users
configure_network
# mount OS resources into chroot environment
mounting_step_2
# install basic tools, kernel and bootloader
if "$USE_EFI"; then
chroot "$CHROOT" "$SELF_NAME" install-packages-system-minimal --efi
else
chroot "$CHROOT" "$SELF_NAME" install-packages-system-minimal --dev-boot "$DEV_BOOT"
fi
# configure packages
chroot "$CHROOT" "$SELF_NAME" configure-locales --locales "${LOCALES:-}"
chroot "$CHROOT" "$SELF_NAME" configure-tzdata --time-zone "${TZ:-}"
chroot "$CHROOT" "$SELF_NAME" configure-keyboard \
--keyboard-model "${XKBMODEL:-}" \
--keyboard-layout "${XKBLAYOUT:-}" \
--keyboard-variant "${XKBVARIANT:-}" \
--keyboard-options "${XKBOPTIONS:-}"
chroot "$CHROOT" "$SELF_NAME" configure-tools
# manage package sources
chroot "$CHROOT" "$SELF_NAME" manage-package-sources --mirror "${MIRROR:-}"
# install software
chroot "$CHROOT" "$SELF_NAME" install-packages-base \
--bundles "${BUNDLES:-}" \
--bundles-file "${BUNDLES_FILE:-}" \
--debconf-file "${DEBCONF_FILE:-}"
# do some modifications for desktop environments
configure_desktop
# remove retrieved package files
chroot "$CHROOT" apt-get clean
# create user
chroot "$CHROOT" "$SELF_NAME" create-user \
--username "$USERNAME_NEW" \
--password "${PASSWORD:-}" \
--user-gecos "${USER_GECOS:-}"
# login to shell for diagnostic purposes
if "$SHELL_LOGIN"; then
echo "$SELF_NAME: You are now logged in to the chroot environment for diagnostic purposes. Press Ctrl-D to escape."
chroot "$CHROOT" /bin/bash
fi
# unmount everything
unmounting_step_2
unmounting_step_1
# show that we are done here
echo "$SELF_NAME: done."
}
function task_install_lxc_image {
# declare local variables
local TEMP_DIR
local IMAGE_RELEASE
local IMAGE_NAME
# verify preconditions
verify_root_privileges
verify_codename
# create temporary directory
TEMP_DIR="$(mktemp -d)"
# set root directory
CHROOT="$TEMP_DIR/rootfs"
# create root directory
mkdir -p "$CHROOT"
# execute debootstrap
install_minimal_system
# configuration before starting chroot
configure_users
# mount OS resources into chroot environment
mounting_step_2
# install basic tools
chroot "$CHROOT" "$SELF_NAME" install-packages-container-image-minimal
# configure packages
chroot "$CHROOT" "$SELF_NAME" configure-locales --locales "${LOCALES:-C.UTF-8}"
chroot "$CHROOT" "$SELF_NAME" configure-tzdata --time-zone "${TZ:-UTC}"
chroot "$CHROOT" "$SELF_NAME" configure-tools
# manage package sources
chroot "$CHROOT" "$SELF_NAME" manage-package-sources --mirror "${MIRROR:-}"
# install software
chroot "$CHROOT" "$SELF_NAME" install-packages-base \
--bundles "${BUNDLES:-}" \
--bundles-file "${BUNDLES_FILE:-}" \
--debconf-file "${DEBCONF_FILE:-}"
# do some modifications for desktop environments
configure_desktop
# remove retrieved package files
chroot "$CHROOT" apt-get clean
# unmount everything
unmounting_step_2
# define image name
IMAGE_RELEASE="$(cat '/proc/sys/kernel/random/uuid' | tr -dc '[:alnum:]')"
IMAGE_NAME="custom-ubuntu/$CODENAME-$IMAGE_RELEASE"
# create metadata file
{
echo "architecture: x86_64"
echo "creation_date: $(date +%s)"
echo "properties:"
echo " architecture: x86_64"
echo " description: Ubuntu $CODENAME with extended tooling"
echo " os: ubuntu"
echo " release: $CODENAME $IMAGE_RELEASE"
echo "templates:"
echo " /etc/hosts:"
echo " when:"
echo " - create"
echo " - copy"
echo " - rename"
echo " template: hosts.tpl"
echo " /etc/hostname:"
echo " when:"
echo " - create"
echo " - copy"
echo " - rename"
echo " template: hostname.tpl"
} >"$TEMP_DIR/metadata.yaml"
# create template directory
mkdir "$TEMP_DIR/templates"
# create templates (use container name as hostname)
configure_hosts_template "{{ container.name }}" "$TEMP_DIR/templates/hostname.tpl" "$TEMP_DIR/templates/hosts.tpl"
# create tarballs for rootfs and metadata
tar -czf "$TEMP_DIR/rootfs.tar.gz" -C "$CHROOT" .
tar -czf "$TEMP_DIR/metadata.tar.gz" -C "$TEMP_DIR" 'metadata.yaml' 'templates'
# install image
lxc image import "$TEMP_DIR/metadata.tar.gz" "$TEMP_DIR/rootfs.tar.gz" --alias "$IMAGE_NAME"
# remove temporary directory
rm -rf "$TEMP_DIR"
# show that we are done here
echo "$SELF_NAME: LXC image $IMAGE_NAME imported"
}
function task_install_docker_image {
# declare local variables
local TEMP_DIR
local IMAGE_RELEASE
local IMAGE_NAME
local CONTAINER_CMD
# verify preconditions
verify_root_privileges
verify_codename
# create temporary directory
TEMP_DIR="$(mktemp -d)"
# set root directory
CHROOT="$TEMP_DIR/rootfs"
# create root directory
mkdir -p "$CHROOT"
# execute debootstrap
install_minimal_system
# configuration before starting chroot
configure_users
# mount OS resources into chroot environment
mounting_step_2
# install basic tools
chroot "$CHROOT" "$SELF_NAME" install-packages-container-image-minimal
# configure packages
chroot "$CHROOT" "$SELF_NAME" configure-locales --locales "${LOCALES:-C.UTF-8}"
chroot "$CHROOT" "$SELF_NAME" configure-tzdata --time-zone "${TZ:-UTC}"
chroot "$CHROOT" "$SELF_NAME" configure-tools
# manage package sources
chroot "$CHROOT" "$SELF_NAME" manage-package-sources --mirror "${MIRROR:-}"
# install software
chroot "$CHROOT" "$SELF_NAME" install-packages-base \
--bundles "${BUNDLES:-}" \
--bundles-file "${BUNDLES_FILE:-}" \
--debconf-file "${DEBCONF_FILE:-}"
# do some modifications for desktop environments
configure_desktop
# remove retrieved package files
chroot "$CHROOT" apt-get clean
# unmount everything
unmounting_step_2
# define image name
IMAGE_RELEASE="$(cat '/proc/sys/kernel/random/uuid' | tr -dc '[:alnum:]')"
IMAGE_NAME="custom/ubuntu:$CODENAME-$IMAGE_RELEASE"
if command -v docker &>/dev/null; then
CONTAINER_CMD="docker"
elif command -v podman &>/dev/null; then
CONTAINER_CMD="podman"
else
echo "$SELF_NAME: missing container tooling" >&2
exit 1
fi
# install image
tar -cC "$CHROOT" . | "$CONTAINER_CMD" import - "$IMAGE_NAME"
# remove temporary directory
rm -rf "$TEMP_DIR"
# show that we are done here
echo "$SELF_NAME: Docker image $IMAGE_NAME imported"
}
function task_configure_locales {
# declare local variables
local LOCALES_LIST
local PRIMARY_LOCAL
# verify preconditions
verify_root_privileges
if [[ -n "${LOCALES:-}" ]]; then
LOCALES_LIST="$(echo "$LOCALES" | tr ',' ' ')"
PRIMARY_LOCAL="$(echo "$LOCALES_LIST" | cut -d ' ' -f 1)"
for i in $LOCALES_LIST; do
if [[ "$i" != "POSIX" ]] && [[ "$i" != "C" ]] && [[ "$i" != "C."* ]]; then
# generate a locale for each entry in list
locale-gen "$i"
fi
done
export LANG="$PRIMARY_LOCAL"
export LANGUAGE=""
export LC_CTYPE="$PRIMARY_LOCAL"
export LC_NUMERIC="$PRIMARY_LOCAL"
export LC_TIME="$PRIMARY_LOCAL"
export LC_COLLATE="$PRIMARY_LOCAL"
export LC_MONETARY="$PRIMARY_LOCAL"
export LC_MESSAGES="POSIX"
export LC_PAPER="$PRIMARY_LOCAL"
export LC_NAME="$PRIMARY_LOCAL"
export LC_ADDRESS="$PRIMARY_LOCAL"
export LC_TELEPHONE="$PRIMARY_LOCAL"
export LC_MEASUREMENT="$PRIMARY_LOCAL"
export LC_IDENTIFICATION="$PRIMARY_LOCAL"
export LC_ALL=""
# the first locale defined in the list will be installed
dpkg-reconfigure --frontend noninteractive locales
else
# interactive configuration by user
dpkg-reconfigure locales
fi
}
function task_configure_tzdata {
# verify preconditions
verify_root_privileges
if [[ -n "${TZ:-}" ]]; then
# set preconfigured time zone
ln -fs "/usr/share/zoneinfo/$TZ" /etc/localtime && dpkg-reconfigure --frontend noninteractive tzdata
else
# interactive configuration by user
dpkg-reconfigure tzdata
fi
}
function task_configure_keyboard {
# declare local variables
local FILE
# verify preconditions
verify_root_privileges
# set path for output file
FILE="/etc/default/keyboard"
if [[ -n "${XKBMODEL:-}" ]]; then
sed -i 's/^XKBMODEL=.*/XKBMODEL="'"$XKBMODEL"'"/' "$FILE"
fi
if [[ -n "${XKBLAYOUT:-}" ]]; then
sed -i 's/^XKBLAYOUT=.*/XKBLAYOUT="'"$XKBLAYOUT"'"/' "$FILE"
fi
if [[ -n "${XKBVARIANT:-}" ]]; then
sed -i 's/^XKBVARIANT=.*/XKBVARIANT="'"$XKBVARIANT"'"/' "$FILE"
fi
if [[ -n "${XKBOPTIONS:-}" ]]; then
sed -i 's/^XKBOPTIONS=.*/XKBOPTIONS="'"$XKBOPTIONS"'"/' "$FILE"
fi
if [[ -n "${XKBMODEL:-}" ]] ||
[[ -n "${XKBLAYOUT:-}" ]] ||
[[ -n "${XKBVARIANT:-}" ]] ||
[[ -n "${XKBOPTIONS:-}" ]]; then
# set preconfigured keyboard layout
dpkg-reconfigure --frontend noninteractive keyboard-configuration
else
# interactive configuration by user
dpkg-reconfigure keyboard-configuration
fi
}
function task_configure_tools {
# declare local variables
local FILE_VIMRC
local FILE_BASHRC
local COMPLETION_SCRIPT
# verify preconditions
verify_root_privileges
# set paths for output files
FILE_VIMRC="/etc/vim/vimrc"
FILE_BASHRC="/etc/bash.bashrc"
# add vim settings
cat >>"$FILE_VIMRC" <<'EOF'
filetype plugin indent on
syntax on
set nocp
set background=light
set tabstop=4
set shiftwidth=4
set expandtab
EOF
# enable bash history search completion
cat >>"$FILE_BASHRC" <<'EOF'