Skip to content

Commit 28c3710

Browse files
authored
Merge pull request #1845 from gwenya/oci-entrypoint-config
OCI entrypoint configuration
2 parents af52378 + 6b79d07 commit 28c3710

File tree

7 files changed

+184
-12
lines changed

7 files changed

+184
-12
lines changed

cmd/incusd/instance.go

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sync"
1313
"time"
1414

15+
"github.com/kballard/go-shellquote"
1516
ociSpecs "github.com/opencontainers/runtime-spec/specs-go"
1617

1718
internalInstance "github.com/lxc/incus/v6/internal/instance"
@@ -245,6 +246,23 @@ func instanceCreateFromImage(ctx context.Context, s *state.State, r *http.Reques
245246
}
246247
}
247248

249+
// Set the entrypoint configuration options.
250+
if len(config.Process.Args) > 0 && args.Config["oci.entrypoint"] == "" {
251+
args.Config["oci.entrypoint"] = shellquote.Join(config.Process.Args...)
252+
}
253+
254+
if config.Process.Cwd != "" && args.Config["oci.cwd"] == "" {
255+
args.Config["oci.cwd"] = config.Process.Cwd
256+
}
257+
258+
if args.Config["oci.uid"] == "" {
259+
args.Config["oci.uid"] = fmt.Sprintf("%d", config.Process.User.UID)
260+
}
261+
262+
if args.Config["oci.gid"] == "" {
263+
args.Config["oci.gid"] = fmt.Sprintf("%d", config.Process.User.GID)
264+
}
265+
248266
err = inst.Update(args, false)
249267
if err != nil {
250268
return err

doc/api-extensions.md

+11
Original file line numberDiff line numberDiff line change
@@ -2764,3 +2764,14 @@ Adds a new `usb` value for `io.bus` on `disk` devices.
27642764
## `storage_driver_linstor`
27652765

27662766
This adds a LINSTOR storage driver.
2767+
2768+
## `instance_oci_entrypoint`
2769+
2770+
This introduces a set of new configuration options on the container to configure the OCI entry point:
2771+
2772+
* `oci.entrypoint`
2773+
* `oci.cwd`
2774+
* `oci.uid`
2775+
* `oci.gid`
2776+
2777+
Those are initialized at creation time using the values from the OCI image.

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
@@ -2344,33 +2344,65 @@ func (d *lxc) startCommon() (string, []func() error, error) {
23442344
}
23452345

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

2361-
err = lxcSetConfigItem(cc, "lxc.init.cwd", config.Process.Cwd)
2362-
if err != nil {
2363-
return "", nil, err
2369+
// Configure the cwd.
2370+
if d.expandedConfig["oci.cwd"] != "" {
2371+
err = lxcSetConfigItem(cc, "lxc.init.cwd", d.expandedConfig["oci.cwd"])
2372+
if err != nil {
2373+
return "", nil, err
2374+
}
2375+
} else {
2376+
err = lxcSetConfigItem(cc, "lxc.init.cwd", config.Process.Cwd)
2377+
if err != nil {
2378+
return "", nil, err
2379+
}
23642380
}
23652381

2366-
err = lxcSetConfigItem(cc, "lxc.init.uid", fmt.Sprintf("%d", config.Process.User.UID))
2367-
if err != nil {
2368-
return "", nil, err
2382+
// Configure the UID
2383+
if d.expandedConfig["oci.uid"] != "" {
2384+
err = lxcSetConfigItem(cc, "lxc.init.uid", d.expandedConfig["oci.uid"])
2385+
if err != nil {
2386+
return "", nil, err
2387+
}
2388+
} else {
2389+
err = lxcSetConfigItem(cc, "lxc.init.uid", fmt.Sprintf("%d", config.Process.User.UID))
2390+
if err != nil {
2391+
return "", nil, err
2392+
}
23692393
}
23702394

2371-
err = lxcSetConfigItem(cc, "lxc.init.gid", fmt.Sprintf("%d", config.Process.User.GID))
2372-
if err != nil {
2373-
return "", nil, err
2395+
// Configure the GID
2396+
if d.expandedConfig["oci.gid"] != "" {
2397+
err = lxcSetConfigItem(cc, "lxc.init.gid", d.expandedConfig["oci.gid"])
2398+
if err != nil {
2399+
return "", nil, err
2400+
}
2401+
} else {
2402+
err = lxcSetConfigItem(cc, "lxc.init.gid", fmt.Sprintf("%d", config.Process.User.GID))
2403+
if err != nil {
2404+
return "", nil, err
2405+
}
23742406
}
23752407

23762408
// 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
{

internal/version/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ var APIExtensions = []string{
475475
"network_io_bus",
476476
"disk_io_bus_usb",
477477
"storage_driver_linstor",
478+
"instance_oci_entrypoint",
478479
}
479480

480481
// APIExtensionsCount returns the number of available API extensions.

0 commit comments

Comments
 (0)