Merge pull request #1803 from ijc25/mkmetadata

Add `linuxkit metadata create` command
This commit is contained in:
Rolf Neugebauer 2017-05-11 20:09:52 +01:00 committed by GitHub
commit 122267c6f8
4 changed files with 91 additions and 11 deletions

View File

@ -58,6 +58,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

View File

@ -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":

View File

@ -0,0 +1,77 @@
package main
import (
"fmt"
"os"
"path/filepath"
log "github.com/Sirupsen/logrus"
"github.com/rneugeba/iso9660wrap"
)
// WriteMetadataISO writes a metadata ISO file in a format usable by pkg/metadata
func WriteMetadataISO(path string, content []byte) error {
outfh, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer outfh.Close()
return iso9660wrap.WriteBuffer(outfh, content, "config")
}
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]
if err := WriteMetadataISO(isoImage, []byte(metadata)); err != nil {
log.Fatal("Failed to write user data ISO: ", 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)
}
}

View File

@ -10,7 +10,6 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/moby/hyperkit/go"
"github.com/rneugeba/iso9660wrap"
"github.com/satori/go.uuid"
)
@ -56,16 +55,8 @@ func runHyperKit(args []string) {
}
}
isoPath = prefix + "-data.iso"
outfh, err := os.OpenFile(isoPath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Cannot create user data ISO: %v", err)
}
err = iso9660wrap.WriteBuffer(outfh, d, "config")
if err != nil {
log.Fatalf("Cannot write user data ISO: %v", err)
}
if err = outfh.Close(); err != nil {
log.Fatalf("Cannot close output ISO: %v", err)
if err := WriteMetadataISO(isoPath, d); err != nil {
log.Fatalf("Cannot write user data ISO: %s", err)
}
}