Skip to content

Commit c7f0d23

Browse files
committed
renmae
1 parent f1ff1b7 commit c7f0d23

8 files changed

Lines changed: 87 additions & 67 deletions

File tree

.github/workflows/build-os-images.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ jobs:
151151
tags: ${{ steps.meta.outputs.tags }}
152152
labels: ${{ steps.meta.outputs.labels }}
153153
secret-files: |
154-
cocoon_script=os-image/${{ matrix.name }}/hack.sh
154+
cocoon_overlay=os-image/${{ matrix.name }}/overlay.sh
155+
cocoon_network=os-image/${{ matrix.name }}/network.sh
155156
no-cache: ${{ matrix.no_cache == 'true' }}
156157
cache-from: ${{ matrix.no_cache == 'true' && '' || format('type=gha,scope={0}-{1}', matrix.name, matrix.tag) }}
157158
cache-to: type=gha,mode=max,scope=${{ matrix.name }}-${{ matrix.tag }}

cmd/vm/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func printRunOCI(configs []*types.StorageConfig, boot *types.BootConfig, vmName,
358358
cocoonLayers := strings.Join(cloudhypervisor.ReverseLayerSerials(configs), ",")
359359

360360
cmdline := fmt.Sprintf(
361-
"console=hvc0 loglevel=3 boot=cocoon cocoon.layers=%s cocoon.cow=%s clocksource=kvm-clock rw",
361+
"console=hvc0 loglevel=3 boot=cocoon-overlay cocoon.layers=%s cocoon.cow=%s clocksource=kvm-clock rw",
362362
cocoonLayers, cloudhypervisor.CowSerial)
363363

364364
fmt.Println("# Prepare COW disk")

hypervisor/cloudhypervisor/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (ch *CloudHypervisor) prepareOCI(ctx context.Context, vmID string, vmCfg *t
136136
// Build cmdline with reversed layer serials for overlayfs lowerdir ordering (top layer first).
137137
var cmdline strings.Builder
138138
fmt.Fprintf(&cmdline,
139-
"console=hvc0 loglevel=3 boot=cocoon cocoon.layers=%s cocoon.cow=%s clocksource=kvm-clock rw",
139+
"console=hvc0 loglevel=3 boot=cocoon-overlay cocoon.layers=%s cocoon.cow=%s clocksource=kvm-clock rw",
140140
strings.Join(ReverseLayerSerials(storageConfigs), ","), CowSerial,
141141
)
142142

os-image/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ DISK_CONFIGS+=("path=cow.raw,readonly=off,direct=on,sparse=on,image_type=raw,num
109109
echo "[7/7] Igniting Cloud Hypervisor..."
110110
"$CH_BIN" --kernel "$KERNEL" --initramfs "$INITRD" \
111111
--disk "${DISK_CONFIGS[@]}" \
112-
--cmdline "console=ttyS0 loglevel=3 boot=cocoon cocoon.layers=${COCOON_LAYERS} cocoon.cow=cocoon-cow clocksource=kvm-clock rw" \
112+
--cmdline "console=ttyS0 loglevel=3 boot=cocoon-overlay cocoon.layers=${COCOON_LAYERS} cocoon.cow=cocoon-cow clocksource=kvm-clock rw" \
113113
--cpus "boot=2,max=8" \
114114
--memory "size=1024M$([ "$(cat /proc/sys/vm/nr_hugepages 2>/dev/null)" -gt 0 ] && echo ',hugepages=on')" \
115115
--rng "src=/dev/urandom" \

os-image/ubuntu/22.04/Dockerfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ ENV DEBIAN_FRONTEND=noninteractive
55

66
# Combined System Setup (Single Layer)
77
# Install packages first so /etc/initramfs-tools/scripts/ exists, then inject the hook.
8-
RUN --mount=type=secret,id=cocoon_script \
8+
RUN --mount=type=secret,id=cocoon_overlay \
9+
--mount=type=secret,id=cocoon_network \
910
apt-get update && apt-get install -y --no-install-recommends \
1011
linux-image-virtual \
1112
initramfs-tools \
@@ -16,8 +17,10 @@ RUN --mount=type=secret,id=cocoon_script \
1617
kmod \
1718
iproute2 \
1819
&& \
19-
cp /run/secrets/cocoon_script /etc/initramfs-tools/scripts/cocoon && \
20-
chmod 0755 /etc/initramfs-tools/scripts/cocoon && \
20+
cp /run/secrets/cocoon_overlay /etc/initramfs-tools/scripts/cocoon-overlay && \
21+
chmod 0755 /etc/initramfs-tools/scripts/cocoon-overlay && \
22+
cp /run/secrets/cocoon_network /etc/initramfs-tools/scripts/init-bottom/cocoon-network && \
23+
chmod 0755 /etc/initramfs-tools/scripts/init-bottom/cocoon-network && \
2124
# [Kernel Config]
2225
printf "erofs\noverlay\next4\nvirtio_blk\nvirtio_pci\nvirtio_ring\nvirtio_net\n" >> /etc/initramfs-tools/modules && \
2326
sed -i 's/^COMPRESS=.*/COMPRESS=gzip/' /etc/initramfs-tools/initramfs.conf && \

os-image/ubuntu/24.04/Dockerfile

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ ENV DEBIAN_FRONTEND=noninteractive
55

66
# Combined System Setup & Optimization (Single Layer)
77
# Install packages first so /etc/initramfs-tools/scripts/ exists, then inject the hook.
8-
RUN --mount=type=secret,id=cocoon_script \
8+
RUN --mount=type=secret,id=cocoon_overlay \
9+
--mount=type=secret,id=cocoon_network \
910
apt-get update && apt-get install -y --no-install-recommends \
10-
linux-image-virtual \
11-
initramfs-tools \
12-
systemd \
13-
systemd-sysv \
14-
systemd-timesyncd \
15-
systemd-resolved \
16-
udev \
17-
kmod \
18-
iproute2 \
11+
linux-image-virtual \
12+
initramfs-tools \
13+
systemd \
14+
systemd-sysv \
15+
systemd-timesyncd \
16+
systemd-resolved \
17+
udev \
18+
kmod \
19+
iproute2 iputils-ping curl wget arping\
1920
&& \
20-
cp /run/secrets/cocoon_script /etc/initramfs-tools/scripts/cocoon && \
21-
chmod 0755 /etc/initramfs-tools/scripts/cocoon && \
21+
cp /run/secrets/cocoon_overlay /etc/initramfs-tools/scripts/cocoon-overlay && \
22+
chmod 0755 /etc/initramfs-tools/scripts/cocoon-overlay && \
23+
cp /run/secrets/cocoon_network /etc/initramfs-tools/scripts/init-bottom/cocoon-network && \
24+
chmod 0755 /etc/initramfs-tools/scripts/init-bottom/cocoon-network && \
2225
# [Kernel Setup] Force critical modules and set gzip compression
2326
printf "erofs\noverlay\next4\nvirtio_blk\nvirtio_pci\nvirtio_ring\nvirtio_net\n" >> /etc/initramfs-tools/modules && \
2427
sed -i 's/^COMPRESS=.*/COMPRESS=gzip/' /etc/initramfs-tools/initramfs.conf && \

os-image/ubuntu/network.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/sh
2+
# Filename: network.sh
3+
# Target path: /etc/initramfs-tools/scripts/init-bottom/cocoon-network
4+
#
5+
# Runs in init-bottom phase — AFTER configure_networking has parsed kernel ip=
6+
# parameters into /run/net-*.conf, and AFTER mountroot has assembled the overlay.
7+
# Converts initramfs network config into systemd-networkd .network files so
8+
# the IP configuration persists after switch_root.
9+
10+
PREREQ=""
11+
prereqs() { echo "$PREREQ"; }
12+
case "$1" in prereqs) prereqs; exit 0 ;; esac
13+
14+
. /scripts/functions
15+
16+
# $rootmnt is set by initramfs — points to the mounted root filesystem.
17+
[ -z "$rootmnt" ] && exit 0
18+
19+
for conf_file in /run/net-*.conf; do
20+
[ -f "$conf_file" ] || continue
21+
22+
unset DEVICE IPV4ADDR IPV4NETMASK IPV4GATEWAY IPV4DNS0 IPV4DNS1 HOSTNAME
23+
. "$conf_file"
24+
[ -z "$DEVICE" ] || [ -z "$IPV4ADDR" ] && continue
25+
26+
# Convert dotted netmask to prefix length.
27+
prefix=0
28+
IFS=. read -r a b c d <<EOF
29+
${IPV4NETMASK}
30+
EOF
31+
for octet in $a $b $c $d; do
32+
case $octet in
33+
255) prefix=$((prefix + 8)) ;;
34+
254) prefix=$((prefix + 7)) ;;
35+
252) prefix=$((prefix + 6)) ;;
36+
248) prefix=$((prefix + 5)) ;;
37+
240) prefix=$((prefix + 4)) ;;
38+
224) prefix=$((prefix + 3)) ;;
39+
192) prefix=$((prefix + 2)) ;;
40+
128) prefix=$((prefix + 1)) ;;
41+
esac
42+
done
43+
44+
mkdir -p "${rootmnt}/etc/systemd/network"
45+
{
46+
printf "[Match]\nName=%s\n\n[Network]\nAddress=%s/%d\n" "$DEVICE" "$IPV4ADDR" "$prefix"
47+
[ -n "$IPV4GATEWAY" ] && [ "$IPV4GATEWAY" != "0.0.0.0" ] && printf "Gateway=%s\n" "$IPV4GATEWAY"
48+
[ -n "$IPV4DNS0" ] && [ "$IPV4DNS0" != "0.0.0.0" ] && printf "DNS=%s\n" "$IPV4DNS0"
49+
[ -n "$IPV4DNS1" ] && [ "$IPV4DNS1" != "0.0.0.0" ] && printf "DNS=%s\n" "$IPV4DNS1"
50+
# Fallback DNS if none provided.
51+
[ -z "$IPV4DNS0" ] || [ "$IPV4DNS0" = "0.0.0.0" ] && printf "DNS=8.8.8.8\nDNS=8.8.4.4\n"
52+
} > "${rootmnt}/etc/systemd/network/10-${DEVICE}.network"
53+
54+
# Set hostname from the first interface that has one.
55+
if [ -n "$HOSTNAME" ] && [ ! -f "${rootmnt}/etc/cocoon-hostname-set" ]; then
56+
echo "$HOSTNAME" > "${rootmnt}/etc/hostname"
57+
: > "${rootmnt}/etc/cocoon-hostname-set"
58+
fi
59+
done
Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
2-
# Filename: cocoon-boot.sh
3-
# Target path: /etc/initramfs-tools/scripts/cocoon
2+
# Filename: overlay.sh
3+
# Target path: /etc/initramfs-tools/scripts/cocoon-overlay
44

