Split linuxkit run -data options into string and file

Signed-off-by: Avi Deitcher <avi@deitcher.net>
This commit is contained in:
Avi Deitcher 2018-01-15 14:56:40 +02:00
parent 62d4858a3e
commit 6c015141f5
6 changed files with 40 additions and 23 deletions

View File

@ -33,7 +33,7 @@ $ linuxkit build -name docker-for-mac base.yml docker-ce.yml
To run the VM with a 4G disk: To run the VM with a 4G disk:
``` ```
linuxkit run hyperkit -networking=vpnkit -vsock-ports=2376 -disk size=4096M -data ./metadata.json docker-for-mac linuxkit run hyperkit -networking=vpnkit -vsock-ports=2376 -disk size=4096M -data-file ./metadata.json docker-for-mac
``` ```
In another terminal you should now be able to access docker via the socket `guest.00000947` in the state directory (`docker-for-mac-state/` by default): In another terminal you should now be able to access docker via the socket `guest.00000947` in the state directory (`docker-for-mac-state/` by default):

View File

@ -65,9 +65,12 @@ needed.
# Metadata image creation # Metadata image creation
Run `linuxkit run` backends accept a `-data=STRING` option which will `linuxkit run` backends accept two options to pass metadata to the VM in a platform specific
cause the given string to be passed to the VM in a platform specific manner to be picked up by the `pkg/metadata` component:
manner to be picked up by the `pkg/metadata` component.
* `-data=STRING` will cause the given `STRING` to be passed to the VM
* `-data-file=PATH` will cause the contents of the file at `PATH` to be passed to the VM
Alternatively `linuxkit metadata create meta.iso STRING` will produce Alternatively `linuxkit metadata create meta.iso STRING` will produce
a correctly formatted ISO image which can be passed to a VM as a CDROM a correctly formatted ISO image which can be passed to a VM as a CDROM

View File

@ -166,7 +166,7 @@ there are a number of packages, such as `vsudd`, which enable
tighter integration of the VM with the host (see below). tighter integration of the VM with the host (see below).
The HyperKit backend also allows passing custom userdata into the The HyperKit backend also allows passing custom userdata into the
[metadata package](./metadata.md) using the `-data` command-line [metadata package](./metadata.md) using either the `-data` or `-data-file` command-line
option. This attaches a CD device with the data on. option. This attaches a CD device with the data on.

View File

@ -42,7 +42,13 @@ func runHyperKit(args []string) {
mem := flags.Int("mem", 1024, "Amount of memory in MB") mem := flags.Int("mem", 1024, "Amount of memory in MB")
var disks Disks var disks Disks
flags.Var(&disks, "disk", "Disk config. [file=]path[,size=1G]") flags.Var(&disks, "disk", "Disk config. [file=]path[,size=1G]")
data := flags.String("data", "", "Metadata to pass to VM (either a path to a file or a string)") data := flags.String("data", "", "String of metadata to pass to VM; error to specify both -data and -data-file")
dataPath := flags.String("data-file", "", "Path to file containing metadata to pass to VM; error to specify both -data and -data-file")
if *data != "" && *dataPath != "" {
log.Fatal("Cannot specify both -data and -data-file")
}
ipStr := flags.String("ip", "", "Preferred IPv4 address for the VM.") ipStr := flags.String("ip", "", "Preferred IPv4 address for the VM.")
state := flags.String("state", "", "Path to directory to keep VM state in") state := flags.String("state", "", "Path to directory to keep VM state in")
vsockports := flags.String("vsock-ports", "", "List of vsock ports to forward from the guest on startup (comma separated). A unix domain socket for each port will be created in the state directory") vsockports := flags.String("vsock-ports", "", "List of vsock ports to forward from the guest on startup (comma separated). A unix domain socket for each port will be created in the state directory")
@ -142,7 +148,7 @@ func runHyperKit(args []string) {
log.Fatalf("Could not create state directory: %v", err) log.Fatalf("Could not create state directory: %v", err)
} }
metadataPaths, err := CreateMetadataISO(*state, *data) metadataPaths, err := CreateMetadataISO(*state, *data, *dataPath)
if err != nil { if err != nil {
log.Fatalf("%v", err) log.Fatalf("%v", err)
} }

View File

@ -148,7 +148,12 @@ func runQemu(args []string) {
// Paths and settings for disks // Paths and settings for disks
var disks Disks var disks Disks
flags.Var(&disks, "disk", "Disk config, may be repeated. [file=]path[,size=1G][,format=qcow2]") flags.Var(&disks, "disk", "Disk config, may be repeated. [file=]path[,size=1G][,format=qcow2]")
data := flags.String("data", "", "Metadata to pass to VM (either a path to a file or a string)") data := flags.String("data", "", "String of metadata to pass to VM; error to specify both -data and -data-file")
dataPath := flags.String("data-file", "", "Path to file containing metadata to pass to VM; error to specify both -data and -data-file")
if *data != "" && *dataPath != "" {
log.Fatal("Cannot specify both -data and -data-file")
}
// Paths and settings for UEFI firware // Paths and settings for UEFI firware
// Note, we do not use defaultFWPath here as we have a special case for containerised execution // Note, we do not use defaultFWPath here as we have a special case for containerised execution
@ -223,7 +228,7 @@ func runQemu(args []string) {
isoPaths = append(isoPaths, path) isoPaths = append(isoPaths, path)
} }
metadataPaths, err := CreateMetadataISO(*state, *data) metadataPaths, err := CreateMetadataISO(*state, *data, *dataPath)
if err != nil { if err != nil {
log.Fatalf("%v", err) log.Fatalf("%v", err)
} }

View File

@ -256,22 +256,25 @@ func NewPublishedPort(publish string) (PublishedPort, error) {
} }
// CreateMetadataISO writes the provided meta data to an iso file in the given state directory // CreateMetadataISO writes the provided meta data to an iso file in the given state directory
func CreateMetadataISO(state, data string) ([]string, error) { func CreateMetadataISO(state, data string, dataPath string) ([]string, error) {
if data == "" { var d []byte
// if we have neither data nor dataPath, nothing to return
switch {
case data != "" && dataPath != "":
return nil, fmt.Errorf("Cannot specify options for both data and dataPath")
case data == "" && dataPath == "":
return []string{}, nil return []string{}, nil
case data != "":
d = []byte(data)
case dataPath != "":
var err error
d, err = ioutil.ReadFile(dataPath)
if err != nil {
return nil, fmt.Errorf("Cannot read user data from path %s: %v", dataPath, err)
}
} }
var d []byte
if st, err := os.Stat(data); os.IsNotExist(err) {
d = []byte(data)
} else if st.Size() == 0 {
return []string{}, nil
} else {
d, err = ioutil.ReadFile(data)
if err != nil {
return nil, fmt.Errorf("Cannot read user data: %v", err)
}
}
isoPath := filepath.Join(state, "data.iso") isoPath := filepath.Join(state, "data.iso")
if err := WriteMetadataISO(isoPath, d); err != nil { if err := WriteMetadataISO(isoPath, d); err != nil {
return nil, fmt.Errorf("Cannot write user data ISO: %v", err) return nil, fmt.Errorf("Cannot write user data ISO: %v", err)