-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
270 lines (230 loc) · 7.6 KB
/
Rakefile
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Rake tasks for building the various parts of the system
#
require_relative 'src/debootstrap_builder'
require_relative 'src/disk_builder_bios'
require_relative 'src/disk_builder_uefi'
require_relative 'src/iso_builder'
distro = "ubuntu" # or "debian"
livecd = false
verbose = ENV.has_key?('VERBOSE')
# Rake always ensures CWD is the dir containing the Rakefile
OUTPUT_DIR = 'output'
Dir.mkdir(OUTPUT_DIR) rescue Errno::EEXIST
# File names
DEBOOTSTRAP_CACHE_NAME = "debootstrap_cache.tgz"
ROOTFS_TAR_NAME = "rootfs.tar"
BIOS_VMDK_FILE_NAME = "bios_disk.vmdk"
UEFI_VMDK_FILE_NAME = "uefi_disk.vmdk"
LIVECD_ISO_FILE_NAME = "live-cd.iso"
# File paths (in the output dir)
DEBOOTSTRAP_CACHE_PATH = File.join(OUTPUT_DIR, DEBOOTSTRAP_CACHE_NAME)
ROOTFS_TAR_PATH = File.join(OUTPUT_DIR, ROOTFS_TAR_NAME)
BIOS_VMDK_FILE_PATH = File.join(OUTPUT_DIR, BIOS_VMDK_FILE_NAME)
UEFI_VMDK_FILE_PATH = File.join(OUTPUT_DIR, UEFI_VMDK_FILE_NAME)
LIVECD_ISO_FILE_PATH = File.join(OUTPUT_DIR, LIVECD_ISO_FILE_NAME)
# Helper to test if the env contains all the vars specified
def ensure_var(var)
if not ENV.has_key?(var)
puts("Please set env var: #{var}")
exit(1)
end
end
# Ensure one of the TMPFS_{DIR|SIZEMB} params are specified
def ensure_tmpfs_params()
if not ENV['TMPFS_SIZEMB'] and not ENV['TMPFS_DIR']
puts "One of TMPFS_SIZEMB or TMPFS_DIR must be specified"
puts "* These are used to create a temp dir or use an existing temp dir"
exit(1)
end
end
# Helper to ensure that env contains DEVICE or one of TMPFS_{DIR|SIZEMB}
def ensure_device_or_tmpfs_params()
if not ENV['DEVICE'] and not ENV['TMPFS_SIZEMB'] and not ENV['TMPFS_DIR']
puts "One of DEVICE or TMPFS_SIZEMB or TMPFS_DIR must be specified"
puts "* When DEVICE is specified, that block device is as workspace"
puts "* When TMPFS_{SZ|DIR} are specified, a loopback device is on a tmpfs"
exit(1)
end
end
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Prerequisite software check tasks
#
namespace :prereqs do
desc "Check if prerequisite software is present"
task :check do
{
# tool: pkgs_that_provides_it_on_xenial
'blockdev': 'util-linux',
'debootstrap': 'debootstrap',
'fallocate': 'util-linux',
'losetup': 'mount',
'mkfs.ext4': 'e2fsprogs',
'parted': 'parted',
'partx': 'util-linux',
'qemu-img': 'qemu-utils',
'mksquashfs': 'squashfs-tools',
'xorriso': 'xorriso',
}\
.each_pair do |tool, pkg|
sh("which #{tool}") do |ok, res|
puts "Missing #{tool}." \
"Run: 'sudo apt-get install #{pkg}'" if not ok
end
end
end
end
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# build tasks
#
namespace :build do
# How to build a cache of pkgs needed for speeding up debootstrap runs.
file DEBOOTSTRAP_CACHE_PATH do
ensure_var('CUSTOMIZE_PKGS')
ensure_tmpfs_params()
builder = DebootstrapBuilder.new(distro,
DEBOOTSTRAP_CACHE_PATH,
customize_pkgs: ENV['CUSTOMIZE_PKGS'],
apt_mirror_url: ENV['APT_MIRROR_URL'],
verbose: verbose)
builder.create_debootstrap_packages_tarball()
end
# How to build a basic rootfs using debootstrap.
# This relies on a tarball of cached packages that is usable by debootstrap.
file ROOTFS_TAR_PATH => DEBOOTSTRAP_CACHE_PATH do
ensure_var('CUSTOMIZE_PKGS')
ensure_var('CUSTOMIZE_SCRIPT')
ensure_var('OVERLAY_ROOTFS')
ensure_tmpfs_params()
builder = DebootstrapBuilder.new(distro,
ROOTFS_TAR_PATH,
debootstrap_pkg_cache: DEBOOTSTRAP_CACHE_PATH,
customize_pkgs: ENV['CUSTOMIZE_PKGS'],
customize_rootfs: ENV['CUSTOMIZE_SCRIPT'],
overlay_rootfs: ENV['OVERLAY_ROOTFS'],
apt_mirror_url: ENV['APT_MIRROR_URL'],
verbose: verbose)
builder.create_debootstrap_rootfs()
end
# How to build a disk (vmdk) given a rootfs (created by debootstrap).
file UEFI_VMDK_FILE_PATH => ROOTFS_TAR_PATH do
ensure_var('PARTITION_LAYOUT')
ensure_device_or_tmpfs_params()
builder = UefiDiskBuilder.new(ROOTFS_TAR_PATH, ENV['PARTITION_LAYOUT'],
outfile: UEFI_VMDK_FILE_PATH,
dev: ENV['DEVICE'])
builder.build()
end
# How to build a disk (vmdk) given a rootfs (created by debootstrap).
file BIOS_VMDK_FILE_PATH => ROOTFS_TAR_PATH do
ensure_var('PARTITION_LAYOUT')
ensure_device_or_tmpfs_params()
builder = BiosDiskBuilder.new(ROOTFS_TAR_PATH, ENV['PARTITION_LAYOUT'],
outfile: BIOS_VMDK_FILE_PATH,
dev: ENV['DEVICE'])
builder.build()
end
#
# Build a tarball of cached deb packages usable by debootstrap.
#
desc 'Build debootstrap package cache (supports some env vars)'
task :cache => DEBOOTSTRAP_CACHE_PATH
#
# Build a basic rootfs using debootstrap.
#
desc 'Build basic rootfs using debootstrap (supports some env vars)'
task :rootfs => ROOTFS_TAR_PATH
desc 'Build (live cd) ISO using the debootstrap rootfs'
task :iso => ROOTFS_TAR_PATH do
IsoBuilder.new(ROOTFS_TAR_PATH, LIVECD_ISO_FILE_PATH).build
end
#
# Build vmdks.
#
namespace :vmdk do
desc 'Build a bootable UEFI vmdk disk using the debootstrap rootfs'
task :uefi => UEFI_VMDK_FILE_PATH
desc 'Build a bootable BIOS vmdk disk using the debootstrap rootfs'
task :bios => BIOS_VMDK_FILE_PATH
end
#
# Build devices
#
namespace :device do
desc 'Build a bootable UEFI device using the debootstrap rootfs'
task :uefi do
ensure_var('DEVICE')
ensure_var('PARTITION_LAYOUT')
builder = UefiDiskBuilder.new(ROOTFS_TAR_PATH,
ENV['PARTITION_LAYOUT'],
dev: ENV['DEVICE'])
builder.build()
end
desc 'Build a bootable BIOS device using the debootstrap rootfs'
task :bios do
ensure_var('DEVICE')
ensure_var('PARTITION_LAYOUT')
builder = BiosDiskBuilder.new(ROOTFS_TAR_PATH,
ENV['PARTITION_LAYOUT'],
dev: ENV['DEVICE'])
builder.build()
end
end
end
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Clean tasks
#
namespace :clean do
desc "Clean the debootstrap rootfs file"
task :cache do
sh("rm -f #{DEBOOTSTRAP_CACHE_PATH}")
end
desc "Clean the debootstrap rootfs file"
task :rootfs do
sh("rm -f #{ROOTFS_TAR_PATH}")
end
desc "Clean the ISO file"
task :iso do
sh("rm -f #{LIVECD_ISO_FILE_PATH}")
end
namespace :vmdk do
desc "Clean the UEFI disk file"
task :uefi do
sh("rm -f #{UEFI_VMDK_FILE_PATH}")
end
desc "Clean the BIOS disk file"
task :bios do
sh("rm -f #{BIOS_VMDK_FILE_PATH}")
end
desc "Clean all VMDK disk files"
task :all => [:bios, :uefi]
end
task :device do
ensure_env(["DEVICE"])
dev = ENV['DEVICE']
raise ArgumentError, "Invalid device " unless File.exists?(dev)
pvs = []
vgs = []
Dir.glob("#{dev}p*") do |part|
pv = `sudo pvs -S pv_name=#{part} -o pv_name --noheadings | grep #{part}`.strip
next if pv.empty? # grep didnt find anything, so this isn't a pv
pvs << pv
vg_name = `sudo pvs -S pv_name=#{part} -o vg_name --noheadings`.strip
next if vg_name.empty?
vgs << vg_name
end
vgs.uniq.each { |vg| sh("sudo vgchange -an #{vg}") }
vgs.uniq.each { |vg| sh("sudo vgremove -y #{vg}") }
pvs.uniq.each { |pv| sh("sudo pvremove #{pv}") }
sh("sudo partx -d #{dev}")
sh("sudo dd if=/dev/zero of=#{dev} bs=4M output=progress")
end
end
# Allow folks to include customizations to this workflow without having to
# maintain their own fork of this repo. For example,
# - you could take any of the rake tasks above and instantiate the worker
# objects with different params to get different behavior
# - inherit from any of the classes under src/ and override some methods to
# get different behavior (e.g. different text in grub_cfg_contents)
if Dir.exists?('./tasks')
Dir.glob('tasks/*.rake').each { |r| import r }
end