Skip to content

Commit 8889a9f

Browse files
committed
add makefile recipes
1 parent dcbfdea commit 8889a9f

File tree

6 files changed

+168
-24
lines changed

6 files changed

+168
-24
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PASSWORD=ubuntu
2+
PUB_KEY=ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCsBxuLygEz/oXh5Z983ERsz1qs2hfpuyFH/dZMeqXJyJNWPo/Q+5+nh+Jkm3BU1ue4tjFcsZIAEm/Hlg14btaPNDUpxuCRkpSXhdINa1Q0uoBTGuPY4mZyhAQtLWJYbiyyKBrVsR59mhQoHT3aVq6W/EJbUI+4rWkxIAsaSD6QocaU4gpQigmzj2sekPM0tHaSc+u7NMc2VyEEYNXgcYZyNNYtgRJxf9rIPgy2MnrKZ9wZrDVcQ889WlpArBDz0K6TcL7itSfvxyvNCsl/7t1poYRkp8Jrh6az+Eeh2ya3LXL9PGHf/bXBHl2yikHDvwcjZELsf03WAHiRlhckr7QYdhaXpXe+ECGsKUrq1wrkOB7hZoouzUKpsMI0Gzxiem8foukSaqSsd3lqGe5NqI15lO615s/eUH11IElrabL/uS9loctn9TNsa5gh+Dw+UAncwoI/4QSm876ubo22oe3pbHtNQit/TTvE6hIwFvUtBM19ciXlxL4zeUQV6vjpvgr+oDMBrZ6VyhF6rNsSHOnc+vcVVnLbGPyY64gS9sg8I9UPscjGwVsSblLdjtpVKmZKXKg2omLETJuGbGMhVGz75gvI6ruMnoep0gHzDiDqqesw9I7Kb9q2DLRQWAyRROsFksaxZaRl84ezKpH8ow8som0MkC7n+vtKecJU6XwxkQ== simple
3+
USERNAME=ubuntu
4+
INSTALL_SSH_SERVER=true

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iso
2+
user-data.yaml
3+
**.key**
4+
**.pem**
5+
**.pub**
6+
**.gpg**
7+
**SHA256SUMS**
8+
**id_rsa**
9+
**authorized_keys**
10+
.env

