Class: Dcmgr::Drivers::Lxc
Instance Method Summary
collapse
#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')
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)
@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
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_path = "#{ctx.inst_data_dir}/rootfs/metadata"
Dir.mkdir(metadata_path) unless File.exists?(metadata_path)
sh("mount -t vfat -o loop -o ro #{ctx.metadata_img_path} #{metadata_path}")
config_path = create_config(ctx)
create_fstab(ctx)
setup_container(ctx)
mount_cgroup
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
|