55
. /scripts/functions
66

@@ -105,51 +105,5 @@ mountroot() {
105105
rm -f "${rootmnt}/etc/machine-id" 2>/dev/null || true
106106
: > "${rootmnt}/etc/machine-id"
107107

108-
# Convert initramfs network config (/run/net-*.conf) to systemd-networkd.
109-
# The kernel ip= parameter is already parsed by initramfs configure_networking
110-
# into /run/net-{device}.conf files. We source them and generate .network files
111-
# so systemd-networkd picks up the config after switch_root.
112-
for conf_file in /run/net-*.conf; do
113-
[ -f "$conf_file" ] || continue
114-
# Each file defines: DEVICE, IPV4ADDR, IPV4NETMASK, IPV4GATEWAY, HOSTNAME, etc.
115-
unset DEVICE IPV4ADDR IPV4NETMASK IPV4GATEWAY IPV4DNS0 IPV4DNS1 HOSTNAME
116-
. "$conf_file"
117-
[ -z "$DEVICE" ] || [ -z "$IPV4ADDR" ] && continue
118-
119-
# Convert dotted netmask to prefix length.
120-
prefix=0
121-
IFS=. read -r a b c d <<EOF
122-
${IPV4NETMASK}
123-
EOF
124-
for octet in $a $b $c $d; do
125-
case $octet in
126-
255) prefix=$((prefix + 8)) ;;
127-
254) prefix=$((prefix + 7)) ;;
128-
252) prefix=$((prefix + 6)) ;;
129-
248) prefix=$((prefix + 5)) ;;
130-
240) prefix=$((prefix + 4)) ;;
131-
224) prefix=$((prefix + 3)) ;;
132-
192) prefix=$((prefix + 2)) ;;
133-
128) prefix=$((prefix + 1)) ;;
134-
esac
135-
done
136-
137-
mkdir -p "${rootmnt}/etc/systemd/network"
138-
{
139-
printf "[Match]\nName=%s\n\n[Network]\nAddress=%s/%d\n" "$DEVICE" "$IPV4ADDR" "$prefix"
140-
[ -n "$IPV4GATEWAY" ] && printf "Gateway=%s\n" "$IPV4GATEWAY"
141-
[ -n "$IPV4DNS0" ] && printf "DNS=%s\n" "$IPV4DNS0"
142-
[ -n "$IPV4DNS1" ] && printf "DNS=%s\n" "$IPV4DNS1"
143-
# Fallback DNS if none provided by kernel ip= parameter.
144-
[ -z "$IPV4DNS0" ] && printf "DNS=8.8.8.8\nDNS=8.8.4.4\n"
145-
} > "${rootmnt}/etc/systemd/network/10-${DEVICE}.network"
146-
147-
# Set hostname from the first interface that provides one.
148-
if [ -n "$HOSTNAME" ] && [ ! -f "${rootmnt}/etc/cocoon-hostname-set" ]; then
149-
echo "$HOSTNAME" > "${rootmnt}/etc/hostname"
150-
: > "${rootmnt}/etc/cocoon-hostname-set"
151-
fi
152-
done
153-
154108
log_success_msg "Cocoon: stealth overlay rootfs ready"
155109
}

0 commit comments

Comments
 (0)