diff --git a/docs/metadata.md b/docs/metadata.md index 17d37607e..bdfeeef9b 100644 --- a/docs/metadata.md +++ b/docs/metadata.md @@ -51,6 +51,15 @@ This hierarchy can then be used by individual containers, who can bind mount the config sub-directory into their namespace where it is needed. +# Metadata image creation + +Run `linuxkit run` backends accept a `--data=STRING` option which will +cause the given string to be passed to the VM in a platform specific +manner to be picked up by the `pkg/metadata` component. + +Alternatively `linuxkit metadata create meta.iso STRING` will produce +a correctly formatted ISO image which can be passed to a VM as a CDROM +device for consumption by the `pkg/metadata` component. # Providers diff --git a/src/cmd/linuxkit/main.go b/src/cmd/linuxkit/main.go index 76cba44e7..f4727e38b 100644 --- a/src/cmd/linuxkit/main.go +++ b/src/cmd/linuxkit/main.go @@ -41,6 +41,7 @@ func main() { flag.Usage = func() { fmt.Printf("USAGE: %s [options] COMMAND\n\n", filepath.Base(os.Args[0])) fmt.Printf("Commands:\n") + fmt.Printf(" metadata Metadata utilities\n") fmt.Printf(" push Push a VM image to a cloud or image store\n") fmt.Printf(" run Run a VM image on a local hypervisor or remote cloud\n") fmt.Printf(" version Print version information\n") @@ -79,6 +80,8 @@ func main() { } switch args[0] { + case "metadata": + metadata(args[1:]) case "push": push(args[1:]) case "run": diff --git a/src/cmd/linuxkit/metadata.go b/src/cmd/linuxkit/metadata.go new file mode 100644 index 000000000..82ac21a70 --- /dev/null +++ b/src/cmd/linuxkit/metadata.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + + log "github.com/Sirupsen/logrus" + "github.com/rneugeba/iso9660wrap" +) + +func metadataCreateUsage() { + invoked := filepath.Base(os.Args[0]) + fmt.Printf("USAGE: %s metadata create [file.iso] [metadata]\n\n", invoked) + + fmt.Printf("'file.iso' is the file to create.\n") + fmt.Printf("'metadata' will be written to '/config' in the ISO.\n") + fmt.Printf("This is compatible with the linuxkit/metadata package\n") +} + +func metadataCreate(args []string) { + if len(args) != 2 { + metadataCreateUsage() + os.Exit(1) + } + switch args[0] { + case "help", "-h", "-help", "--help": + metadataCreateUsage() + os.Exit(0) + } + + isoImage := args[0] + metadata := args[1] + + outfh, err := os.OpenFile(isoImage, os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + log.Fatal("Cannot create user data ISOs", "err", err) + } + defer outfh.Close() + + err = iso9660wrap.WriteBuffer(outfh, []byte(metadata), "config") + if err != nil { + log.Fatal("Cannot write user data ISO", "err", err) + } +} + +func metadataUsage() { + invoked := filepath.Base(os.Args[0]) + fmt.Printf("USAGE: %s metadata COMMAND [options]\n\n", invoked) + fmt.Printf("Commands:\n") + fmt.Printf(" create Create a metadata ISO\n") +} + +func metadata(args []string) { + if len(args) < 1 { + metadataUsage() + os.Exit(1) + } + switch args[0] { + case "help", "-h", "-help", "--help": + metadataUsage() + os.Exit(0) + } + + switch args[0] { + case "create": + metadataCreate(args[1:]) + default: + fmt.Printf("%q is not a valid metadata command.\n\n", args[0]) + metadataUsage() + os.Exit(1) + } +}