Add a --distro option

This is meant to allow the user to create toolbox containers of
operating system distributions that are different from that of the
host and isn't Fedora.

https://github.com/containers/toolbox/pull/667
This commit is contained in:
Ondřej Míchal 2021-01-07 21:35:20 +01:00 committed by Debarshi Ray
parent c990fb43ca
commit 32d711cc64
8 changed files with 73 additions and 17 deletions

View File

@ -5,6 +5,10 @@ __toolbox_containers() {
podman ps --all --format '{{.Names}}' podman ps --all --format '{{.Names}}'
} }
__toolbox_distros() {
echo "fedora"
}
__toolbox_images() { __toolbox_images() {
podman images --format '{{.Repository}}:{{.Tag}}' podman images --format '{{.Repository}}:{{.Tag}}'
} }
@ -16,14 +20,14 @@ __toolbox() {
local commands="create enter help init-container list reset rm rmi run" local commands="create enter help init-container list reset rm rmi run"
declare -A options declare -A options
local options=([create]="--container --image --release" \ local options=([create]="--container --distro --image --release" \
[enter]="--container --release" \ [enter]="--container --distro --release" \
[help]="$commands" \ [help]="$commands" \
[init-container]="--home --home-link --monitor-host --shell --uid --user" \ [init-container]="--home --home-link --monitor-host --shell --uid --user" \
[list]="--containers --images" \ [list]="--containers --images" \
[rm]="--all --force" \ [rm]="--all --force" \
[rmi]="--all --force" \ [rmi]="--all --force" \
[run]="--container --release") [run]="--container --distro --release")
_init_completion -s || return _init_completion -s || return
@ -41,6 +45,10 @@ __toolbox() {
mapfile -t COMPREPLY < <(compgen -W "$(__toolbox_containers)" -- "$2") mapfile -t COMPREPLY < <(compgen -W "$(__toolbox_containers)" -- "$2")
return 0 return 0
;; ;;
--distro | -d)
mapfile -t COMPREPLY < <(compgen -W "$(__toolbox_distros)" -- "$2")
return 0
;;
--image | -i) --image | -i)
mapfile -t COMPREPLY < <(compgen -W "$(__toolbox_images)" -- "$2") mapfile -t COMPREPLY < <(compgen -W "$(__toolbox_images)" -- "$2")
return 0 return 0

View File

@ -5,6 +5,7 @@ toolbox\-create - Create a new toolbox container
## SYNOPSIS ## SYNOPSIS
**toolbox create** [*--container NAME* | *-c NAME*] **toolbox create** [*--container NAME* | *-c NAME*]
[*--distro DISTRO* | *-d DISTRO*]
[*--image NAME* | *-i NAME*] [*--image NAME* | *-i NAME*]
[*--release RELEASE* | *-r RELEASE*] [*--release RELEASE* | *-r RELEASE*]
@ -15,9 +16,10 @@ to interact with the container at any point.
A toolbox container is an OCI container created from an OCI image. On Fedora A toolbox container is an OCI container created from an OCI image. On Fedora
the base image is known as `fedora-toolbox`. If the image is not present the base image is known as `fedora-toolbox`. If the image is not present
locally, then it is pulled from `registry.fedoraproject.org`. The base image is locally, then it is pulled from a well-known registry like
locally customized for the current user to create a second image, from which `registry.fedoraproject.org`. The base image is locally customized for the
the container is finally created. current user to create a second image, from which the container is finally
created.
Toolbox containers and images are tagged with the version of the OS that Toolbox containers and images are tagged with the version of the OS that
corresponds to the content inside them. The user-specific images and the corresponds to the content inside them. The user-specific images and the
@ -34,6 +36,11 @@ Assign a different NAME to the toolbox container. This is useful for creating
multiple toolbox containers from the same base image, or for entirely multiple toolbox containers from the same base image, or for entirely
customized containers from custom-built base images. customized containers from custom-built base images.
**--distro** DISTRO, **-d** DISTRO
Create a toolbox container for a different operating system DISTRO than the
host. Cannot be used with `--image`.
**--image** NAME, **-i** NAME **--image** NAME, **-i** NAME
Change the NAME of the base image used to create the toolbox container. This Change the NAME of the base image used to create the toolbox container. This
@ -56,7 +63,7 @@ $ toolbox create
### Create a toolbox container using the default image for Fedora 30 ### Create a toolbox container using the default image for Fedora 30
``` ```
$ toolbox create --release f30 $ toolbox create --distro fedora --release f30
``` ```
### Create a custom toolbox container from a custom image ### Create a custom toolbox container from a custom image

View File

