Skip to content

Commit 8e7efed

Browse files
committed
add "dependency" provisioning mode
Signed-off-by: Justin Alvarez <[email protected]>
1 parent e57ac78 commit 8e7efed

File tree

10 files changed

+116
-63
lines changed

10 files changed

+116
-63
lines changed

examples/default.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ containerd:
199199
# - mode: boot
200200
# script: |
201201
# systemctl disable NetworkManager-wait-online.service
202+
# # `dependency` is executed before the regular dependency resolution workflow in
203+
# # pkg/cidata/cidata.TEMPLATE.d/boot/30-install-packages.sh
204+
# # If skipDefaultDependencyResolution is set on at least one `dependency` mode provisioning script, the regular
205+
# # dependency resolution workflow in pkg/cidata/cidata.TEMPLATE.d/boot/30-install-packages.sh will be skipped.
206+
# - mode: dependency
207+
# skipDefaultDependencyResolution: false
208+
# script: |
209+
# #!/bin/bash
210+
# dnf config-manager --add-repo ...
211+
# dnf install ...
202212

203213
# Probe scripts to check readiness.
204214
# 🟢 Builtin default: null

pkg/cidata/cidata.TEMPLATE.d/boot/09-host-dns-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22
set -eux
33

4-
# Wait until iptables has been installed; 30-install-packages.sh will call this script again
4+
# Wait until iptables has been installed; 35-configure-packages.sh will call this script again
55
if command -v iptables >/dev/null 2>&1; then
66
if [ -n "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" ] && [ "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" -ne 0 ]; then
77
# Only add the rule once

pkg/cidata/cidata.TEMPLATE.d/boot/30-install-packages.sh

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
#!/bin/sh
22
set -eux
33

