| 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 main |
| 9 | |
| 10 | import ( |
| 11 | "flag" |
| 12 | "log" |
| 13 | |
| 14 | "github.com/lxc/go-lxc" |
| 15 | ) |
| 16 | |
| 17 | var ( |
| 18 | lxcpath string |
| 19 | template string |
| 20 | distro string |
| 21 | release string |
| 22 | arch string |
| 23 | name string |
| 24 | verbose bool |
| 25 | flush bool |
| 26 | validation bool |
| 27 | fssize string |
| 28 | bdevtype string |
| 29 | ) |
| 30 | |
| 31 | func init() { |
| 32 | flag.StringVar(&lxcpath, "lxcpath", lxc.DefaultConfigPath(), "Use specified container path") |
| 33 | flag.StringVar(&template, "template", "download", "Template to use") |
| 34 | flag.StringVar(&distro, "distro", "ubuntu", "Template to use") |
| 35 | flag.StringVar(&release, "release", "trusty", "Template to use") |
| 36 | flag.StringVar(&arch, "arch", "amd64", "Template to use") |
| 37 | flag.StringVar(&name, "name", "rubik", "Name of the container") |
| 38 | flag.BoolVar(&verbose, "verbose", false, "Verbose output") |
| 39 | flag.BoolVar(&flush, "flush", false, "Flush the cache") |
| 40 | flag.BoolVar(&validation, "validation", false, "GPG validation") |
| 41 | |
| 42 | flag.StringVar(&bdevtype, "bdev", "dir", "backing store type") |
| 43 | flag.StringVar(&fssize, "fssize", "", "backing store size") |
| 44 | // TODO support more flags for zfs, lvm, or rbd |
| 45 | |
| 46 | flag.Parse() |
| 47 | } |
| 48 | |
| 49 | func main() { |
| 50 | c, err := lxc.NewContainer(name, lxcpath) |
| 51 | if err != nil { |
| 52 | log.Fatalf("ERROR: %s\n", err.Error()) |
| 53 | } |
| 54 | defer c.Release() |
| 55 | |
| 56 | log.Printf("Creating container...\n") |
| 57 | if verbose { |
| 58 | c.SetVerbosity(lxc.Verbose) |
| 59 | } |
| 60 | |
| 61 | var backend lxc.BackendStore |
| 62 | if err := (&backend).Set(bdevtype); err != nil { |
| 63 | log.Fatalf("ERROR: %s\n", err.Error()) |
| 64 | } |
| 65 | |
| 66 | var bdevSize lxc.ByteSize |
| 67 | if fssize != "" { |
| 68 | var err error |
| 69 | bdevSize, err = lxc.ParseBytes(fssize) |
| 70 | if err != nil { |
| 71 | log.Fatalf("ERROR: %s\n", err.Error()) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | options := lxc.TemplateOptions{ |
| 76 | Template: template, |
| 77 | Distro: distro, |
| 78 | Release: release, |
| 79 | Arch: arch, |
| 80 | FlushCache: flush, |
| 81 | DisableGPGValidation: validation, |
| 82 | Backend: backend, |
| 83 | BackendSpecs: &lxc.BackendStoreSpecs{ |
| 84 | FSSize: uint64(bdevSize), |
| 85 | }, |
| 86 | } |
| 87 | |
| 88 | c.SetLogFile("log") |
| 89 | c.SetLogLevel(lxc.DEBUG) |
| 90 | |
| 91 | if err := c.Create(options); err != nil { |
| 92 | log.Printf("ERROR: %s\n", err.Error()) |
| 93 | } |
| 94 | } |