github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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/packaging/manager"
    12  
    13  	"github.com/juju/juju/container"
    14  	"github.com/juju/juju/version"
    15  )
    16  
    17  var requiredPackages = []string{
    18  	"uvtool-libvirt",
    19  	"uvtool",
    20  }
    21  
    22  type containerInitialiser struct{}
    23  
    24  // containerInitialiser implements container.Initialiser.
    25  var _ container.Initialiser = (*containerInitialiser)(nil)
    26  
    27  // NewContainerInitialiser returns an instance used to perform the steps
    28  // required to allow a host machine to run a KVM container.
    29  func NewContainerInitialiser() container.Initialiser {
    30  	return &containerInitialiser{}
    31  }
    32  
    33  // Initialise is specified on the container.Initialiser interface.
    34  func (ci *containerInitialiser) Initialise() error {
    35  	return ensureDependencies()
    36  }
    37  
    38  // getPackageManager is a helper function which returns the
    39  // package manager implementation for the current system.
    40  func getPackageManager() (manager.PackageManager, error) {
    41  	return manager.NewPackageManager(version.Current.Series)
    42  }
    43  
    44  func ensureDependencies() error {
    45  	pacman, err := getPackageManager()
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	for _, pack := range requiredPackages {
    51  		if err := pacman.Install(pack); err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  const kvmNeedsUbuntu = `Sorry, KVM support with the local provider is only supported
    60  on the Ubuntu OS.`
    61  
    62  const kvmNotSupported = `KVM is not currently supported with the current settings.
    63  You could try running 'kvm-ok' yourself as root to get the full rationale as to
    64  why it isn't supported, or potentially some BIOS settings to change to enable
    65  KVM support.`
    66  
    67  const needToInstallKVMOk = `kvm-ok is not installed. Please install the cpu-checker package.
    68      sudo apt-get install cpu-checker`
    69  
    70  const missingKVMDeps = `Some required packages are missing for KVM to work:
    71  
    72      sudo apt-get install %s
    73  `
    74  
    75  // VerifyKVMEnabled makes sure that the host OS is Ubuntu, and that the required
    76  // packages are installed, and that the host CPU is able to support KVM.
    77  func VerifyKVMEnabled() error {
    78  	pacman, err := getPackageManager()
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	if !utils.IsUbuntu() {
    84  		return fmt.Errorf(kvmNeedsUbuntu)
    85  	}
    86  	supported, err := IsKVMSupported()
    87  	if err != nil {
    88  		// Missing the kvm-ok package.
    89  		return fmt.Errorf(needToInstallKVMOk)
    90  	}
    91  	if !supported {
    92  		return fmt.Errorf(kvmNotSupported)
    93  	}
    94  	// Check for other packages needed.
    95  	toInstall := []string{}
    96  	for _, pkg := range requiredPackages {
    97  		if !pacman.IsInstalled(pkg) {
    98  			toInstall = append(toInstall, pkg)
    99  		}
   100  	}
   101  	if len(toInstall) > 0 {
   102  		return fmt.Errorf(missingKVMDeps, strings.Join(toInstall, " "))
   103  	}
   104  	return nil
   105  }