blob: 2f35d6b4d15bbced220025ee4a0db4377660e5d9 [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001# image-vm.bbclass
2# (loosly based off image-live.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.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050018inherit live-vm-common
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050020do_bootdirectdisk[depends] += "dosfstools-native:do_populate_sysroot \
21 virtual/kernel:do_deploy \
22 syslinux:do_populate_sysroot \
23 syslinux-native:do_populate_sysroot \
24 parted-native:do_populate_sysroot \
25 mtools-native:do_populate_sysroot \
Patrick Williamsc0f7c042017-02-23 20:41:17 -060026 ${PN}:do_image_${VM_ROOTFS_TYPE} \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050027 "
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028
Patrick Williamsc0f7c042017-02-23 20:41:17 -060029IMAGE_TYPEDEP_vmdk = "${VM_ROOTFS_TYPE}"
30IMAGE_TYPEDEP_vdi = "${VM_ROOTFS_TYPE}"
31IMAGE_TYPEDEP_qcow2 = "${VM_ROOTFS_TYPE}"
32IMAGE_TYPEDEP_hdddirect = "${VM_ROOTFS_TYPE}"
Patrick Williamsf1e5d692016-03-30 15:21:19 -050033IMAGE_TYPES_MASKED += "vmdk vdi qcow2 hdddirect"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034
Patrick Williamsc0f7c042017-02-23 20:41:17 -060035VM_ROOTFS_TYPE ?= "ext4"
36ROOTFS ?= "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${VM_ROOTFS_TYPE}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050037
38# Used by bootloader
39LABELS_VM ?= "boot"
40ROOT_VM ?= "root=/dev/sda2"
41# Using an initramfs is optional. Enable it by setting INITRD_IMAGE_VM.
42INITRD_IMAGE_VM ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060043INITRD_VM ?= "${@'${IMGDEPLOYDIR}/${INITRD_IMAGE_VM}-${MACHINE}.cpio.gz' if '${INITRD_IMAGE_VM}' else ''}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050044do_bootdirectdisk[depends] += "${@'${INITRD_IMAGE_VM}:do_image_complete' if '${INITRD_IMAGE_VM}' else ''}"
45
46BOOTDD_VOLUME_ID ?= "boot"
47BOOTDD_EXTRA_SPACE ?= "16384"
48
49DISK_SIGNATURE ?= "${DISK_SIGNATURE_GENERATED}"
50DISK_SIGNATURE[vardepsexclude] = "DISK_SIGNATURE_GENERATED"
51
52build_boot_dd() {
53 HDDDIR="${S}/hdd/boot"
54 HDDIMG="${S}/hdd.image"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060055 IMAGE=${IMGDEPLOYDIR}/${IMAGE_NAME}.hdddirect
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050056
57 populate_kernel $HDDDIR
58
59 if [ "${PCBIOS}" = "1" ]; then
60 syslinux_hddimg_populate $HDDDIR
61 fi
62 if [ "${EFI}" = "1" ]; then
63 efi_hddimg_populate $HDDDIR
64 fi
65
66 BLOCKS=`du -bks $HDDDIR | cut -f 1`
67 BLOCKS=`expr $BLOCKS + ${BOOTDD_EXTRA_SPACE}`
68
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050069 # Remove it since mkdosfs would fail when it exists
70 rm -f $HDDIMG
71 mkdosfs -n ${BOOTDD_VOLUME_ID} -S 512 -C $HDDIMG $BLOCKS
72 mcopy -i $HDDIMG -s $HDDDIR/* ::/
73
74 if [ "${PCBIOS}" = "1" ]; then
75 syslinux_hdddirect_install $HDDIMG
76 fi
77 chmod 644 $HDDIMG
78
79 ROOTFSBLOCKS=`du -Lbks ${ROOTFS} | cut -f 1`
80 TOTALSIZE=`expr $BLOCKS + $ROOTFSBLOCKS`
81 END1=`expr $BLOCKS \* 1024`
82 END2=`expr $END1 + 512`
83 END3=`expr \( $ROOTFSBLOCKS \* 1024 \) + $END1`
84
85 echo $ROOTFSBLOCKS $TOTALSIZE $END1 $END2 $END3
86 rm -rf $IMAGE
87 dd if=/dev/zero of=$IMAGE bs=1024 seek=$TOTALSIZE count=1
88
89 parted $IMAGE mklabel msdos
90 parted $IMAGE mkpart primary fat16 0 ${END1}B
91 parted $IMAGE unit B mkpart primary ext2 ${END2}B ${END3}B
92 parted $IMAGE set 1 boot on
93
94 parted $IMAGE print
95
96 awk "BEGIN { printf \"$(echo ${DISK_SIGNATURE} | fold -w 2 | tac | paste -sd '' | sed 's/\(..\)/\\x&/g')\" }" | \
97 dd of=$IMAGE bs=1 seek=440 conv=notrunc
98
99 OFFSET=`expr $END2 / 512`
100 if [ "${PCBIOS}" = "1" ]; then
101 dd if=${STAGING_DATADIR}/syslinux/mbr.bin of=$IMAGE conv=notrunc
102 fi
103
104 dd if=$HDDIMG of=$IMAGE conv=notrunc seek=1 bs=512
105 dd if=${ROOTFS} of=$IMAGE conv=notrunc seek=$OFFSET bs=512
106
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600107 cd ${IMGDEPLOYDIR}
108
109 ln -sf ${IMAGE_NAME}.hdddirect ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.hdddirect
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500110}
111
112python do_bootdirectdisk() {
113 validate_disk_signature(d)
114 set_live_vm_vars(d, 'VM')
115 if d.getVar("PCBIOS", True) == "1":
116 bb.build.exec_func('build_syslinux_cfg', d)
117 if d.getVar("EFI", True) == "1":
118 bb.build.exec_func('build_efi_cfg', d)
119 bb.build.exec_func('build_boot_dd', d)
120}
121
122def generate_disk_signature():
123 import uuid
124
125 signature = str(uuid.uuid4())[:8]
126
127 if signature != '00000000':
128 return signature
129 else:
130 return 'ffffffff'
131
132def validate_disk_signature(d):
133 import re
134
135 disk_signature = d.getVar("DISK_SIGNATURE", True)
136
137 if not re.match(r'^[0-9a-fA-F]{8}$', disk_signature):
138 bb.fatal("DISK_SIGNATURE '%s' must be an 8 digit hex string" % disk_signature)
139
140DISK_SIGNATURE_GENERATED := "${@generate_disk_signature()}"
141
142run_qemu_img (){
143 type="$1"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600144 qemu-img convert -O $type ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.hdddirect ${IMGDEPLOYDIR}/${IMAGE_NAME}.$type
145
146 ln -sf ${IMAGE_NAME}.$type ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.$type
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500147}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148create_vmdk_image () {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500149 run_qemu_img vmdk
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150}
151
152create_vdi_image () {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500153 run_qemu_img vdi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500154}
155
156create_qcow2_image () {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500157 run_qemu_img qcow2
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158}
159
160python do_vmimg() {
161 if 'vmdk' in d.getVar('IMAGE_FSTYPES', True):
162 bb.build.exec_func('create_vmdk_image', d)
163 if 'vdi' in d.getVar('IMAGE_FSTYPES', True):
164 bb.build.exec_func('create_vdi_image', d)
165 if 'qcow2' in d.getVar('IMAGE_FSTYPES', True):
166 bb.build.exec_func('create_qcow2_image', d)
167}
168
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500169addtask bootdirectdisk before do_vmimg
170addtask vmimg after do_bootdirectdisk before do_image_complete
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171do_vmimg[depends] += "qemu-native:do_populate_sysroot"