Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 18 | inherit live-vm-common |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 20 | do_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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 26 | ${PN}:do_image_${VM_ROOTFS_TYPE} \ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 27 | " |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 29 | IMAGE_TYPEDEP_vmdk = "${VM_ROOTFS_TYPE}" |
| 30 | IMAGE_TYPEDEP_vdi = "${VM_ROOTFS_TYPE}" |
| 31 | IMAGE_TYPEDEP_qcow2 = "${VM_ROOTFS_TYPE}" |
| 32 | IMAGE_TYPEDEP_hdddirect = "${VM_ROOTFS_TYPE}" |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 33 | IMAGE_TYPES_MASKED += "vmdk vdi qcow2 hdddirect" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 35 | VM_ROOTFS_TYPE ?= "ext4" |
| 36 | ROOTFS ?= "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${VM_ROOTFS_TYPE}" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 37 | |
| 38 | # Used by bootloader |
| 39 | LABELS_VM ?= "boot" |
| 40 | ROOT_VM ?= "root=/dev/sda2" |
| 41 | # Using an initramfs is optional. Enable it by setting INITRD_IMAGE_VM. |
| 42 | INITRD_IMAGE_VM ?= "" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 43 | INITRD_VM ?= "${@'${IMGDEPLOYDIR}/${INITRD_IMAGE_VM}-${MACHINE}.cpio.gz' if '${INITRD_IMAGE_VM}' else ''}" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 44 | do_bootdirectdisk[depends] += "${@'${INITRD_IMAGE_VM}:do_image_complete' if '${INITRD_IMAGE_VM}' else ''}" |
| 45 | |
| 46 | BOOTDD_VOLUME_ID ?= "boot" |
| 47 | BOOTDD_EXTRA_SPACE ?= "16384" |
| 48 | |
| 49 | DISK_SIGNATURE ?= "${DISK_SIGNATURE_GENERATED}" |
| 50 | DISK_SIGNATURE[vardepsexclude] = "DISK_SIGNATURE_GENERATED" |
| 51 | |
| 52 | build_boot_dd() { |
| 53 | HDDDIR="${S}/hdd/boot" |
| 54 | HDDIMG="${S}/hdd.image" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 55 | IMAGE=${IMGDEPLOYDIR}/${IMAGE_NAME}.hdddirect |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 56 | |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 69 | # 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 107 | cd ${IMGDEPLOYDIR} |
| 108 | |
| 109 | ln -sf ${IMAGE_NAME}.hdddirect ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.hdddirect |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | python 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 | |
| 122 | def 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 | |
| 132 | def 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 | |
| 140 | DISK_SIGNATURE_GENERATED := "${@generate_disk_signature()}" |
| 141 | |
| 142 | run_qemu_img (){ |
| 143 | type="$1" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 144 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 147 | } |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | create_vmdk_image () { |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 149 | run_qemu_img vmdk |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | create_vdi_image () { |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 153 | run_qemu_img vdi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | create_qcow2_image () { |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 157 | run_qemu_img qcow2 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | python 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 169 | addtask bootdirectdisk before do_vmimg |
| 170 | addtask vmimg after do_bootdirectdisk before do_image_complete |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 171 | do_vmimg[depends] += "qemu-native:do_populate_sysroot" |