File: create.go

package info (click to toggle)
golang-gopkg-lxc-go-lxc.v2 0.0%2Bgit20220627.ad3d9f7-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 428 kB
  • sloc: ansic: 572; makefile: 77
file content (93 lines) | stat: -rw-r--r-- 2,279 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright © 2013, 2014, The Go-LXC Authors. All rights reserved.
// Use of this source code is governed by a LGPLv2.1
// license that can be found in the LICENSE file.

// +build linux,cgo

package main

import (
	"flag"
	"log"

	"github.com/lxc/go-lxc"
)

var (
	lxcpath    string
	template   string
	distro     string
	release    string
	arch       string
	name       string
	verbose    bool
	flush      bool
	validation bool
	fssize     string
	bdevtype   string
)

func init() {
	flag.StringVar(&lxcpath, "lxcpath", lxc.DefaultConfigPath(), "Use specified container path")
	flag.StringVar(&template, "template", "download", "Template to use")
	flag.StringVar(&distro, "distro", "ubuntu", "Template to use")
	flag.StringVar(&release, "release", "trusty", "Template to use")
	flag.StringVar(&arch, "arch", "amd64", "Template to use")
	flag.StringVar(&name, "name", "rubik", "Name of the container")
	flag.BoolVar(&verbose, "verbose", false, "Verbose output")
	flag.BoolVar(&flush, "flush", false, "Flush the cache")
	flag.BoolVar(&validation, "validation", false, "GPG validation")

	flag.StringVar(&bdevtype, "bdev", "dir", "backing store type")
	flag.StringVar(&fssize, "fssize", "", "backing store size")
	// TODO support more flags for zfs, lvm, or rbd

	flag.Parse()
}

func main() {
	c, err := lxc.NewContainer(name, lxcpath)
	if err != nil {
		log.Fatalf("ERROR: %s\n", err.Error())
	}
	defer c.Release()

	log.Printf("Creating container...\n")
	if verbose {
		c.SetVerbosity(lxc.Verbose)
	}

	var backend lxc.BackendStore
	if err := (&backend).Set(bdevtype); err != nil {
		log.Fatalf("ERROR: %s\n", err.Error())
	}

	var bdevSize lxc.ByteSize
	if fssize != "" {
		var err error
		bdevSize, err = lxc.ParseBytes(fssize)
		if err != nil {
			log.Fatalf("ERROR: %s\n", err.Error())
		}
	}

	options := lxc.TemplateOptions{
		Template:             template,
		Distro:               distro,
		Release:              release,
		Arch:                 arch,
		FlushCache:           flush,
		DisableGPGValidation: validation,
		Backend:              backend,
		BackendSpecs: &lxc.BackendStoreSpecs{
			FSSize: uint64(bdevSize),
		},
	}

	c.SetLogFile("log")
	c.SetLogLevel(lxc.DEBUG)

	if err := c.Create(options); err != nil {
		log.Printf("ERROR: %s\n", err.Error())
	}
}