Skip to content

Commit 7f9dcab

Browse files
committed
wip: fixing cloud-init configs & create image script
1 parent 88cbc6f commit 7f9dcab

2 files changed

Lines changed: 64 additions & 63 deletions

File tree

scripts/create_image.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
mkdir $1
4+
sudo debootstrap jammy $1 http://archive.ubuntu.com/ubuntu
5+
6+
sudo arch-chroot $1 /bin/bash <<EOF
7+
8+
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
9+
rm /etc/resolv.conf
10+
echo 'nameserver 1.1.1.1' > /etc/resolv.conf
11+
12+
apt-get update -y
13+
apt-get install -y cloud-init openssh-server curl
14+
cloud-init clean
15+
16+
apt-get install -y linux-modules-extra-5.15.0-25-generic
17+
echo 'fs-virtiofs' >> /etc/initramfs-tools/modules
18+
update-initramfs -c -k all
19+
20+
EOF

scripts/debug_image.sh

Lines changed: 44 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
#!/bin/bash
22

3-
set -euo pipefail
4-
5-
# constants
3+
# Constants
64
readonly SOCKET="/tmp/virtiofs.sock"
75
readonly OVERLAYFS="/tmp/merged"
86

9-
# defaults
7+
# Defaults
108
cidata="/tmp/cidata.img"
11-
user="user"
12-
pass="pass"
13-
name="cloud"
149
cmdline="rw console=ttyS0 reboot=k panic=1 root=vroot rootfstype=virtiofs rootdelay=30"
1510

16-
# globals
11+
# Globals
12+
declare -A options=(
13+
[--debug]="false"
14+
[--cidata]=""
15+
[--kernel]=""
16+
[--initramfs]=""
17+
[--init]=""
18+
[--user]="user"
19+
[--pass]="pass"
20+
[--name]="cloud"
21+
)
1722
init=""
1823
kernel=""
1924
initramfs=""
2025
fspid=0
2126
flpid=0
2227

28+
# Functions
2329
fail() {
2430
echo "Error: $*" >&2
2531
exit 1
@@ -42,8 +48,11 @@ handle_options() {
4248
;;
4349
esac
4450
done
45-
}
4651

52+
if [[ "${options[--debug]}" == true ]]; then
53+
set -x
54+
fi
55+
}
4756

4857
validate() {
4958
for tool in virtiofsd cloud-hypervisor rfs1; do
@@ -60,42 +69,33 @@ validate() {
6069
}
6170

6271
create_cidata() {
63-
sudo chroot "${OVERLAYFS}" cloud-init clean
64-
6572
if [[ -f "${options[--cidata]}" ]]; then
6673
cidata="${options[--cidata]}"
6774
return
6875
fi
6976

70-
[ ! -z "${options[--user]}" ] && user="${options[--user]}"
71-
[ ! -z "${options[--pass]}" ] && pass="${options[--pass]}"
72-
[ ! -z "${options[--name]}" ] && name="${options[--name]}"
73-
74-
echo "Creating cidata"
77+
local user="${options[--user]}"
78+
local pass="${options[--pass]}"
79+
local name="${options[--name]}"
80+
81+
echo '#cloud-config' > user-data
82+
echo 'users:' >> user-data
83+
echo " - name: $user" >> user-data
84+
echo " plain_text_passwd: $pass" >> user-data
85+
echo " shell: /bin/bash" >> user-data
86+
echo " lock_passwd: false" >> user-data
87+
echo " sudo: ALL=(ALL) NOPASSWD:ALL" >> user-data
88+
89+
echo "instance-id: $name" > meta-data
90+
echo "local-hostname: $name" >> meta-data
7591

7692
rm -f "${cidata}"
7793
mkdosfs -n CIDATA -C "${cidata}" 8192
78-
79-
cat >user-data <<EOF
80-
#cloud-config
81-
users:
82-
- name: $user
83-
plain_text_passwd: $pass
84-
shell: /bin/bash
85-
lock_passwd: false
86-
sudo: ALL=(ALL) NOPASSWD:ALL
87-
EOF
8894
mcopy -oi "${cidata}" -s user-data ::
89-
rm -f user-data
90-
91-
cat >meta-data <<EOF
92-
instance-id: $name
93-
local-hostname: $name
94-
EOF
9595
mcopy -oi "${cidata}" -s meta-data ::
96-
rm -f meta-data
97-
}
9896

97+
rm -f user-data meta-data
98+
}
9999

100100
cleanup() {
101101
local pids=( "$flpid" "$fspid" )
@@ -130,11 +130,11 @@ prepare_rootfs() {
130130
echo "${image_path} is flist, mounting"
131131

132132
rootfs=/tmp/lower
133-
sudo mkdir -p "$rootfs" && sudo chmod 777 "$rootfs"
133+
sudo mkdir -p "$rootfs" && sudo chmod 777 "$rootfs"
134134

135135
rfs1 --meta "$image_path" "$rootfs" &
136136
flpid=$!
137-
else
137+
else
138138
rootfs="${image_path}"
139139
fi
140140

@@ -166,45 +166,26 @@ prepare_boot() {
166166
cmdline="$cmdline init=$init"
167167
fi
168168

169-
if [ ! -f "$kernel" ] | [ ! -f "$initramfs" ]; then
169+
if [ ! -f "$kernel" ] || [ ! -f "$initramfs" ]; then
170170
fail "kernel or initramfs not found"
171-
# in case no kernel or initramfs, it is a container image
172-
# chroot and install ci and add symlink to host vmlinuz/initrd and update initramfs
173171
fi
174172
}
175173

176-
# main
177-
178-
# move defaults here
179-
declare -A options=(
180-
[--debug]="false"
181-
[--cidata]=""
182-
[--kernel]=""
183-
[--initramfs]=""
184-
[--init]=""
185-
[--user]=""
186-
[--pass]=""
187-
[--name]=""
188-
)
189-
174+
# Main
190175
handle_options "$@"
191176

192-
if [[ "${options[--debug]}" == true ]]; then
193-
set -x
194-
fi
195-
196-
echo "validating requirements"
177+
echo "Validating requirements"
197178
validate
198179

199-
echo "preparing rootfs"
180+
echo "Preparing rootfs"
200181
prepare_rootfs
201182

202-
echo "preparing boot"
183+
echo "Preparing boot"
203184
prepare_boot
204185

205-
echo "creating cidata"
186+
echo "Creating cidata"
206187
create_cidata
207188
trap cleanup EXIT
208189

209-
echo "booting"
210-
boot
190+
echo "Booting"
191+
boot

0 commit comments

Comments
 (0)