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 \ |
| 26 | ${PN}:do_image_ext4 \ |
| 27 | " |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | |
| 29 | IMAGE_TYPEDEP_vmdk = "ext4" |
| 30 | IMAGE_TYPEDEP_vdi = "ext4" |
| 31 | IMAGE_TYPEDEP_qcow2 = "ext4" |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 32 | IMAGE_TYPEDEP_hdddirect = "ext4" |
| 33 | IMAGE_TYPES_MASKED += "vmdk vdi qcow2 hdddirect" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 35 | ROOTFS ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.ext4" |
| 36 | |
| 37 | # Used by bootloader |
| 38 | LABELS_VM ?= "boot" |
| 39 | ROOT_VM ?= "root=/dev/sda2" |
| 40 | # Using an initramfs is optional. Enable it by setting INITRD_IMAGE_VM. |
| 41 | INITRD_IMAGE_VM ?= "" |
| 42 | INITRD_VM ?= "${@'${DEPLOY_DIR_IMAGE}/${INITRD_IMAGE_VM}-${MACHINE}.cpio.gz' if '${INITRD_IMAGE_VM}' else ''}" |
| 43 | do_bootdirectdisk[depends] += "${@'${INITRD_IMAGE_VM}:do_image_complete' if '${INITRD_IMAGE_VM}' else ''}" |
| 44 | |
| 45 | BOOTDD_VOLUME_ID ?= "boot" |
| 46 | BOOTDD_EXTRA_SPACE ?= "16384" |
| 47 | |
| 48 | DISK_SIGNATURE ?= "${DISK_SIGNATURE_GENERATED}" |
| 49 | DISK_SIGNATURE[vardepsexclude] = "DISK_SIGNATURE_GENERATED" |
| 50 | |
| 51 | build_boot_dd() { |
| 52 | HDDDIR="${S}/hdd/boot" |
| 53 | HDDIMG="${S}/hdd.image" |
| 54 | IMAGE=${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.hdddirect |
| 55 | |
| 56 | populate_kernel $HDDDIR |
| 57 | |
| 58 | if [ "${PCBIOS}" = "1" ]; then |
| 59 | syslinux_hddimg_populate $HDDDIR |
| 60 | fi |
| 61 | if [ "${EFI}" = "1" ]; then |
| 62 | efi_hddimg_populate $HDDDIR |
| 63 | fi |
| 64 | |
| 65 | BLOCKS=`du -bks $HDDDIR | cut -f 1` |
| 66 | BLOCKS=`expr $BLOCKS + ${BOOTDD_EXTRA_SPACE}` |
| 67 | |
| 68 | # Ensure total sectors is an integral number of sectors per |
| 69 | # track or mcopy will complain. Sectors are 512 bytes, and we |
| 70 | # generate images with 32 sectors per track. This calculation is |
| 71 | # done in blocks, thus the mod by 16 instead of 32. |
| 72 | BLOCKS=$(expr $BLOCKS + $(expr 16 - $(expr $BLOCKS % 16))) |
| 73 | |
| 74 | # Remove it since mkdosfs would fail when it exists |
| 75 | rm -f $HDDIMG |
| 76 | mkdosfs -n ${BOOTDD_VOLUME_ID} -S 512 -C $HDDIMG $BLOCKS |
| 77 | mcopy -i $HDDIMG -s $HDDDIR/* ::/ |
| 78 | |
| 79 | if [ "${PCBIOS}" = "1" ]; then |
| 80 | syslinux_hdddirect_install $HDDIMG |
| 81 | fi |
| 82 | chmod 644 $HDDIMG |
| 83 | |
| 84 | ROOTFSBLOCKS=`du -Lbks ${ROOTFS} | cut -f 1` |
| 85 | TOTALSIZE=`expr $BLOCKS + $ROOTFSBLOCKS` |
| 86 | END1=`expr $BLOCKS \* 1024` |
| 87 | END2=`expr $END1 + 512` |
| 88 | END3=`expr \( $ROOTFSBLOCKS \* 1024 \) + $END1` |
| 89 | |
| 90 | echo $ROOTFSBLOCKS $TOTALSIZE $END1 $END2 $END3 |
| 91 | rm -rf $IMAGE |
| 92 | dd if=/dev/zero of=$IMAGE bs=1024 seek=$TOTALSIZE count=1 |
| 93 | |
| 94 | parted $IMAGE mklabel msdos |
| 95 | parted $IMAGE mkpart primary fat16 0 ${END1}B |
| 96 | parted $IMAGE unit B mkpart primary ext2 ${END2}B ${END3}B |
| 97 | parted $IMAGE set 1 boot on |
| 98 | |
| 99 | parted $IMAGE print |
| 100 | |
| 101 | awk "BEGIN { printf \"$(echo ${DISK_SIGNATURE} | fold -w 2 | tac | paste -sd '' | sed 's/\(..\)/\\x&/g')\" }" | \ |
| 102 | dd of=$IMAGE bs=1 seek=440 conv=notrunc |
| 103 | |
| 104 | OFFSET=`expr $END2 / 512` |
| 105 | if [ "${PCBIOS}" = "1" ]; then |
| 106 | dd if=${STAGING_DATADIR}/syslinux/mbr.bin of=$IMAGE conv=notrunc |
| 107 | fi |
| 108 | |
| 109 | dd if=$HDDIMG of=$IMAGE conv=notrunc seek=1 bs=512 |
| 110 | dd if=${ROOTFS} of=$IMAGE conv=notrunc seek=$OFFSET bs=512 |
| 111 | |
| 112 | cd ${DEPLOY_DIR_IMAGE} |
| 113 | rm -f ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.hdddirect |
| 114 | ln -s ${IMAGE_NAME}.hdddirect ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.hdddirect |
| 115 | } |
| 116 | |
| 117 | python do_bootdirectdisk() { |
| 118 | validate_disk_signature(d) |
| 119 | set_live_vm_vars(d, 'VM') |
| 120 | if d.getVar("PCBIOS", True) == "1": |
| 121 | bb.build.exec_func('build_syslinux_cfg', d) |
| 122 | if d.getVar("EFI", True) == "1": |
| 123 | bb.build.exec_func('build_efi_cfg', d) |
| 124 | bb.build.exec_func('build_boot_dd', d) |
| 125 | } |
| 126 | |
| 127 | def generate_disk_signature(): |
| 128 | import uuid |
| 129 | |
| 130 | signature = str(uuid.uuid4())[:8] |
| 131 | |
| 132 | if signature != '00000000': |
| 133 | return signature |
| 134 | else: |
| 135 | return 'ffffffff' |
| 136 | |
| 137 | def validate_disk_signature(d): |
| 138 | import re |
| 139 | |
| 140 | disk_signature = d.getVar("DISK_SIGNATURE", True) |
| 141 | |
| 142 | if not re.match(r'^[0-9a-fA-F]{8}$', disk_signature): |
| 143 | bb.fatal("DISK_SIGNATURE '%s' must be an 8 digit hex string" % disk_signature) |
| 144 | |
| 145 | DISK_SIGNATURE_GENERATED := "${@generate_disk_signature()}" |
| 146 | |
| 147 | run_qemu_img (){ |
| 148 | type="$1" |
| 149 | qemu-img convert -O $type ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.hdddirect ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.$type |
| 150 | ln -sf ${IMAGE_NAME}.$type ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.$type |
| 151 | } |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 152 | create_vmdk_image () { |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 153 | run_qemu_img vmdk |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | create_vdi_image () { |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 157 | run_qemu_img vdi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | create_qcow2_image () { |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 161 | run_qemu_img qcow2 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | python do_vmimg() { |
| 165 | if 'vmdk' in d.getVar('IMAGE_FSTYPES', True): |
| 166 | bb.build.exec_func('create_vmdk_image', d) |
| 167 | if 'vdi' in d.getVar('IMAGE_FSTYPES', True): |
| 168 | bb.build.exec_func('create_vdi_image', d) |
| 169 | if 'qcow2' in d.getVar('IMAGE_FSTYPES', True): |
| 170 | bb.build.exec_func('create_qcow2_image', d) |
| 171 | } |
| 172 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 173 | addtask bootdirectdisk before do_vmimg |
| 174 | addtask vmimg after do_bootdirectdisk before do_image_complete |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 175 | do_vmimg[depends] += "qemu-native:do_populate_sysroot" |