| 1 | // Copyright © 2013, 2014, The Go-LXC Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a LGPLv2.1 |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | //go:build linux && cgo |
| 6 | // +build linux,cgo |
| 7 | |
| 8 | package mainscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/ |
| 9 | |
| 10 | import ( |
| 11 | "flagscip-go gomod github.com/golang/go/src go1.20 flag/" |
| 12 | "logscip-go gomod github.com/golang/go/src go1.20 log/" |
| 13 | |
| 14 | "github.com/lxc/go-lxcscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/" |
| 15 | ) |
| 16 | |
| 17 | var ( |
| 18 | lxcpathscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/lxcpath. string |
| 19 | templatescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/template. string |
| 20 | distroscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/distro. string |
| 21 | releasescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/release. string |
| 22 | archscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/arch. string |
| 23 | namescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/name. string |
| 24 | verbosescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/verbose. bool |
| 25 | flushscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/flush. bool |
| 26 | validationscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/validation. bool |
| 27 | fssizescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/fssize. string |
| 28 | bdevtypescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/bdevtype. string |
| 29 | ) |
| 30 | |
| 31 | func initscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/init().() { |
| 32 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.StringVarscip-go gomod github.com/golang/go/src go1.20 flag/StringVar().(&lxcpathscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/lxcpath., "lxcpath", lxcscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/.DefaultConfigPathscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/DefaultConfigPath().(), "Use specified container path") |
| 33 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.StringVarscip-go gomod github.com/golang/go/src go1.20 flag/StringVar().(&templatescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/template., "template", "download", "Template to use") |
| 34 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.StringVarscip-go gomod github.com/golang/go/src go1.20 flag/StringVar().(&distroscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/distro., "distro", "ubuntu", "Template to use") |
| 35 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.StringVarscip-go gomod github.com/golang/go/src go1.20 flag/StringVar().(&releasescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/release., "release", "trusty", "Template to use") |
| 36 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.StringVarscip-go gomod github.com/golang/go/src go1.20 flag/StringVar().(&archscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/arch., "arch", "amd64", "Template to use") |
| 37 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.StringVarscip-go gomod github.com/golang/go/src go1.20 flag/StringVar().(&namescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/name., "name", "rubik", "Name of the container") |
| 38 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.BoolVarscip-go gomod github.com/golang/go/src go1.20 flag/BoolVar().(&verbosescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/verbose., "verbose", false, "Verbose output") |
| 39 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.BoolVarscip-go gomod github.com/golang/go/src go1.20 flag/BoolVar().(&flushscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/flush., "flush", false, "Flush the cache") |
| 40 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.BoolVarscip-go gomod github.com/golang/go/src go1.20 flag/BoolVar().(&validationscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/validation., "validation", false, "GPG validation") |
| 41 | |
| 42 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.StringVarscip-go gomod github.com/golang/go/src go1.20 flag/StringVar().(&bdevtypescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/bdevtype., "bdev", "dir", "backing store type") |
| 43 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.StringVarscip-go gomod github.com/golang/go/src go1.20 flag/StringVar().(&fssizescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/fssize., "fssize", "", "backing store size") |
| 44 | // TODO support more flags for zfs, lvm, or rbd |
| 45 | |
| 46 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.Parsescip-go gomod github.com/golang/go/src go1.20 flag/Parse().() |
| 47 | } |
| 48 | |
| 49 | func mainscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/main().() { |
| 50 | c, err := lxcscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/.NewContainerscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/NewContainer().(namescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/name., lxcpathscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/lxcpath.) |
| 51 | if err != nil { |
| 52 | logscip-go gomod github.com/golang/go/src go1.20 log/.Fatalfscip-go gomod github.com/golang/go/src go1.20 log/Fatalf().("ERROR: %s\n", err.Error()) |
| 53 | } |
| 54 | defer c.Releasescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/Container#Release().() |
| 55 | |
| 56 | logscip-go gomod github.com/golang/go/src go1.20 log/.Printfscip-go gomod github.com/golang/go/src go1.20 log/Printf().("Creating container...\n") |
| 57 | if verbosescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/verbose. { |
| 58 | c.SetVerbosityscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/Container#SetVerbosity().(lxcscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/.Verbosescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/Verbose.) |
| 59 | } |
| 60 | |
| 61 | var backend lxcscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/.BackendStorescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/BackendStore# |
| 62 | if err := (&backend).Setscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/BackendStore#Set().(bdevtypescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/bdevtype.); err != nil { |
| 63 | logscip-go gomod github.com/golang/go/src go1.20 log/.Fatalfscip-go gomod github.com/golang/go/src go1.20 log/Fatalf().("ERROR: %s\n", err.Error()) |
| 64 | } |
| 65 | |
| 66 | var bdevSize lxcscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/.ByteSizescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/ByteSize# |
| 67 | if fssizescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/fssize. != "" { |
| 68 | var err error |
| 69 | bdevSize, err = lxcscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/.ParseBytesscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/ParseBytes().(fssizescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/fssize.) |
| 70 | if err != nil { |
| 71 | logscip-go gomod github.com/golang/go/src go1.20 log/.Fatalfscip-go gomod github.com/golang/go/src go1.20 log/Fatalf().("ERROR: %s\n", err.Error()) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | options := lxcscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/.TemplateOptionsscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/TemplateOptions#{ |
| 76 | Templatescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/TemplateOptions#Template.: templatescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/template., |
| 77 | Distroscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/TemplateOptions#Distro.: distroscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/distro., |
| 78 | Releasescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/TemplateOptions#Release.: releasescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/release., |
| 79 | Archscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/TemplateOptions#Arch.: archscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/arch., |
| 80 | FlushCachescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/TemplateOptions#FlushCache.: flushscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/flush., |
| 81 | DisableGPGValidationscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/TemplateOptions#DisableGPGValidation.: validationscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc/examples/create`/validation., |
| 82 | Backendscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/TemplateOptions#Backend.: backend, |
| 83 | BackendSpecsscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/TemplateOptions#BackendSpecs.: &lxcscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/.BackendStoreSpecsscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/BackendStoreSpecs#{ |
| 84 | FSSizescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/BackendStoreSpecs#FSSize.: uint64(bdevSize), |
| 85 | }, |
| 86 | } |
| 87 | |
| 88 | c.SetLogFilescip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/Container#SetLogFile().("log") |
| 89 | c.SetLogLevelscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/Container#SetLogLevel().(lxcscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/.DEBUGscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/DEBUG.) |
| 90 | |
| 91 | if err := c.Createscip-go gomod github.com/lxc/go-lxc . `github.com/lxc/go-lxc`/Container#Create().(options); err != nil { |
| 92 | logscip-go gomod github.com/golang/go/src go1.20 log/.Printfscip-go gomod github.com/golang/go/src go1.20 log/Printf().("ERROR: %s\n", err.Error()) |
| 93 | } |
| 94 | } |