summaryrefslogtreecommitdiffstats
path: root/meta/classes/image-vm.bbclass
diff options
context:
space:
mode:
authorRobert Yang <liezhi.yang@windriver.com>2016-03-30 00:23:09 -0700
committerRichard Purdie <richard.purdie@linuxfoundation.org>2016-03-31 23:01:37 +0100
commit723fa563a09daaf7d1b7e9c1bb079139b3e84677 (patch)
tree6ae64a1e5f556fc51dafb00dbdcebba3a3aa8053 /meta/classes/image-vm.bbclass
parent9e588481f866acb7ba09fb8a21fe32df42633af2 (diff)
downloadpoky-723fa563a09daaf7d1b7e9c1bb079139b3e84677.tar.gz
boot-directdisk.bbclass: merge it into image-vm.bbclass
They are doing the same things: create virtual machine images, merge them into one bbclass makes it easy to understand. (From OE-Core rev: c314fda9d739560b6a08e627e7aabf105d97c8c4) Signed-off-by: Robert Yang <liezhi.yang@windriver.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/classes/image-vm.bbclass')
-rw-r--r--meta/classes/image-vm.bbclass216
1 files changed, 198 insertions, 18 deletions
diff --git a/meta/classes/image-vm.bbclass b/meta/classes/image-vm.bbclass
index 8608ec0c11..68cf89b68d 100644
--- a/meta/classes/image-vm.bbclass
+++ b/meta/classes/image-vm.bbclass
@@ -1,37 +1,217 @@
1# image-vm.bbclass
2# (loosly based off bootimg.bbclass Copyright (C) 2004, Advanced Micro Devices, Inc.)
3#
4# Create an image which can be placed directly onto a harddisk using dd and then
5# booted.
6#
7# This uses syslinux. extlinux would have been nice but required the ext2/3
8# partition to be mounted. grub requires to run itself as part of the install
9# process.
10#
11# The end result is a 512 boot sector populated with an MBR and partition table
12# followed by an msdos fat16 partition containing syslinux and a linux kernel
13# completed by the ext2/3 rootfs.
14#
15# We have to push the msdos parition table size > 16MB so fat 16 is used as parted
16# won't touch fat12 partitions.
1 17
2LABELS_VM ?= "boot" 18do_bootdirectdisk[depends] += "dosfstools-native:do_populate_sysroot \
19 virtual/kernel:do_deploy \
20 syslinux:do_populate_sysroot \
21 syslinux-native:do_populate_sysroot \
22 parted-native:do_populate_sysroot \
23 mtools-native:do_populate_sysroot \
24 ${PN}:do_image_ext4 \
25 "
26
27IMAGE_TYPEDEP_vmdk = "ext4"
28IMAGE_TYPEDEP_vdi = "ext4"
29IMAGE_TYPEDEP_qcow2 = "ext4"
30IMAGE_TYPEDEP_hdddirect = "ext4"
31IMAGE_TYPES_MASKED += "vmdk vdi qcow2 hdddirect"
3 32
33ROOTFS ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.ext4"
34
35# Used by bootloader
36LABELS_VM ?= "boot"
37ROOT_VM ?= "root=/dev/sda2"
4# Using an initramfs is optional. Enable it by setting INITRD_IMAGE_VM. 38# Using an initramfs is optional. Enable it by setting INITRD_IMAGE_VM.
5INITRD_IMAGE_VM ?= "" 39INITRD_IMAGE_VM ?= ""
6INITRD_VM ?= "${@'${DEPLOY_DIR_IMAGE}/${INITRD_IMAGE_VM}-${MACHINE}.cpio.gz' if '${INITRD_IMAGE_VM}' else ''}" 40INITRD_VM ?= "${@'${DEPLOY_DIR_IMAGE}/${INITRD_IMAGE_VM}-${MACHINE}.cpio.gz' if '${INITRD_IMAGE_VM}' else ''}"
7do_bootdirectdisk[depends] += "${@'${INITRD_IMAGE_VM}:do_image_complete' if '${INITRD_IMAGE_VM}' else ''}" 41do_bootdirectdisk[depends] += "${@'${INITRD_IMAGE_VM}:do_image_complete' if '${INITRD_IMAGE_VM}' else ''}"
8 42
9# need to define the dependency and the ROOTFS for directdisk 43BOOTDD_VOLUME_ID ?= "boot"
10do_bootdirectdisk[depends] += "${PN}:do_image_ext4" 44BOOTDD_EXTRA_SPACE ?= "16384"
11ROOTFS ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.ext4"
12 45
13# creating VM images relies on having a hdddirect so ensure we inherit it here. 46EFI = "${@bb.utils.contains("MACHINE_FEATURES", "efi", "1", "0", d)}"
14inherit boot-directdisk 47EFI_PROVIDER ?= "grub-efi"
48EFI_CLASS = "${@bb.utils.contains("MACHINE_FEATURES", "efi", "${EFI_PROVIDER}", "", d)}"
15 49
16IMAGE_TYPEDEP_vmdk = "ext4" 50# Include legacy boot if MACHINE_FEATURES includes "pcbios" or if it does not
17IMAGE_TYPEDEP_vdi = "ext4" 51# contain "efi". This way legacy is supported by default if neither is
18IMAGE_TYPEDEP_qcow2 = "ext4" 52# specified, maintaining the original behavior.
19IMAGE_TYPEDEP_hdddirect = "ext4" 53def pcbios(d):
20IMAGE_TYPES_MASKED += "vmdk vdi qcow2 hdddirect" 54 pcbios = bb.utils.contains("MACHINE_FEATURES", "pcbios", "1", "0", d)
55 if pcbios == "0":
56 pcbios = bb.utils.contains("MACHINE_FEATURES", "efi", "0", "1", d)
57 return pcbios
58
59def pcbios_class(d):
60 if d.getVar("PCBIOS", True) == "1":
61 return "syslinux"
62 return ""
63
64PCBIOS = "${@pcbios(d)}"
65PCBIOS_CLASS = "${@pcbios_class(d)}"
66
67# Get the build_syslinux_cfg() function from the syslinux class
68inherit ${PCBIOS_CLASS}
69inherit ${EFI_CLASS}
70
71DISK_SIGNATURE ?= "${DISK_SIGNATURE_GENERATED}"
72
73boot_direct_populate() {
74 dest=$1
75 install -d $dest
76
77 # Install bzImage, initrd, and rootfs.img in DEST for all loaders to use.
78 if [ -e ${DEPLOY_DIR_IMAGE}/bzImage ]; then
79 install -m 0644 ${DEPLOY_DIR_IMAGE}/bzImage $dest/vmlinuz
80 fi
81
82 # initrd is made of concatenation of multiple filesystem images
83 if [ -n "${INITRD}" ]; then
84 rm -f $dest/initrd
85 for fs in ${INITRD}
86 do
87 if [ -s "${fs}" ]; then
88 cat ${fs} >> $dest/initrd
89 else
90 bbfatal "${fs} is invalid. initrd image creation failed."
91 fi
92 done
93 chmod 0644 $dest/initrd
94 fi
95}
96
97build_boot_dd() {
98 HDDDIR="${S}/hdd/boot"
99 HDDIMG="${S}/hdd.image"
100 IMAGE=${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.hdddirect
101
102 boot_direct_populate $HDDDIR
21 103
104 if [ "${PCBIOS}" = "1" ]; then
105 syslinux_hddimg_populate $HDDDIR
106 fi
107 if [ "${EFI}" = "1" ]; then
108 efi_hddimg_populate $HDDDIR
109 fi
110
111 if [ "x${AUTO_SYSLINUXMENU}" = "x1" ] ; then
112 install -m 0644 ${STAGING_DIR}/${MACHINE}/usr/share/syslinux/vesamenu.c32 $HDDDIR/${SYSLINUXDIR}/
113 if [ "x${SYSLINUX_SPLASH}" != "x" ] ; then
114 install -m 0644 ${SYSLINUX_SPLASH} $HDDDIR/${SYSLINUXDIR}/splash.lss
115 fi
116 fi
117
118 BLOCKS=`du -bks $HDDDIR | cut -f 1`
119 BLOCKS=`expr $BLOCKS + ${BOOTDD_EXTRA_SPACE}`
120
121 # Ensure total sectors is an integral number of sectors per
122 # track or mcopy will complain. Sectors are 512 bytes, and we
123 # generate images with 32 sectors per track. This calculation is
124 # done in blocks, thus the mod by 16 instead of 32.
125 BLOCKS=$(expr $BLOCKS + $(expr 16 - $(expr $BLOCKS % 16)))
126
127 # Remove it since mkdosfs would fail when it exists
128 rm -f $HDDIMG
129 mkdosfs -n ${BOOTDD_VOLUME_ID} -S 512 -C $HDDIMG $BLOCKS
130 mcopy -i $HDDIMG -s $HDDDIR/* ::/
131
132 if [ "${PCBIOS}" = "1" ]; then
133 syslinux_hdddirect_install $HDDIMG
134 fi
135 chmod 644 $HDDIMG
136
137 ROOTFSBLOCKS=`du -Lbks ${ROOTFS} | cut -f 1`
138 TOTALSIZE=`expr $BLOCKS + $ROOTFSBLOCKS`
139 END1=`expr $BLOCKS \* 1024`
140 END2=`expr $END1 + 512`
141 END3=`expr \( $ROOTFSBLOCKS \* 1024 \) + $END1`
142
143 echo $ROOTFSBLOCKS $TOTALSIZE $END1 $END2 $END3
144 rm -rf $IMAGE
145 dd if=/dev/zero of=$IMAGE bs=1024 seek=$TOTALSIZE count=1
146
147 parted $IMAGE mklabel msdos
148 parted $IMAGE mkpart primary fat16 0 ${END1}B
149 parted $IMAGE unit B mkpart primary ext2 ${END2}B ${END3}B
150 parted $IMAGE set 1 boot on
151
152 parted $IMAGE print
153
154 awk "BEGIN { printf \"$(echo ${DISK_SIGNATURE} | fold -w 2 | tac | paste -sd '' | sed 's/\(..\)/\\x&/g')\" }" | \
155 dd of=$IMAGE bs=1 seek=440 conv=notrunc
156
157 OFFSET=`expr $END2 / 512`
158 if [ "${PCBIOS}" = "1" ]; then
159 dd if=${STAGING_DATADIR}/syslinux/mbr.bin of=$IMAGE conv=notrunc
160 fi
161
162 dd if=$HDDIMG of=$IMAGE conv=notrunc seek=1 bs=512
163 dd if=${ROOTFS} of=$IMAGE conv=notrunc seek=$OFFSET bs=512
164
165 cd ${DEPLOY_DIR_IMAGE}
166 rm -f ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.hdddirect
167 ln -s ${IMAGE_NAME}.hdddirect ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.hdddirect
168}
169
170python do_bootdirectdisk() {
171 validate_disk_signature(d)
172 set_live_vm_vars(d, 'VM')
173 if d.getVar("PCBIOS", True) == "1":
174 bb.build.exec_func('build_syslinux_cfg', d)
175 if d.getVar("EFI", True) == "1":
176 bb.build.exec_func('build_efi_cfg', d)
177 bb.build.exec_func('build_boot_dd', d)
178}
179
180def generate_disk_signature():
181 import uuid
182
183 signature = str(uuid.uuid4())[:8]
184
185 if signature != '00000000':
186 return signature
187 else:
188 return 'ffffffff'
189
190def validate_disk_signature(d):
191 import re
192
193 disk_signature = d.getVar("DISK_SIGNATURE", True)
194
195 if not re.match(r'^[0-9a-fA-F]{8}$', disk_signature):
196 bb.fatal("DISK_SIGNATURE '%s' must be an 8 digit hex string" % disk_signature)
197
198DISK_SIGNATURE_GENERATED := "${@generate_disk_signature()}"
199
200run_qemu_img (){
201 type="$1"
202 qemu-img convert -O $type ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.hdddirect ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.$type
203 ln -sf ${IMAGE_NAME}.$type ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.$type
204}
22create_vmdk_image () { 205create_vmdk_image () {
23 qemu-img convert -O vmdk ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.hdddirect ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.vmdk 206 run_qemu_img vmdk
24 ln -sf ${IMAGE_NAME}.vmdk ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.vmdk
25} 207}
26 208
27create_vdi_image () { 209create_vdi_image () {
28 qemu-img convert -O vdi ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.hdddirect ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.vdi 210 run_qemu_img vdi
29 ln -sf ${IMAGE_NAME}.vdi ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.vdi
30} 211}
31 212
32create_qcow2_image () { 213create_qcow2_image () {
33 qemu-img convert -O qcow2 ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.hdddirect ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.qcow2 214 run_qemu_img qcow2
34 ln -sf ${IMAGE_NAME}.qcow2 ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.qcow2
35} 215}
36 216
37python do_vmimg() { 217python do_vmimg() {
@@ -43,6 +223,6 @@ python do_vmimg() {
43 bb.build.exec_func('create_qcow2_image', d) 223 bb.build.exec_func('create_qcow2_image', d)
44} 224}
45 225
226addtask bootdirectdisk before do_vmimg
46addtask vmimg after do_bootdirectdisk before do_image_complete 227addtask vmimg after do_bootdirectdisk before do_image_complete
47do_vmimg[depends] += "qemu-native:do_populate_sysroot" 228do_vmimg[depends] += "qemu-native:do_populate_sysroot"
48