Skip to content

Commit ea75e98

Browse files
committed
incusd/instance/lxc: Add OCI entrypoint configuration
1 parent 00be2ea commit ea75e98

File tree

4 files changed

+154
-12
lines changed

4 files changed

+154
-12
lines changed

doc/config_options.txt

+34
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,40 @@ The specified version expression is used to set `libnvidia-container NVIDIA_REQU
665665
```
666666

667667
<!-- config group instance-nvidia end -->
668+
<!-- config group instance-oci start -->
669+
```{config:option} oci.cwd instance-oci
670+
:condition: "OCI container"
671+
:liveupdate: "no"
672+
:shortdesc: "OCI container working directory"
673+
:type: "string"
674+
Override the working directory of an OCI container.
675+
```
676+
677+
```{config:option} oci.entrypoint instance-oci
678+
:condition: "OCI container"
679+
:liveupdate: "no"
680+
:shortdesc: "OCI container entrypoint"
681+
:type: "string"
682+
Override the entrypoint of an OCI container.
683+
```
684+
685+
```{config:option} oci.gid instance-oci
686+
:condition: "OCI container"
687+
:liveupdate: "no"
688+
:shortdesc: "OCI container GID"
689+
:type: "string"
690+
Override the GID of the process run in an OCI container.
691+
```
692+
693+
```{config:option} oci.uid instance-oci
694+
:condition: "OCI container"
695+
:liveupdate: "no"
696+
:shortdesc: "OCI container UID"
697+
:type: "string"
698+
Override the UID of the process run in an OCI container.
699+
```
700+
701+
<!-- config group instance-oci end -->
668702
<!-- config group instance-raw start -->
669703
```{config:option} raw.apparmor instance-raw
670704
:liveupdate: "yes"

internal/instance/config.go

+36
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,42 @@ var InstanceConfigKeysContainer = map[string]func(value string) error{
652652
// shortdesc: Required driver version
653653
"nvidia.require.driver": validate.IsAny,
654654

655+
// gendoc:generate(entity=instance, group=oci, key=oci.entrypoint)
656+
// Override the entrypoint of an OCI container.
657+
// ---
658+
// type: string
659+
// liveupdate: no
660+
// condition: OCI container
661+
// shortdesc: OCI container entrypoint
662+
"oci.entrypoint": validate.IsAny,
663+
664+
// gendoc:generate(entity=instance, group=oci, key=oci.cwd)
665+
// Override the working directory of an OCI container.
666+
// ---
667+
// type: string
668+
// liveupdate: no
669+
// condition: OCI container
670+
// shortdesc: OCI container working directory
671+
"oci.cwd": validate.Optional(validate.IsAbsFilePath),
672+
673+
// gendoc:generate(entity=instance, group=oci, key=oci.gid)
674+
// Override the GID of the process run in an OCI container.
675+
// ---
676+
// type: string
677+
// liveupdate: no
678+
// condition: OCI container
679+
// shortdesc: OCI container GID
680+
"oci.gid": validate.Optional(validate.IsUint32),
681+
682+
// gendoc:generate(entity=instance, group=oci, key=oci.uid)
683+
// Override the UID of the process run in an OCI container.
684+
// ---
685+
// type: string
686+
// liveupdate: no
687+
// condition: OCI container
688+
// shortdesc: OCI container UID
689+
"oci.uid": validate.Optional(validate.IsUint32),
690+
655691
// Caller is responsible for full validation of any raw.* value.
656692

657693
// gendoc:generate(entity=instance, group=raw, key=raw.lxc)

internal/server/instance/drivers/driver_lxc.go

+44-12
Original file line numberDiff line numberDiff line change
@@ -2341,33 +2341,65 @@ func (d *lxc) startCommon() (string, []func() error, error) {
23412341
}
23422342

23432343
// Configure the entry point.
2344-
if len(config.Process.Args) > 0 && slices.Contains([]string{"/init", "/sbin/init", "/s6-init"}, config.Process.Args[0]) {
2344+
entrypoint := config.Process.Args
2345+
if d.expandedConfig["oci.entrypoint"] != "" {
2346+
entrypoint, err = shellquote.Split(d.expandedConfig["oci.entrypoint"])
2347+
if err != nil {
2348+
return "", nil, err
2349+
}
2350+
}
2351+
2352+
if len(entrypoint) > 0 && slices.Contains([]string{"/init", "/sbin/init", "/s6-init"}, entrypoint[0]) {
23452353
// For regular init systems, call them directly as PID1.
2346-
err = lxcSetConfigItem(cc, "lxc.init.cmd", shellquote.Join(config.Process.Args...))
2354+
err = lxcSetConfigItem(cc, "lxc.init.cmd", shellquote.Join(entrypoint...))
23472355
if err != nil {
23482356
return "", nil, err
23492357
}
23502358
} else {
23512359
// For anything else, run them under our own PID1.
2352-
err = lxcSetConfigItem(cc, "lxc.execute.cmd", shellquote.Join(config.Process.Args...))
2360+
err = lxcSetConfigItem(cc, "lxc.execute.cmd", shellquote.Join(entrypoint...))
23532361
if err != nil {
23542362
return "", nil, err
23552363
}
23562364
}
23572365

2358-
err = lxcSetConfigItem(cc, "lxc.init.cwd", config.Process.Cwd)
2359-
if err != nil {
2360-
return "", nil, err
2366+
// Configure the cwd.
2367+
if d.expandedConfig["oci.cwd"] != "" {
2368+
err = lxcSetConfigItem(cc, "lxc.init.cwd", d.expandedConfig["oci.cwd"])
2369+
if err != nil {
2370+
return "", nil, err
2371+
}
2372+
} else {
2373+
err = lxcSetConfigItem(cc, "lxc.init.cwd", config.Process.Cwd)
2374+
if err != nil {
2375+
return "", nil, err
2376+
}
23612377
}
23622378

2363-
err = lxcSetConfigItem(cc, "lxc.init.uid", fmt.Sprintf("%d", config.Process.User.UID))
2364-
if err != nil {
2365-
return "", nil, err
2379+
// Configure the UID
2380+
if d.expandedConfig["oci.uid"] != "" {
2381+
err = lxcSetConfigItem(cc, "lxc.init.uid", d.expandedConfig["oci.uid"])
2382+
if err != nil {
2383+
return "", nil, err
2384+
}
2385+
} else {
2386+
err = lxcSetConfigItem(cc, "lxc.init.uid", fmt.Sprintf("%d", config.Process.User.UID))
2387+
if err != nil {
2388+
return "", nil, err
2389+
}
23662390
}
23672391

2368-
err = lxcSetConfigItem(cc, "lxc.init.gid", fmt.Sprintf("%d", config.Process.User.GID))
2369-
if err != nil {
2370-
return "", nil, err
2392+
// Configure the GID
2393+
if d.expandedConfig["oci.gid"] != "" {
2394+
err = lxcSetConfigItem(cc, "lxc.init.gid", d.expandedConfig["oci.gid"])
2395+
if err != nil {
2396+
return "", nil, err
2397+
}
2398+
} else {
2399+
err = lxcSetConfigItem(cc, "lxc.init.gid", fmt.Sprintf("%d", config.Process.User.GID))
2400+
if err != nil {
2401+
return "", nil, err
2402+
}
23712403
}
23722404

23732405
// Get all mounts so far.

internal/server/metadata/configuration.json

+40
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,46 @@
730730
}
731731
]
732732
},
733+
"oci": {
734+
"keys": [
735+
{
736+
"oci.cwd": {
737+
"condition": "OCI container",
738+
"liveupdate": "no",
739+
"longdesc": "Override the working directory of an OCI container.",
740+
"shortdesc": "OCI container working directory",
741+
"type": "string"
742+
}
743+
},
744+
{
745+
"oci.entrypoint": {
746+
"condition": "OCI container",
747+
"liveupdate": "no",
748+
"longdesc": "Override the entrypoint of an OCI container.",
749+
"shortdesc": "OCI container entrypoint",
750+
"type": "string"
751+
}
752+
},
753+
{
754+
"oci.gid": {
755+
"condition": "OCI container",
756+
"liveupdate": "no",
757+
"longdesc": "Override the GID of the process run in an OCI container.",
758+
"shortdesc": "OCI container GID",
759+
"type": "string"
760+
}
761+
},
762+
{
763+
"oci.uid": {
764+
"condition": "OCI container",
765+
"liveupdate": "no",
766+
"longdesc": "Override the UID of the process run in an OCI container.",
767+
"shortdesc": "OCI container UID",
768+
"type": "string"
769+
}
770+
}
771+
]
772+
},
733773
"raw": {
734774
"keys": [
735775
{

0 commit comments

Comments
 (0)