/
mkvmdist
executable file
·250 lines (173 loc) · 6.92 KB
/
mkvmdist
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
#!/bin/sh -e
# Requirements: parted, e2fsprogs, librsvg[tools]
# open-ovf, ovftool, vmware-{vdiskmanager,mount}
#
# http://dmtf.org/standards/vman
# http://gitorious.org/open-ovf
# http://blogs.vmware.com/vapp/2009/11/virtual-hardware-in-ovf-part-1.html
sinfo() {
echo ${BASH:+-e} "\033[1;33;41m$@\033[0m"
}
# Must have root directory as an argument
if [ -z "$1" ]; then
echo "$0 <livecd root>"
exit 1
fi
# Origin paths
src=`dirname $0`
distroot=$1/dist
cdroot=${distroot}/cdroot
vmstage=${distroot}/vmstage
# Versioning and naming
version=`cat ${src}/conf/version`
distname=liberte-${version}
# Derived paths
logosvg=${src}/src/home/anon/config/images/liberte-logo-nofont.svg
efiboot=${cdroot}/boot/efiboot.img
syslinux=${cdroot}/liberte/boot/syslinux/syslinux-x86.tbz
ovfpath=${vmstage}/${distname}.ovf
diskpath=${vmstage}/${distname}.vmdk
logopath=${vmstage}/${distname}.png
ovapath=${distroot}/${distname}.ova
# Descriptions
volume="Liberté Linux"
pname="Liberte Linux"
purl="http://dee.su/liberte"
pdesc="${pname}: Mission-critical stealth communication."
pclass=su.dee.liberte
efilabel=LIBERTE_EFI
# VM parameters
ramsize=256
disksize=512
diskformat="http://www.vmware.com/interfaces/specifications/vmdk.html#sparse"
nettype=NAT
logowidth=128
efiextrablocks=32
# Internal names
vhwname=hw0
diskid=disk0
netid=net0
iconid=icon0
# Common mkovf parameters
param="-f ${ovfpath} --encoding=UTF-8"
# Safety checks
if [ ! -e ${cdroot} ]; then
echo "cdroot does not exist yet"
exit 1
fi
# Prepare a staging directory
rm -rf ${vmstage}
mkdir ${vmstage}
# See also disk controller resource below
sinfo "Initializing virtual disk"
rm -f ${diskpath}
vmware-vdiskmanager -c -q -a ide -t 0 -s ${disksize}MB ${diskpath}
sinfo "Mounting virtual disk image"
mkdir ${vmstage}/vdisk
vmware-mount -f ${diskpath} ${vmstage}/vdisk
sinfo "Partitioning and installing MBR"
efiblocks=`du -s -B 4K --apparent-size ${cdroot}/EFI | cut -f1`
efisize=$(((efiblocks + 6 + efiextrablocks) * 4))
parted -s ${vmstage}/vdisk/flat \
mklabel msdos \
mkpart primary fat16 4KiB $(((4 + efisize) * 1024 - 1))B \
mkpart primary ext2 $((4 + efisize))KiB 100% \
set 2 boot on
sfdisk -q --change-id ${vmstage}/vdisk/flat 1 ef
loop=`losetup -f`
losetup ${loop} ${vmstage}/vdisk/flat
tar -xjf ${syslinux} -C ${vmstage} mbr_c.bin
cat ${vmstage}/mbr_c.bin > ${loop}
while ! losetup -d ${loop}; do sleep 1; done
sinfo "Creating EFI filesystem"
losetup -o $((4 * 1024)) -s $((efisize * 1024)) ${loop} ${vmstage}/vdisk/flat
mkdosfs -n "${efilabel}" ${loop}
mkdir ${vmstage}/vpart
mount -t vfat ${loop} ${vmstage}/vpart
rsync -aHS ${cdroot}/EFI ${vmstage}/vpart
umount -d ${vmstage}/vpart
sinfo "Creating main filesystem"
losetup -o $(((4 + efisize) * 1024)) ${loop} ${vmstage}/vdisk/flat
mke2fs -q -m0 -T largefile -L "${volume}" -O ^resize_inode,^dir_index,sparse_super \
-F ${loop}
tune2fs -c 0 -i 0 ${loop}
mount -t ext2 -o noatime ${loop} ${vmstage}/vpart
sinfo "Copying live image"
rsync -aHS ${cdroot}/liberte ${vmstage}/vpart
sinfo "Installing boot manager"
${cdroot}/liberte/setup.sh ${loop}
umount -d ${vmstage}/vpart
sinfo "Unmounting virtual disk image"
vmware-mount -d ${vmstage}/vdisk
sinfo "Verifying virtual partitions"
vmware-mount ${diskpath} 1 ${vmstage}/vpart
test -e ${vmstage}/vpart/EFI/BOOT/BOOTx64.EFI
vmware-mount -d ${vmstage}/vpart
vmware-mount ${diskpath} 2 ${vmstage}/vpart
test -e ${vmstage}/vpart/liberte/boot/syslinux/ldlinux.sys
vmware-mount -d ${vmstage}/vpart
sinfo "Rasterizing logo"
rsvg-convert -a -w ${logowidth} -f png -o ${logopath} ${logosvg}
sinfo "Creating OVF envelope"
mkovf --init ${param}
mkovf --lang ${param} -l "en-GB"
# Reference external files
mkovf --efile ${param} -i ${diskid} -n `basename ${diskpath}` -c identity
mkovf --efile ${param} -i ${iconid} -n `basename ${logopath}` -c identity
# Create disks section (-r references --efile -i above)
mkovf --disk ${param} -i ${diskid} -r ${diskid} -u "MegaBytes" \
-c ${disksize} -m "Virtual Disks" -F "${diskformat}"
# Create networks section
mkovf --net ${param} -i ${netid} -e ${nettype} -m "Virtual Networks" \
-d "NAT Network"
# Define a virtual system
mkovf --vs ${param} -i "${distname}" -m "Virtual Appliance"
# Add Guest OS (100 = Linux 2.6 (64-bit) in VirtualBox, required for x64 EFI)
mkovf --os ${param} -d "Linux 3.x" -m "Guest Operating System" --secID=100
# Add virtual hardware section (vmx-08 for vmwgfx dri)
mkovf --virthw ${param} -p "vmx-06 vmx-08" --instanceID=1 -n ${vhwname} \
-m "Virtual Hardware Requirements"
# Allocate CPUs
mkovf --resource ${param} --elementName="Processors" \
-i 1 -k 3 -d "Number of Virtual CPUs" -k 3 -g 1
# Allocate RAM
mkovf --resource ${param} --elementName="Memory" -u "MegaBytes" \
-i 2 -k 4 -d "Memory Size" -g ${ramsize}
# Allocate NIC (-n references --net -e above)
mkovf --resource ${param} --elementName="Network Adapter" -t True \
-i 3 -k 10 -d "Ethernet Adapter" -b E1000 -n ${nettype}
# Allocate disk controller
# IDE: -k 5 (TianoCore EFI fw does not support SCSI disks)
# SCSI: -k 6 / -b lsilogic
# SATA: -k 20 / -b AHCI (Not supported in VMware)
mkovf --resource ${param} --elementName="IDE" \
-i 4 -k 5 -d "IDE Controller" -a 0
# Allocate disk
mkovf --resource ${param} --elementName="Hard Disk" \
-i 5 -k 17 -d "Hard Disk" -j 4 -p 0 -r ovf://disk/${diskid}
# Allocate USB controller (optional; EHCI (USB 2.0) is not on by default, however)
mkovf --resource ${param} --elementName="USB" \
-i 6 -k 23 -d "USB Controller" -b vmware.usb.ehci -y
# Allocate sound adapters (optional, VMware and VirtualBox subtypes)
mkovf --resource ${param} --elementName="Sound" \
-i 7 -k 35 -d "Sound Card" -b ensoniq1371 -y
mkovf --resource ${param} --elementName="Sound" \
-i 8 -k 1 -d "Sound Card" -b vmware.soundcard.ensoniq1371 -y
# Create informative sections
mkovf --annotate ${param} -m "Annotation" -a "${pdesc}"
mkovf --product ${param} -m "Product Information" -p "${pname}" -v ${version} \
-r "${purl}" -c ${pclass} -n ${pclass}.1
mkovf --icon ${param} -t image/png -w ${logowidth} -n ${iconid} -s ${pclass} \
--instance=${pclass}.1
# Post-process unicode names (mkovf uses --encoding only for XML header)
# mkovf --resource -y is an on-switch without corresponding off-switch
sinfo "Fixing OVF envelope"
sed -i 's/Liberte/Liberté/g ;
s/\<Item ovf:required="true"/Item ovf:required="false"/g' \
${ovfpath}
sinfo "Validating OVF and creating OVA bundle"
ovftool -o -q --schemaValidate --skipManifestCheck --skipManifestGeneration \
${ovfpath} ${ovapath}
chmod --reference=${ovfpath} ${ovapath}
rm -r ${vmstage}
sinfo "Done."