github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/container/kvm/initialisation.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package kvm
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/juju/utils"
    11  	"github.com/juju/utils/apt"
    12  
    13  	"github.com/juju/juju/container"
    14  )
    15  
    16  var requiredPackages = []string{
    17  	"uvtool-libvirt",
    18  	"uvtool",
    19  }
    20  
    21  type containerInitialiser struct{}
    22  
    23  // containerInitialiser implements container.Initialiser.
    24  var _ container.Initialiser = (*containerInitialiser)(nil)
    25  
    26  // NewContainerInitialiser returns an instance used to perform the steps
    27  // required to allow a host machine to run a KVM container.
    28  func NewContainerInitialiser() container.Initialiser {
    29  	return &containerInitialiser{}
    30  }
    31  
    32  // Initialise is specified on the container.Initialiser interface.
    33  func (ci *containerInitialiser) Initialise() error {
    34  	return ensureDependencies()
    35  }
    36  
    37  func ensureDependencies() error {
    38  	return apt.GetInstall(requiredPackages...)
    39  }
    40  
    41  const kvmNeedsUbuntu = `Sorry, KVM support with the local provider is only supported
    42  on the Ubuntu OS.`
    43  
    44  const kvmNotSupported = `KVM is not currently supported with the current settings.
    45  You could try running 'kvm-ok' yourself as root to get the full rationale as to
    46  why it isn't supported, or potentially some BIOS settings to change to enable
    47  KVM support.`
    48  
    49  const neetToInstallKVMOk = `kvm-ok is not installed. Please install the cpu-checker package.
    50      sudo apt-get install cpu-checker`
    51  
    52  const missingKVMDeps = `Some required packages are missing for KVM to work:
    53  
    54      sudo apt-get install %s
    55  `
    56  
    57  // VerifyKVMEnabled makes sure that the host OS is Ubuntu, and that the required
    58  // packages are installed, and that the host CPU is able to support KVM.
    59  func VerifyKVMEnabled() error {
    60  	if !utils.IsUbuntu() {
    61  		return fmt.Errorf(kvmNeedsUbuntu)
    62  	}
    63  	supported, err := IsKVMSupported()
    64  	if err != nil {
    65  		// Missing the kvm-ok package.
    66  		return fmt.Errorf(neetToInstallKVMOk)
    67  	}
    68  	if !supported {
    69  		return fmt.Errorf(kvmNotSupported)
    70  	}
    71  	// Check for other packages needed.
    72  	toInstall := []string{}
    73  	for _, pkg := range requiredPackages {
    74  		if !apt.IsPackageInstalled(pkg) {
    75  			toInstall = append(toInstall, pkg)
    76  		}
    77  	}
    78  	if len(toInstall) > 0 {
    79  		return fmt.Errorf(missingKVMDeps, strings.Join(toInstall, " "))
    80  	}
    81  	return nil
    82  }