Skip to content
Hands-on IT IT Drills

Day 10: Your Own Appliance

Goal: Turn a prepared VM into your own image that already contains notes-api, and a template that starts from it without installing packages. · Duration: approx. 60 min · Prerequisite: Day 9

Briefing

  • So far, every notes VM installs Podman at start and downloads the container image. That takes time and needs internet.
  • Better: set up a VM once, save its disk as a new image (disk-saveas), and start from that image. The startup script then only configures.
  • This is the path from a “startup-script appliance” to an “image appliance”. Ready-made appliances in the Marketplace are built exactly this way.
 Template alpine ─► VM bau ─(apk add podman, podman pull)─► poweroff
                                                         └─ disk-saveas ─► Image notes-basis
 Template notes-schnell (Image notes-basis, kurzes Startskript) ─► VM in ~60 s

Drill 1: The build VM

onetemplate instantiate alpine --name bau --user-inputs GRUSS=bau

After a minute:

IP=$(onevm show bau | grep -o 'ETH0_IP="[0-9.]*' | cut -d'"' -f2)
ssh $IP "apk add -q podman && rc-update add cgroups && rc-service cgroups start >/dev/null \
  && podman pull -q ghcr.io/thomaszachmann/notes-api:0.7.1 && rm -rf /var/cache/apk/* \
  && podman images && df -h / | tail -1"

Expected (shortened):

* For running rootless Podman you need to:
...
 * service cgroups added to runlevel default