4-
update_fuse_conf() {
5-
# Modify /etc/fuse.conf (/etc/fuse3.conf) to allow "-o allow_root"
6-
if [ "${LIMA_CIDATA_MOUNTS}" -gt 0 ]; then
7-
fuse_conf="/etc/fuse.conf"
8-
if [ -e /etc/fuse3.conf ]; then
9-
fuse_conf="/etc/fuse3.conf"
10-
fi
11-
if ! grep -q "^user_allow_other" "${fuse_conf}"; then
12-
echo "user_allow_other" >>"${fuse_conf}"
13-
fi
14-
fi
15-
}
16-
174
INSTALL_IPTABLES=0
185
if [ "${LIMA_CIDATA_CONTAINERD_SYSTEM}" = 1 ] || [ "${LIMA_CIDATA_CONTAINERD_USER}" = 1 ]; then
196
INSTALL_IPTABLES=1
@@ -23,11 +10,30 @@ if [ "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" -ne 0 ] || [ "${LIMA_CIDATA_TCP_DNS_LOC
2310
fi
2411

2512
# Install minimum dependencies
13+
# Run any user provided dependency scripts first
14+
if [ -d "${LIMA_CIDATA_MNT}"/provision.dependency ]; then
15+
echo "Detected dependency provisioning scripts, running before default dependency installation"
16+
CODE=0
17+
for f in "${LIMA_CIDATA_MNT}"/provision.dependency/*; do
18+
if ! "$f"; then
19+
CODE=1
20+
fi
21+
done
22+
if [ $CODE != 0 ]; then
23+
exit "$CODE"
24+
fi
25+
fi
26+
2627
# apt-get detected through the first bytes of apt-get binary to ensure we're
2728
# matching to an actual binary and not a wrapper script. This case is an issue
2829
# on OpenSuse which wraps its own package manager in to a script named apt-get
2930
# to mimic certain options but doesn't offer full parameters compatibility
3031
# See : https://github.com/lima-vm/lima/pull/1014
32+
if [ "${LIMA_CIDATA_SKIP_DEFAULT_DEPENDENCY_RESOLUTION}" = 1 ]; then
33+
echo "LIMA_CIDATA_SKIP_DEFAULT_DEPENDENCY_RESOLUTION is set, skipping regular dependency installation"
34+
exit 0
35+
fi
36+
3137
if hexdump -C -n 4 "$(command -v apt-get)" | grep -qF 'ELF' >/dev/null 2>&1; then
3238
pkgs=""
3339
if [ "${LIMA_CIDATA_MOUNTTYPE}" = "reverse-sshfs" ]; then
@@ -168,21 +174,3 @@ elif command -v apk >/dev/null 2>&1; then
168174
apk add ${pkgs}
169175
fi
170176
fi
171-
172-
SETUP_DNS=0
173-
if [ -n "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" ] && [ "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" -ne 0 ]; then
174-
SETUP_DNS=1
175-
fi
176-
if [ -n "${LIMA_CIDATA_TCP_DNS_LOCAL_PORT}" ] && [ "${LIMA_CIDATA_TCP_DNS_LOCAL_PORT}" -ne 0 ]; then
177-
SETUP_DNS=1
178-
fi
179-
if [ "${SETUP_DNS}" = 1 ]; then
180-
# Try to setup iptables rule again, in case we just installed iptables
181-
"${LIMA_CIDATA_MNT}/boot/09-host-dns-setup.sh"
182-
fi
183-
184-
# update_fuse_conf has to be called after installing all the packages,
185-
# otherwise apt-get fails with conflict
186-
if [ "${LIMA_CIDATA_MOUNTTYPE}" = "reverse-sshfs" ]; then
187-
update_fuse_conf
188-
fi
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
set -eux
3+
4+
update_fuse_conf() {
5+
# Modify /etc/fuse.conf (/etc/fuse3.conf) to allow "-o allow_root"
6+
if [ "${LIMA_CIDATA_MOUNTS}" -gt 0 ]; then
7+
fuse_conf="/etc/fuse.conf"
8+
if [ -e /etc/fuse3.conf ]; then
9+
fuse_conf="/etc/fuse3.conf"
10+
fi
11+
if ! grep -q "^user_allow_other" "${fuse_conf}"; then
12+
echo "user_allow_other" >>"${fuse_conf}"
13+
fi
14+
fi
15+
}
16+
17+
SETUP_DNS=0
18+
if [ -n "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" ] && [ "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" -ne 0 ]; then
19+
SETUP_DNS=1
20+
fi
21+
if [ -n "${LIMA_CIDATA_TCP_DNS_LOCAL_PORT}" ] && [ "${LIMA_CIDATA_TCP_DNS_LOCAL_PORT}" -ne 0 ]; then
22+
SETUP_DNS=1
23+
fi
24+
if [ "${SETUP_DNS}" = 1 ]; then
25+
# Try to setup iptables rule again, in case we just installed iptables
26+
"${LIMA_CIDATA_MNT}/boot/09-host-dns-setup.sh"
27+
fi
28+
29+
# update_fuse_conf has to be called after installing all the packages,
30+
# otherwise apt-get fails with conflict
31+
if [ "${LIMA_CIDATA_MOUNTTYPE}" = "reverse-sshfs" ]; then
32+
update_fuse_conf
33+
fi

pkg/cidata/cidata.TEMPLATE.d/lima.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ LIMA_CIDATA_UDP_DNS_LOCAL_PORT={{.UDPDNSLocalPort}}
2929
LIMA_CIDATA_TCP_DNS_LOCAL_PORT={{.TCPDNSLocalPort}}
3030
LIMA_CIDATA_ROSETTA_ENABLED={{.RosettaEnabled}}
3131
LIMA_CIDATA_ROSETTA_BINFMT={{.RosettaBinFmt}}
32+
{{- if .SkipDefaultDependencyResolution}}
33+
LIMA_CIDATA_SKIP_DEFAULT_DEPENDENCY_RESOLUTION=1
34+
{{- else}}
35+
LIMA_CIDATA_SKIP_DEFAULT_DEPENDENCY_RESOLUTION=
36+
{{- end}}

pkg/cidata/cidata.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
262262

263263
args.BootCmds = getBootCmds(y.Provision)
264264

265+
for _, f := range y.Provision {
266+
if f.Mode == limayaml.ProvisionModeDependency && *f.SkipDefaultDependencyResolution {
267+
args.SkipDefaultDependencyResolution = true
268+
}
269+
}
270+
265271
if err := ValidateTemplateArgs(args); err != nil {
266272
return err
267273
}
@@ -273,7 +279,7 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
273279

274280
for i, f := range y.Provision {
275281
switch f.Mode {
276-
case limayaml.ProvisionModeSystem, limayaml.ProvisionModeUser:
282+
case limayaml.ProvisionModeSystem, limayaml.ProvisionModeUser, limayaml.ProvisionModeDependency:
277283
layout = append(layout, iso9660util.Entry{
278284
Path: fmt.Sprintf("provision.%s/%08d", f.Mode, i),
279285
Reader: strings.NewReader(f.Script),

pkg/cidata/template.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,30 @@ type Disk struct {
5050
Device string
5151
}
5252
type TemplateArgs struct {
53-
Name string // instance name
54-
IID string // instance id
55-
User string // user name
56-
UID int
57-
SSHPubKeys []string
58-
Mounts []Mount
59-
MountType string
60-
Disks []Disk
61-
Containerd Containerd
62-
Networks []Network
63-
SlirpNICName string
64-
SlirpGateway string
65-
SlirpDNS string
66-
SlirpIPAddress string
67-
UDPDNSLocalPort int
68-
TCPDNSLocalPort int
69-
Env map[string]string
70-
DNSAddresses []string
71-
CACerts CACerts
72-
HostHomeMountPoint string
73-
BootCmds []BootCmds
74-
RosettaEnabled bool
75-
RosettaBinFmt bool
53+
Name string // instance name
54+
IID string // instance id
55+
User string // user name
56+
UID int
57+
SSHPubKeys []string
58+
Mounts []Mount
59+
MountType string
60+
Disks []Disk
61+
Containerd Containerd
62+
Networks []Network
63+
SlirpNICName string
64+
SlirpGateway string
65+
SlirpDNS string
66+
SlirpIPAddress string
67+
UDPDNSLocalPort int
68+
TCPDNSLocalPort int
69+
Env map[string]string
70+
DNSAddresses []string
71+
CACerts CACerts
72+
HostHomeMountPoint string
73+
BootCmds []BootCmds
74+
RosettaEnabled bool
75+
RosettaBinFmt bool
76+
SkipDefaultDependencyResolution bool
7677
}
7778

7879
func ValidateTemplateArgs(args TemplateArgs) error {

pkg/limayaml/defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
308308
if provision.Mode == "" {
309309
provision.Mode = ProvisionModeSystem
310310
}
311+
if provision.Mode == ProvisionModeDependency && provision.SkipDefaultDependencyResolution == nil {
312+
provision.SkipDefaultDependencyResolution = pointer.Bool(false)
313+
}
311314
}
312315

313316
if y.Containerd.System == nil {

pkg/limayaml/limayaml.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,16 @@ type Video struct {
141141
type ProvisionMode = string
142142

143143
const (
144-
ProvisionModeSystem ProvisionMode = "system"
145-
ProvisionModeUser ProvisionMode = "user"
146-
ProvisionModeBoot ProvisionMode = "boot"
144+
ProvisionModeSystem ProvisionMode = "system"
145+
ProvisionModeUser ProvisionMode = "user"
146+
ProvisionModeBoot ProvisionMode = "boot"
147+
ProvisionModeDependency ProvisionMode = "dependency"
147148
)
148149

149150
type Provision struct {
150-
Mode ProvisionMode `yaml:"mode" json:"mode"` // default: "system"
151-
Script string `yaml:"script" json:"script"`
151+
Mode ProvisionMode `yaml:"mode" json:"mode"` // default: "system"
152+
SkipDefaultDependencyResolution *bool `yaml:"skipDefaultDependencyResolution,omitempty" json:"skipDefaultDependencyResolution,omitempty"`
153+
Script string `yaml:"script" json:"script"`
152154
}
153155

154156
type Containerd struct {

pkg/limayaml/validate.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,14 @@ func Validate(y LimaYAML, warn bool) error {
153153
for i, p := range y.Provision {
154154
switch p.Mode {
155155
case ProvisionModeSystem, ProvisionModeUser, ProvisionModeBoot:
156+
if p.SkipDefaultDependencyResolution != nil {
157+
return fmt.Errorf("field `provision[%d].mode` cannot set skipDefaultDependencyResolution, only valid on scripts of type %q",
158+
i, ProvisionModeDependency)
159+
}
160+
case ProvisionModeDependency:
156161
default:
157-
return fmt.Errorf("field `provision[%d].mode` must be either %q, %q, or %q",
158-
i, ProvisionModeSystem, ProvisionModeUser, ProvisionModeBoot)
162+
return fmt.Errorf("field `provision[%d].mode` must one of %q, %q, %q, or %q",
163+
i, ProvisionModeSystem, ProvisionModeUser, ProvisionModeBoot, ProvisionModeDependency)
159164
}
160165
}
161166
needsContainerdArchives := (y.Containerd.User != nil && *y.Containerd.User) || (y.Containerd.System != nil && *y.Containerd.System)

0 commit comments

Comments
 (0)