Makefile

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!make
2+
3+
-include .env
4+
5+
TODAY ?= $(shell date +"%Y-%m-%d")
6+
GCC ?= /bin/bash
7+
GCMD ?= ./ubuntu-autoinstall-generator.sh
8+
GCFLAGS ?= --all-in-one
9+
USER_DATA_INFILE ?= ./user-data.template.yaml
10+
USER_DATA_FILE ?= ./user-data.yaml
11+
12+
PROJECT := simple
13+
PASSWORD ?= $(shell stty -echo; read -p "Password: " pwd; stty echo; echo $$pwd)
14+
INSTALL_SERVER ?= false
15+
PUB_KEY ?= $(shell cat id_rsa.pub)
16+
ENCRYPTED_PASSWORD ?= $(shell /usr/bin/openssl passwd -1 $(PASSWORD))
17+
18+
RANDOM_ADJ := $(shell cat /usr/share/dict/words \
19+
| grep -E '^[[:alpha:]]{1,5}$$' \
20+
| grep -e 'ly$$' -e 'ing$$' -e 'ed$$'\
21+
| sed -e 's/\(.*\)/\L\1/'| shuf -n1)
22+
23+
RANDOM_WORD := $(shell cat /usr/share/dict/words \
24+
| grep -E '^[[:alpha:]]{1,5}$$' \
25+
| shuf -n1 \
26+
| sed -e s/"'s"/""/g \
27+
| sed -e 's/\(.*\)/\L\1/' \
28+
| sed -e 's/ *$$//g')
29+
30+
HOSTNAME ?= $(RANDOM_ADJ)-$(RANDOM_WORD)
31+
USERNAME ?= ubuntu
32+
ISO_OUTFILE ?= $(HOSTNAME)_$(TODAY).iso
33+
34+
ENV_CONTENTS := $(shell cat ./.env)
35+
36+
MAKE_ENV += ENCRYPTED_PASSWORD
37+
MAKE_ENV += PUB_KEY
38+
MAKE_ENV += HOSTNAME
39+
MAKE_ENV += USERNAME
40+
MAKE_ENV += INSTALL_SSH_SERVER
41+
42+
SHELL_EXPORT := $(foreach v,$(MAKE_ENV),$(v)='$($(v))' )
43+
44+
CDROM ?= $(shell find . -maxdepth 1 -type f -iname "*_$(TODAY).iso" | head -1)
45+
RELEASE ?= 20.04
46+
INSTALLER := /usr/bin/virt-install
47+
URI ?= qemu:///system
48+
49+
RAM ?= 2096
50+
DISK ?= pool=default,size=25,bus=virtio,format=qcow2
51+
VCPUS ?= 1
52+
OS_TYPE := linux
53+
OS_VARIANT := ubuntu$(RELEASE)
54+
NETWORK ?= network:default
55+
GRAPHICS ?= vnc
56+
CONSOLE ?= pty,target_type=serial
57+
EXTRA_ARGS ?= console=ttyS0,115200n8 serial
58+
59+
compile:
60+
$(MAKE) get_pub_key
61+
ifneq (,$(findstring HOSTNAME,$(ENV_CONTENTS)))
62+
# Found
63+
else
64+
echo "\nHOSTNAME=$(HOSTNAME)" >> ./.env;
65+
endif
66+
$(SHELL_EXPORT) envsubst <$(USER_DATA_INFILE) >$(USER_DATA_FILE); \
67+
$(GCC) $(GCMD) $(GCFLAGS) --user-data $(USER_DATA_FILE) --destination $(ISO_OUTFILE)
68+
69+
encrypt_password:
70+
@/usr/bin/openssl passwd -1 $(PASSWORD)
71+
72+
create_keypair:
73+
ssh-keygen -t rsa -b 4096 -f id_rsa -C $(PROJECT) -N "" -q
74+
chmod 600 id_rsa
75+
76+
write_file:
77+
$(SHELL_EXPORT) envsubst <$(USER_DATA_INFILE) >$(USER_DATA_FILE)
78+
79+
get_pub_key:
80+
ifeq ($(PUB_KEY),)
81+
$(MAKE) create_keypair
82+
endif
83+
84+
build_user_data:
85+
$(MAKE) get_pub_key
86+
$(MAKE) write_file
87+
88+
remove_old_keys:
89+
rm ./*id_rsa*
90+
rm ./*SHA256SUMS*
91+
rm ./*.keyring
92+
93+
cleanup_isos:
94+
rm -f ./*.iso
95+
96+
make_clean_all:
97+
$(MAKE) remove_old_keys
98+
$(MAKE) cleanup_isos
99+
100+
install:
101+
$(GCC) $(INSTALLER) \
102+
--connect=$(URI) \
103+
--name $(HOSTNAME) \
104+
--ram $(RAM) \
105+
--disk $(DISK) \
106+
--vcpus $(VCPUS) \
107+
--os-type $(OS_TYPE) \
108+
--os-variant $(OS_VARIANT) \
109+
--network $(NETWORK) \
110+
--graphics $(GRAPHICS) \
111+
--console $(CONSOLE) \
112+
--cdrom $(CDROM) \
113+
--force --debug
114+

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ By default, the source ISO image is checked for integrity and authenticity using
2020
Tested on a host running Ubuntu 20.04.1.
2121
- Utilities required:
2222
- ```p7zip-full```
23+
- ```curl```
2324
- ```mkisofs``` or ```genisoimage```
2425

2526
### Usage
@@ -53,29 +54,29 @@ Available options:
5354
### Example
5455
```
5556
user@testbox:~$ bash ubuntu-autoinstall-generator.sh -a -u user-data.example -d ubuntu-autoinstall-example.iso
56-
[2020-12-23 14:06:07] 👶 Starting up...
57-
[2020-12-23 14:06:07] 📁 Created temporary working directory /tmp/tmp.jrmlEaDhL3
58-
[2020-12-23 14:06:07] 🔎 Checking for required utilities...
59-
[2020-12-23 14:06:07] 👍 All required utilities are installed.
60-
[2020-12-23 14:06:07] 🌎 Downloading current daily ISO image for Ubuntu 20.04 Focal Fossa...
61-
[2020-12-23 14:08:01] 👍 Downloaded and saved to /home/user/ubuntu-original-2020-12-23.iso
62-
[2020-12-23 14:08:01] 🌎 Downloading SHA256SUMS & SHA256SUMS.gpg files...
63-
[2020-12-23 14:08:02] 🌎 Downloading and saving Ubuntu signing key...
64-
[2020-12-23 14:08:02] 👍 Downloaded and saved to /home/user/843938DF228D22F7B3742BC0D94AA3F0EFE21092.keyring
65-
[2020-12-23 14:08:02] 🔐 Verifying /home/user/ubuntu-original-2020-12-23.iso integrity and authenticity...
66-
[2020-12-23 14:08:09] 👍 Verification succeeded.
67-
[2020-12-23 14:08:09] 🔧 Extracting ISO image...
68-
[2020-12-23 14:08:11] 👍 Extracted to /tmp/tmp.jrmlEaDhL3
69-
[2020-12-23 14:08:11] 🧩 Adding autoinstall parameter to kernel command line...
70-
[2020-12-23 14:08:11] 👍 Added parameter to UEFI and BIOS kernel command lines.
71-
[2020-12-23 14:08:11] 🧩 Adding user-data and meta-data files...
72-
[2020-12-23 14:08:11] 👍 Added data and configured kernel command line.
73-
[2020-12-23 14:08:11] 👷 Updating /tmp/tmp.jrmlEaDhL3/md5sum.txt with hashes of modified files...
74-
[2020-12-23 14:08:11] 👍 Updated hashes.
75-
[2020-12-23 14:08:11] 📦 Repackaging extracted files into an ISO image...
76-
[2020-12-23 14:08:14] 👍 Repackaged into /home/user/ubuntu-autoinstall-example.iso
77-
[2020-12-23 14:08:14]Completed.
78-
[2020-12-23 14:08:14] 🚽 Deleted temporary working directory /tmp/tmp.jrmlEaDhL3
57+
[2020-12-23 14:06:07]Starting up...
58+
[2020-12-23 14:06:07]Created temporary working directory /tmp/tmp.jrmlEaDhL3
59+
[2020-12-23 14:06:07]Checking for required utilities...
60+
[2020-12-23 14:06:07]All required utilities are installed.
61+
[2020-12-23 14:06:07]Downloading current daily ISO image for Ubuntu 20.04 Focal Fossa...
62+
[2020-12-23 14:08:01]Downloaded and saved to /home/user/ubuntu-original-2020-12-23.iso
63+
[2020-12-23 14:08:01]Downloading SHA256SUMS & SHA256SUMS.gpg files...
64+
[2020-12-23 14:08:02]Downloading and saving Ubuntu signing key...
65+
[2020-12-23 14:08:02]Downloaded and saved to /home/user/843938DF228D22F7B3742BC0D94AA3F0EFE21092.keyring
66+
[2020-12-23 14:08:02]Verifying /home/user/ubuntu-original-2020-12-23.iso integrity and authenticity...
67+
[2020-12-23 14:08:09]Verification succeeded.
68+
[2020-12-23 14:08:09]Extracting ISO image...
69+
[2020-12-23 14:08:11]Extracted to /tmp/tmp.jrmlEaDhL3
70+
[2020-12-23 14:08:11]Adding autoinstall parameter to kernel command line...
71+
[2020-12-23 14:08:11]Added parameter to UEFI and BIOS kernel command lines.
72+
[2020-12-23 14:08:11]Adding user-data and meta-data files...
73+
[2020-12-23 14:08:11]Added data and configured kernel command line.
74+
[2020-12-23 14:08:11]Updating /tmp/tmp.jrmlEaDhL3/md5sum.txt with hashes of modified files...
75+
[2020-12-23 14:08:11]Updated hashes.
76+
[2020-12-23 14:08:11]Repackaging extracted files into an ISO image...
77+
[2020-12-23 14:08:14]Repackaged into /home/user/ubuntu-autoinstall-example.iso
78+
[2020-12-23 14:08:14]Completed.
79+
[2020-12-23 14:08:14]Deleted temporary working directory /tmp/tmp.jrmlEaDhL3
7980
```
8081

8182
Now you can boot your target machine using ```ubuntu-autoinstall-example.iso``` and it will automatically install Ubuntu using the configuration from ```user-data.example```.

user-data.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ autoinstall:
33
version: 1
44
identity:
55
hostname: ubuntu-server
6-
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
6+
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0" # ubuntu
77
username: ubuntu
8+
ssh:
9+
install-server: true
10+
authorized-keys:
11+
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDLUB0B9Pog/XwBgT3WPN8Xmmxv0V9dUJvSJ5njL5uBGy9/fcLKueSeGkVvFCD1sSa7JVCcsYU0pHbNzjmE209U9ZKyz78PsK/6ibGonHXLqs3G/Fl1v6yqHsIprtUdW78dJSZf34kEpD3sxYwayvViy89dofMhLfgrYrf+KNzdiQlshVWrTbNg4iPsF6MVGS5H3zoxx66oAX0wXw1N9jT8DmyK+DOioi6EVvdE5kt80yPqWq4iycymJpHOkqML3yfDtAzVBni4q47vxhYKGODxa4Mn80zuolzRKghNHdzdgOpnBIs32wQQScMyLJ6z24VWcXjBm6lP7LHmkUBuUQlRLGQSW1KihrjGbpmS/xB54nF2tOAiuzxySGFrNdcOdo9tIuEnXz3HtgrIIIq6pyAc0zct/edHZVoJlgVxp/14XV/kaBzIMPE8+iYHObMTvki+8u2+7ty39au/R0K+RghdTq+/HmpRYQh/2tzJ+X6NEdnbmv4twYATHHNApt8e0uc=

user-data.template.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#cloud-config
2+
autoinstall:
3+
version: 1
4+
identity:
5+
hostname: ${HOSTNAME}
6+
password: "${ENCRYPTED_PASSWORD}"
7+
username: ${USERNAME}
8+
ssh:
9+
install-server: ${INSTALL_SSH_SERVER}
10+
authorized-keys:
11+
- ${PUB_KEY}

0 commit comments

Comments
 (0)