| 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 | name string |
| 20 | clear bool |
| 21 | x86 bool |
| 22 | regular bool |
| 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | flag.StringVar(&lxcpath, "lxcpath", lxc.DefaultConfigPath(), "Use specified container path") |
| 27 | flag.StringVar(&name, "name", "rubik", "Name of the original container") |
| 28 | flag.BoolVar(&clear, "clear", false, "Attach with clear environment") |
| 29 | flag.BoolVar(&x86, "x86", false, "Attach using x86 personality") |
| 30 | flag.BoolVar(®ular, "regular", false, "Attach using a regular user") |
| 31 | flag.Parse() |
| 32 | } |
| 33 | |
| 34 | func main() { |
| 35 | c, err := lxc.NewContainer(name, lxcpath) |
| 36 | if err != nil { |
| 37 | log.Fatalf("ERROR: %s\n", err.Error()) |
| 38 | } |
| 39 | defer c.Release() |
| 40 | |
| 41 | options := lxc.DefaultAttachOptions |
| 42 | options.ClearEnv = false |
| 43 | if clear { |
| 44 | options.ClearEnv = true |
| 45 | } |
| 46 | if x86 { |
| 47 | options.Arch = lxc.X86 |
| 48 | } |
| 49 | if regular { |
| 50 | options.UID = 1000 |
| 51 | options.GID = 1000 |
| 52 | } |
| 53 | log.Printf("AttachShell\n") |
| 54 | err = c.AttachShell(options) |
| 55 | if err != nil { |
| 56 | log.Fatalf("ERROR: %s\n", err.Error()) |
| 57 | } |
| 58 | |
| 59 | log.Printf("RunCommand\n") |
| 60 | _, err = c.RunCommand([]string{"id"}, options) |
| 61 | if err != nil { |
| 62 | log.Fatalf("ERROR: %s\n", err.Error()) |
| 63 | } |
| 64 | } |