Class: Dcmgr::Drivers::Lxc

Inherits:
Hypervisor show all
Includes:
Helpers::CliHelper, Logger
Defined in:
lib/dcmgr/drivers/lxc.rb

Instance Method Summary collapse

Methods included from Helpers::CliHelper

#sh, #tryagain

Methods included from Logger

create, default_logdev, included

Methods inherited from Hypervisor

select_hypervisor

Instance Method Details

#attach_volume_to_guest(ctx) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dcmgr/drivers/lxc.rb', line 77

def attach_volume_to_guest(ctx)
  sddev = File.expand_path(File.readlink(ctx.os_devpath), '/dev/disk/by-path')

  # find major number and minor number to device file
  stat = File.stat(sddev)
  devnum = [stat.rdev_major,stat.rdev_minor].join(':')

  sh("echo \"b #{devnum} rwm\" > /cgroup/#{ctx.inst_id}/devices.allow")
  logger.debug("Makinging new block device: #{ctx.inst_data_dir}/rootfs#{sddev}")
  sh("mknod #{ctx.inst_data_dir}/rootfs#{sddev} -m 660 b #{stat.rdev_major} #{stat.rdev_minor}")

  config_path = "#{ctx.inst_data_dir}/config.#{ctx.inst_id}"
  File.open(config_path, 'a+') { |f|
    f.puts "lxc.cgroup.devices.allow = b #{devnum} rwm"
  }

  devnum
end

#detach_volume_from_guest(ctx) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dcmgr/drivers/lxc.rb', line 96

def detach_volume_from_guest(ctx)
  vol = ctx.vol
  sddev = File.expand_path(File.readlink(vol[:host_device_name]), '/dev/disk/by-path')
  devnum = vol[:guest_device_name]

  sh("echo \"b #{devnum} rwm\" > /cgroup/#{ctx.inst_id}/devices.deny")
  logger.debug("Deleting block device: #{ctx.inst_data_dir}/rootfs#{sddev}")
  sh("rm #{ctx.inst_data_dir}/rootfs#{sddev}")

  config_path = "#{ctx.inst_data_dir}/config.#{ctx.inst_id}"
  config_body = File.open(config_path, 'r') { |f|
    f.readlines.select {|line| line != "lxc.cgroup.devices.allow = b #{devnum} rwm\n" }
  }
  File.open(config_path, 'w') { |f|
    f.write config_body
  }
end

#reboot_instance(ctx) ⇒ Object



72
73
74
75
# File 'lib/dcmgr/drivers/lxc.rb', line 72

def reboot_instance(ctx)
  sh("lxc-stop -n #{ctx.inst[:uuid]}")
  sh("lxc-start -n %s -d -l DEBUG -o %s/%s.log 3<&- 4<&- 6<&-", [ctx.inst[:uuid], ctx.inst_data_dir, ctx.inst[:uuid]])
end

#run_instance(ctx) ⇒ Object



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
# File 'lib/dcmgr/drivers/lxc.rb', line 11

def run_instance(ctx)
  # run lxc
  @os_devpath = ctx.os_devpath
  if @os_devpath.nil?
    if ctx.inst[:image][:boot_dev_type] == 1
      ctx.inst[:volume].each{ |vol_id, vol|
        @os_devpath = vol[:host_device_name] if vol[:boot_dev] == 1
      }
    else
      @os_devpath = "#{ctx.inst_data_dir}/#{ctx.inst_id}"
    end
  end
  # check mount point
  mount_point = "#{ctx.inst_data_dir}/rootfs"
  Dir.mkdir(mount_point) unless File.exists?(mount_point)

  cmd = "mount %s %s"
  args = [@os_devpath, mount_point]
  if ctx.inst[:image][:boot_dev_type] == 2
    cmd += " -o loop"
  end
  sh(cmd, args)

  # metadata drive
   = "#{ctx.inst_data_dir}/rootfs/metadata"
  Dir.mkdir() unless File.exists?()
  sh("mount -t vfat -o loop -o ro #{ctx.} #{}")

  config_path = create_config(ctx)
  create_fstab(ctx)
  setup_container(ctx)
  mount_cgroup

  # Ubuntu 10.04.3 LTS
  # Linux ubuntu 2.6.38-10-generic #46~lucid1-Ubuntu SMP Wed Jul 6 18:40:11 UTC 2011 i686 GNU/Linux
  # Linux ubuntu 2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 GNU/Linux
  # lxc 0.7.4-0ubuntu7
  #
  # Ubuntu-10.04.3 on Virtualbox-4.0.12 r72916 on Windows-7
  # Ubuntu-10.10
  #
  # > lxc-start 1311803515.629 ERROR    lxc_start - inherited fd 3 on pipe:[58281]
  # > lxc-start 1311803515.629 ERROR    lxc_start - inherited fd 4 on pipe:[58281]
  # > lxc-start 1311803515.629 ERROR    lxc_start - inherited fd 6 on socket:[58286]
  #
  # http://comments.gmane.org/gmane.linux.kernel.containers.lxc.general/912
  # http://comments.gmane.org/gmane.linux.kernel.containers.lxc.general/1400
  lxc_version = `lxc-version`.chomp.split(': ').last
  logger.debug("lxc-version: #{lxc_version}")

  sh("lxc-create -f %s -n %s", [config_path, ctx.inst[:uuid]])
  sh("lxc-start -n %s -d -l DEBUG -o %s/%s.log 3<&- 4<&- 6<&-", [ctx.inst[:uuid], ctx.inst_data_dir, ctx.inst[:uuid]])
end

#terminate_instance(ctx) ⇒ Object



65
66
67
68
69
70
# File 'lib/dcmgr/drivers/lxc.rb', line 65

def terminate_instance(ctx)
  sh("lxc-stop -n #{ctx.inst_id}")
  sh("lxc-destroy -n #{ctx.inst_id}")
  sh("umount #{ctx.inst_data_dir}/rootfs/metadata")
  sh("umount #{ctx.inst_data_dir}/rootfs")
end