mirror of
				https://github.com/linuxkit/linuxkit.git
				synced 2025-10-31 01:37:42 +00:00 
			
		
		
		
	Merge pull request #1803 from ijc25/mkmetadata
Add `linuxkit metadata create` command
This commit is contained in:
		| @@ -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 | mount the config sub-directory into their namespace where it is | ||||||
| needed. | 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 | # Providers | ||||||
|  |  | ||||||
|   | |||||||
| @@ -41,6 +41,7 @@ func main() { | |||||||
| 	flag.Usage = func() { | 	flag.Usage = func() { | ||||||
| 		fmt.Printf("USAGE: %s [options] COMMAND\n\n", filepath.Base(os.Args[0])) | 		fmt.Printf("USAGE: %s [options] COMMAND\n\n", filepath.Base(os.Args[0])) | ||||||
| 		fmt.Printf("Commands:\n") | 		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("  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("  run         Run a VM image on a local hypervisor or remote cloud\n") | ||||||
| 		fmt.Printf("  version     Print version information\n") | 		fmt.Printf("  version     Print version information\n") | ||||||
| @@ -79,6 +80,8 @@ func main() { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	switch args[0] { | 	switch args[0] { | ||||||
|  | 	case "metadata": | ||||||
|  | 		metadata(args[1:]) | ||||||
| 	case "push": | 	case "push": | ||||||
| 		push(args[1:]) | 		push(args[1:]) | ||||||
| 	case "run": | 	case "run": | ||||||
|   | |||||||
							
								
								
									
										77
									
								
								src/cmd/linuxkit/metadata.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								src/cmd/linuxkit/metadata.go
									
									
									
									
									
										Normal 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) | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -10,7 +10,6 @@ import ( | |||||||
|  |  | ||||||
| 	log "github.com/Sirupsen/logrus" | 	log "github.com/Sirupsen/logrus" | ||||||
| 	"github.com/moby/hyperkit/go" | 	"github.com/moby/hyperkit/go" | ||||||
| 	"github.com/rneugeba/iso9660wrap" |  | ||||||
| 	"github.com/satori/go.uuid" | 	"github.com/satori/go.uuid" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -56,16 +55,8 @@ func runHyperKit(args []string) { | |||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		isoPath = prefix + "-data.iso" | 		isoPath = prefix + "-data.iso" | ||||||
| 		outfh, err := os.OpenFile(isoPath, os.O_CREATE|os.O_WRONLY, 0644) | 		if err := WriteMetadataISO(isoPath, d); err != nil { | ||||||
| 		if err != nil { | 			log.Fatalf("Cannot write user data ISO: %s", err) | ||||||
| 			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) |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user