-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-vm
executable file
·118 lines (102 loc) · 3.21 KB
/
build-vm
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
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env ruby
require 'fileutils'
require 'getoptlong'
class Builder
def initialize
@basepath = File.expand_path('../', __FILE__)
@vmformat = 'raw'
@mirror = 'http://cdn.debian.net/debian'
@vzver = "042stab053.5"
@outfile = nil
end
def usage
puts <<-EOF
build-vm [OPTION] ... FILE
-h, --help Show this help
--format [format] Select VM disk format (raw [def], qcow2, vmdk)
--mirror [url] Debian mirror to use
(def: #{@mirror})
FILE: output filename
EOF
end
def run
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--format', GetoptLong::REQUIRED_ARGUMENT ],
[ '--mirror', GetoptLong::REQUIRED_ARGUMENT ],
)
opts.each do |opt, arg|
case opt
when '--help'
usage
exit 0
when '--format'
@vmformat = arg
when '--mirror'
@mirror = arg
end
end
outfile = ARGV.shift
if outfile.nil? || outfile.empty?
usage
exit 1
end
@outfile = File.expand_path(outfile)
puts "HostingStack/hs-build build-vm"
puts " * Using basepath \"#{@basepath}\""
puts " * Writing VM image to \"#{@outfile}\""
prepare_kernels
build_vm
puts " * Done."
end
def system(cmdline)
Kernel.system(cmdline)
raise "Command \"#{cmdline}\" failed with error code #{$?.exitstatus}" unless $?.exitstatus == 0
end
def prepare_kernels
kernels = File.join(@basepath, 'tmp/kernels')
puts " * Preparing OpenVZ Kernels ..."
FileUtils.mkdir_p kernels
def download_vzkernel(path, rpm)
return if File.exist?(File.join(path, rpm))
system "cd \"#{path}\" && wget http://download.openvz.org/kernel/branches/rhel6-2.6.32/#{@vzver}/#{rpm}"
end
def make_vzkernel(path, rpm, deb)
return if File.exist?(File.join(path, deb))
download_vzkernel path, rpm
system "cd \"#{path}\" && fakeroot alien -k -d #{rpm}"
end
make_vzkernel kernels, "vzkernel-2.6.32-#{@vzver}.x86_64.rpm", "vzkernel_2.6.32-#{@vzver}_amd64.deb"
make_vzkernel kernels, "vzkernel-devel-2.6.32-#{@vzver}.x86_64.rpm", "vzkernel-devel_2.6.32-#{@vzver}_amd64.deb"
end
def build_vm
post_scripts = File.join(@basepath, 'util/build/post-scripts')
packages = File.join(@basepath, 'util/build/packages')
puts " * Building VM image ..."
vmfile = @outfile
if @vmformat != 'raw'
vmfile = vmfile + '.tmp'
end
cmdline = "sudo HS_BASEPATH=\"#{@basepath}\" grml-debootstrap --vmfile "
cmdline << " -r wheezy --arch amd64 --filesystem ext3 --force"
cmdline << " --vmsize 4G --hostname hsdemo --password CHANGEME"
cmdline << " -t #{vmfile}"
cmdline << " --mirror #{@mirror}"
cmdline << " --scripts #{post_scripts}"
cmdline << " --packages #{packages}"
puts " * Running \"#{cmdline}\" ..."
system(cmdline)
if @vmformat != 'raw'
puts " * Converting to format #{@vmformat} ..."
system("qemu-img convert -p -f raw -O #{@vmformat} #{vmfile} #{@outfile}")
File.unlink vmfile
end
end
end
begin
Builder.new.run
rescue => e
puts e
exit 1
end
exit 0