eb5553ddb0cff5727bf122a2650b5574ba2cf00f61f9d38f2627eb249c1b469f
REPOSITORY                        TAG         IMAGE ID      CREATED     SIZE
ghcr.io/thomaszachmann/notes-api  0.7.1       eb5553ddb0cf  9 days ago  11.9 MB
/dev/vda        474M  214M  234M  48% /
  • rc-update add cgroups registers the service permanently. In the image, it then starts on its own.
  • rm -rf /var/cache/apk/* keeps the image small. Anything left lying around here ends up in every future VM.

Save your progress? Sign up for free

Drill 2: Save the disk

You save with the VM powered off; otherwise the file system is in the middle of writing:

onevm poweroff bau

When onevm list shows poff:

onevm disk-saveas bau 0 notes-basis

Expected:

Image ID: 9

0 is the disk ID of the operating system. Half a minute later:

oneimage list | grep -E 'ID|notes-basis'

Expected (shortened):

  ID USER     GROUP    NAME            DATASTORE     SIZE TYPE PER STAT RVMS
   9 oneadmin oneadmin notes-basis     default       512M OS    No rdy     0

The build VM has served its purpose:

onevm terminate bau

Save your progress? Sign up for free

Drill 3: The fast template

The startup script shrinks to the essentials, without apk and without cgroups:

mkdir -p ~/one-lab/tag10
cd ~/one-lab/tag10
cat > notes-schnell.sh <<'EOF'
#!/bin/sh
# notes-api aus dem fertigen Image: nur noch warten und starten
. /run/one-context/one_env
until nc -z -w2 "$DB_HOST" 5432; do sleep 5; done
URL="postgres://notes:$DB_PASSWORD@$DB_HOST:5432/notes?sslmode=disable"
IMG="ghcr.io/thomaszachmann/notes-api:$NOTES_VERSION"
podman run --rm --network host -e NOTES_DB_URL="$URL" "$IMG" migrate
podman rm -f notes
podman run -d --name notes --network host -e NOTES_API_KEY="$API_KEY" -e NOTES_DB_URL="$URL" "$IMG"
EOF

The template is derived from the one from Day 6, with a different name, a different image, and a new script:

sed -e 's/^NAME = "notes"/NAME = "notes-schnell"/' \
  -e 's/IMAGE = "Alpine Linux 3.20"/IMAGE = "notes-basis"/' \
  -e 's|START_SCRIPT_BASE64 = "[^"]*"|START_SCRIPT_BASE64 = "SKRIPT"|' \
  ../tag06/notes.tpl > notes-schnell.tpl
sed -i "s|SKRIPT|$(base64 -w0 notes-schnell.sh)|" notes-schnell.tpl
grep -E '^NAME|IMAGE =' notes-schnell.tpl
onetemplate create notes-schnell.tpl

Expected:

NAME = "notes-schnell"
DISK = [ IMAGE = "notes-basis", IMAGE_UNAME = "oneadmin" ]
ID: 7

Save your progress? Sign up for free

Drill 4: The comparison

The database of the OneFlow service from Day 9 is running and has the IP 10.10.10.20. Two additional notes VMs may log in there. The stopwatch runs from the command to the first response:

T0=$(date +%s)
onetemplate instantiate notes-schnell --name schnell \
  --user-inputs DB_PASSWORD=lab-db-pass-123,API_KEY=lab-api-key-123,NOTES_VERSION=0.7.1
sleep 15
IP=$(onevm show schnell | grep -o 'ETH0_IP="[0-9.]*' | cut -d'"' -f2)
until curl -s -m2 $IP:8080/version >/dev/null; do sleep 2; done
echo "Sekunden: $(( $(date +%s) - T0 ))"

Expected (roughly):

VM ID: 32
Sekunden: 62

The same with the old template:

T0=$(date +%s)
onetemplate instantiate notes --name langsam \
  --user-inputs DB_PASSWORD=lab-db-pass-123,API_KEY=lab-api-key-123,NOTES_VERSION=0.7.1
sleep 15
IP=$(onevm show langsam | grep -o 'ETH0_IP="[0-9.]*' | cut -d'"' -f2)
until curl -s -m2 $IP:8080/version >/dev/null; do sleep 2; done
echo "Sekunden: $(( $(date +%s) - T0 ))"

Expected (roughly):

VM ID: 33
Sekunden: 72

Ten seconds faster; that sounds like little. Most of the minute is spent copying the disk, booting, and one-context. Something else matters more: notes-schnell downloads nothing from the internet at start. If the package source or the container registry goes down, it still starts, and every VM is guaranteed to have the same software.

onevm terminate --hard schnell
onevm terminate --hard langsam

Save your progress? Sign up for free

Checkpoint

oneimage list | grep notes-basis

Expected: notes-basis, type OS, STAT rdy.

diff ~/one-lab/tag10/notes-schnell.sh ~/one-lab/code/loesung/tag10/notes-schnell.sh

Expected: no output.

Challenge

Team blau should be able to start notes-api on its own. Share notes-schnell with it and have blau-dev create a VM. What can the team reach in the intern network afterwards, and is that a problem?

Solution

onetemplate chmod notes-schnell 604
oneimage chmod notes-basis 604
A="--user blau-dev --password lab-blau-pass-123"
onetemplate instantiate notes-schnell --name blau-notes \
  --user-inputs DB_PASSWORD=x,API_KEY=y,NOTES_VERSION=0.7.1 $A
onevm list $A

Expected (shortened):

VM ID: 34
  ID USER     GROUP    NAME            STAT  CPU     MEM HOST             TIME
  34 blau-dev team-bla blau-notes      prol  0.5    384M localhost    0d 00h00

It works, and that is exactly the problem: the VM is attached to the intern network, because the template says so, and the ACL from Day 8 allows team blau all networks. That means the team can reach the database’s port 5432. Only the password still protects it.

Tenants belong in their own networks. In the lab, you revoke the sharing:

onevm terminate --hard blau-notes $A
onetemplate chmod notes-schnell 600
oneimage chmod notes-basis 600

A permanent solution is a dedicated VDC that only gives team blau the vnet network (Day 8, Enterprise Note), or a network ACL only for vnet.

Save your progress? Sign up for free

Enterprise Note

  • Build images instead of clicking: In production, appliance images are built reproducibly, for example with Packer and the OpenNebula plugin, or with the tools from the one-apps repository that OpenNebula uses to build its own appliances.
  • Clean up before saving: A saved image contains everything: SSH host keys, logs, machine-id, shell history. That has to go before disk-saveas, so that not all VMs have the same host keys.
  • Your own marketplaces: Images can be shared between zones and sites via a private marketplace (HTTP or S3). onemarketapp create uploads an image there, onemarketapp export fetches it at another site.