@ -5,6 +5,7 @@ toolbox\-enter - Enter a toolbox container for interactive use
## SYNOPSIS ## SYNOPSIS
**toolbox enter** [*--container NAME* | *-c NAME*] **toolbox enter** [*--container NAME* | *-c NAME*]
[*--distro DISTRO* | *-d DISTRO*]
[*--release RELEASE* | *-r RELEASE*] [*--release RELEASE* | *-r RELEASE*]
## DESCRIPTION ## DESCRIPTION
@ -18,7 +19,7 @@ fall back to it, even if it doesn't match the default name.
A toolbox container is an OCI container. Therefore, `toolbox enter` is A toolbox container is an OCI container. Therefore, `toolbox enter` is
analogous to a `podman start` followed by a `podman exec`. analogous to a `podman start` followed by a `podman exec`.
On Fedora the toolbox containers are tagged with the version of the OS that By default, the toolbox containers are tagged with the version of the OS that
corresponds to the content inside them. Their names are prefixed with the name corresponds to the content inside them. Their names are prefixed with the name
of the base image and suffixed with the current user name. of the base image and suffixed with the current user name.
@ -32,6 +33,11 @@ Enter a toolbox container with the given NAME. This is useful when there are
multiple toolbox containers created from the same base image, or entirely multiple toolbox containers created from the same base image, or entirely
customized containers created from custom-built base images. customized containers created from custom-built base images.
**--distro** DISTRO, **-d** DISTRO
Enter a toolbox container for a different operating system DISTRO than the
host.
**--release** RELEASE, **-r** RELEASE **--release** RELEASE, **-r** RELEASE
Enter a toolbox container for a different operating system RELEASE than the Enter a toolbox container for a different operating system RELEASE than the
@ -48,7 +54,7 @@ $ toolbox enter
### Enter a toolbox container using the default image for Fedora 30 ### Enter a toolbox container using the default image for Fedora 30
``` ```
$ toolbox enter --release f30 $ toolbox enter --distro fedora --release f30
``` ```
### Enter a custom toolbox container using a custom image ### Enter a custom toolbox container using a custom image

View File

@ -5,6 +5,7 @@ toolbox\-run - Run a command in an existing toolbox container
## SYNOPSIS ## SYNOPSIS
**toolbox run** [*--container NAME* | *-c NAME*] **toolbox run** [*--container NAME* | *-c NAME*]
[*--distro DISTRO* | *-d DISTRO*]
[*--release RELEASE* | *-r RELEASE*] [*COMMAND*] [*--release RELEASE* | *-r RELEASE*] [*COMMAND*]
## DESCRIPTION ## DESCRIPTION
@ -15,7 +16,7 @@ been created using the `toolbox create` command.
A toolbox container is an OCI container. Therefore, `toolbox run` is analogous A toolbox container is an OCI container. Therefore, `toolbox run` is analogous
to a `podman start` followed by a `podman exec`. to a `podman start` followed by a `podman exec`.
On Fedora the toolbox containers are tagged with the version of the OS that By default, the toolbox containers are tagged with the version of the OS that
corresponds to the content inside them. Their names are prefixed with the name corresponds to the content inside them. Their names are prefixed with the name
of the base image and suffixed with the current user name. of the base image and suffixed with the current user name.
@ -29,6 +30,11 @@ Run command inside a toolbox container with the given NAME. This is useful
when there are multiple toolbox containers created from the same base image, when there are multiple toolbox containers created from the same base image,
or entirely customized containers created from custom-built base images. or entirely customized containers created from custom-built base images.
**--distro** DISTRO, **-d** DISTRO
Run command inside a toolbox container for a different operating system DISTRO
than the host.
**--release** RELEASE, **-r** RELEASE **--release** RELEASE, **-r** RELEASE
Run command inside a toolbox container for a different operating system Run command inside a toolbox container for a different operating system
@ -45,7 +51,7 @@ $ toolbox run ls -la
### Run emacs inside a toolbox container using the default image for Fedora 30 ### Run emacs inside a toolbox container using the default image for Fedora 30
``` ```
$ toolbox run --release f30 emacs $ toolbox run --distro fedora --release f30 emacs
``` ```
### Run uptime inside a custom toolbox container using a custom image ### Run uptime inside a custom toolbox container using a custom image

View File

