/
initialisation.go
105 lines (85 loc) · 2.66 KB
/
initialisation.go
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
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package kvm
import (
"fmt"
"strings"
"github.com/juju/utils"
"github.com/juju/utils/packaging/manager"
"github.com/juju/juju/container"
"github.com/juju/juju/version"
)
var requiredPackages = []string{
"uvtool-libvirt",
"uvtool",
}
type containerInitialiser struct{}
// containerInitialiser implements container.Initialiser.
var _ container.Initialiser = (*containerInitialiser)(nil)
// NewContainerInitialiser returns an instance used to perform the steps
// required to allow a host machine to run a KVM container.
func NewContainerInitialiser() container.Initialiser {
return &containerInitialiser{}
}
// Initialise is specified on the container.Initialiser interface.
func (ci *containerInitialiser) Initialise() error {
return ensureDependencies()
}
// getPackageManager is a helper function which returns the
// package manager implementation for the current system.
func getPackageManager() (manager.PackageManager, error) {
return manager.NewPackageManager(version.Current.Series)
}
func ensureDependencies() error {
pacman, err := getPackageManager()
if err != nil {
return err
}
for _, pack := range requiredPackages {
if err := pacman.Install(pack); err != nil {
return err
}
}
return nil
}
const kvmNeedsUbuntu = `Sorry, KVM support with the local provider is only supported
on the Ubuntu OS.`
const kvmNotSupported = `KVM is not currently supported with the current settings.
You could try running 'kvm-ok' yourself as root to get the full rationale as to
why it isn't supported, or potentially some BIOS settings to change to enable
KVM support.`
const needToInstallKVMOk = `kvm-ok is not installed. Please install the cpu-checker package.
sudo apt-get install cpu-checker`
const missingKVMDeps = `Some required packages are missing for KVM to work:
sudo apt-get install %s
`
// VerifyKVMEnabled makes sure that the host OS is Ubuntu, and that the required
// packages are installed, and that the host CPU is able to support KVM.
func VerifyKVMEnabled() error {
pacman, err := getPackageManager()
if err != nil {
return err
}
if !utils.IsUbuntu() {
return fmt.Errorf(kvmNeedsUbuntu)
}
supported, err := IsKVMSupported()
if err != nil {
// Missing the kvm-ok package.
return fmt.Errorf(needToInstallKVMOk)
}
if !supported {
return fmt.Errorf(kvmNotSupported)
}
// Check for other packages needed.
toInstall := []string{}
for _, pkg := range requiredPackages {
if !pacman.IsInstalled(pkg) {
toInstall = append(toInstall, pkg)
}
}
if len(toInstall) > 0 {
return fmt.Errorf(missingKVMDeps, strings.Join(toInstall, " "))
}
return nil
}