@ -43,6 +43,7 @@ const (
var ( var (
createFlags struct { createFlags struct {
container string container string
distro string
image string image string
release string release string
} }
@ -71,6 +72,12 @@ func init() {
"", "",
"Assign a different name to the toolbox container") "Assign a different name to the toolbox container")
flags.StringVarP(&createFlags.distro,
"distro",
"d",
"",
"Create a toolbox container for a different operating system distribution than the host")
flags.StringVarP(&createFlags.image, flags.StringVarP(&createFlags.image,
"image", "image",
"i", "i",
@ -100,6 +107,10 @@ func create(cmd *cobra.Command, args []string) error {
return nil return nil
} }
if cmd.Flag("distro").Changed && cmd.Flag("image").Changed {
return errors.New("options --distro and --image cannot be used together")
}
if cmd.Flag("image").Changed && cmd.Flag("release").Changed { if cmd.Flag("image").Changed && cmd.Flag("release").Changed {
return errors.New("options --image and --release cannot be used together") return errors.New("options --image and --release cannot be used together")
} }
@ -130,7 +141,7 @@ func create(cmd *cobra.Command, args []string) error {
var release string var release string
if createFlags.release != "" { if createFlags.release != "" {
var err error var err error
release, err = utils.ParseRelease("", createFlags.release) release, err = utils.ParseRelease(createFlags.distro, createFlags.release)
if err != nil { if err != nil {
err := utils.CreateErrorInvalidRelease(executableBase) err := utils.CreateErrorInvalidRelease(executableBase)
return err return err
@ -138,7 +149,7 @@ func create(cmd *cobra.Command, args []string) error {
} }
container, image, release, err := utils.ResolveContainerAndImageNames(container, container, image, release, err := utils.ResolveContainerAndImageNames(container,
"", createFlags.distro,
createFlags.image, createFlags.image,
release) release)
if err != nil { if err != nil {

View File

@ -29,6 +29,7 @@ import (
var ( var (
enterFlags struct { enterFlags struct {
container string container string
distro string
release string release string
} }
) )
@ -48,6 +49,12 @@ func init() {
"", "",
"Enter a toolbox container with the given name") "Enter a toolbox container with the given name")
flags.StringVarP(&enterFlags.distro,
"distro",
"d",
"",
"Enter a toolbox container for a different operating system distribution than the host")
flags.StringVarP(&enterFlags.release, flags.StringVarP(&enterFlags.release,
"release", "release",
"r", "r",
@ -102,14 +109,14 @@ func enter(cmd *cobra.Command, args []string) error {
nonDefaultContainer = true nonDefaultContainer = true
var err error var err error
release, err = utils.ParseRelease("", enterFlags.release) release, err = utils.ParseRelease(enterFlags.distro, enterFlags.release)
if err != nil { if err != nil {
err := utils.CreateErrorInvalidRelease(executableBase) err := utils.CreateErrorInvalidRelease(executableBase)
return err return err
} }
} }
container, image, release, err := utils.ResolveContainerAndImageNames(container, "", "", release) container, image, release, err := utils.ResolveContainerAndImageNames(container, enterFlags.distro, "", release)
if err != nil { if err != nil {
return err return err
} }

View File

@ -33,6 +33,7 @@ import (
var ( var (
runFlags struct { runFlags struct {
container string container string
distro string
release string release string
} }
) )
@ -53,6 +54,12 @@ func init() {
"", "",
"Run command inside a toolbox container with the given name") "Run command inside a toolbox container with the given name")
flags.StringVarP(&runFlags.distro,
"distro",
"d",
"",
"Run command inside a toolbox container for a different operating system distribution than the host")
flags.StringVarP(&runFlags.release, flags.StringVarP(&runFlags.release,
"release", "release",
"r", "r",
@ -97,7 +104,7 @@ func run(cmd *cobra.Command, args []string) error {
nonDefaultContainer = true nonDefaultContainer = true
var err error var err error
release, err = utils.ParseRelease("", runFlags.release) release, err = utils.ParseRelease(runFlags.distro, runFlags.release)
if err != nil { if err != nil {
err := utils.CreateErrorInvalidRelease(executableBase) err := utils.CreateErrorInvalidRelease(executableBase)
return err return err
@ -115,7 +122,7 @@ func run(cmd *cobra.Command, args []string) error {
command := args command := args
container, image, release, err := utils.ResolveContainerAndImageNames(runFlags.container, "", "", release) container, image, release, err := utils.ResolveContainerAndImageNames(runFlags.container, runFlags.distro, "", release)
if err != nil { if err != nil {
return err return err
} }

View File

@ -676,6 +676,10 @@ func ResolveContainerAndImageNames(container, distro, image, release string) (st
distro = distroDefault distro = distroDefault
} }
if distro != distroDefault && release == "" {
return "", "", "", fmt.Errorf("release not found for non-default distribution %s", distro)
}
if release == "" { if release == "" {
release = releaseDefault release = releaseDefault
} }