[Android Q&A, Help & Troubleshooting] Inkypen:
Been lurking on XDA for a while trying to soak up all the good info here. Finally decided to have a go at building a ROM from source. I'm using Ubuntu in a virtual machine, managed to setup my local repository, included necessary device tree and kernel with custom local manifest.

The build process fails at 99% on the very last step which is creating the flashable zip. I found it quite annoying after waiting 3 hours for it to build, but figured everything is there to create the zip. I then tried make otapackage and it successfully spat out a flashable zip.

I''m perfectly comfortable with command line interfaces but i'm a complete Linux noob, last time I experimented with a Unix flavour was Amix (Amiga port of UNIX System V R4) and couldn't wrap my head around it. Ubuntu is a lot friendlier but I still don't get what the error message is trying to tell me. Can anyone give me a clue?

[ 99% 16544/16545] build /home/nicholas/aospex/out/target/product/lithium/aosp_lithium-ota-19b2cd9547.zip
FAILED: /home/nicholas/aospex/out/target/product/lithium/aosp_lithium-ota-19b2cd9547.zip
/bin/bash -c "(echo \"Package OTA: /home/nicholas/aospex/out/target/product/lithium/aosp_lithium-ota-19b2cd9547.zip\" ) && (PATH=/home/nicholas/aospex/out/host/linux-x86/bin/:\$PATH MKBOOTIMG=/home/nicholas/aospex/out/host/linux-x86/bin/mkbootimg	./build/tools/releasetools/ota_from_target_files -v	--block	--extracted_input_target_files /home/nicholas/aospex/out/target/product/lithium/obj/PACKAGING/target_files_intermediates/aosp_lithium-target_files-19b2cd9547	-p /home/nicholas/aospex/out/host/linux-x86	-k build/target/product/security/testkey	--backup=true	/home/nicholas/aospex/out/target/product/lithium/obj/PACKAGING/target_files_intermediates/aosp_lithium-target_files-19b2cd9547.zip /home/nicholas/aospex/out/target/product/lithium/aosp_lithium-ota-19b2cd9547.zip )"
Package OTA: /home/nicholas/aospex/out/target/product/lithium/aosp_lithium-ota-19b2cd9547.zip
running: openssl pkcs8 -in build/target/product/security/testkey.pk8 -inform DER -nocrypt
--- target info ---
blockimgdiff_versions = (str) 3,4
blocksize = (int) 262144
boot_size = (int) 67108864
build.prop = (dict) {'ro.build.display.id': 'aosp_lithium-userdebug 8.1.0 OPM6.171019.030.H1 19b2cd9547 test-keys', 'ro.wifi.channels': '', 'ro.treble.enabled': 'false', 'ro.modversion': 'AospExtended-v5.8-20180818-2322-UNOFFICIAL', 'ro.build.id': 'OPM6.171019.030.H1', 'ro.product.cpu.abilist': 'arm64-v8a,armeabi-v7a,armeabi', 'ro.build.product': 'lithium', 'ro.build.host': 'nicholas-VirtualBox', 'ro.product.name': 'lithium', 'ro.build.version.security_patch': '2018-08-05', 'ro.product.manufacturer': 'Xiaomi', 'ro.extended.display.version': 'AospExtended-v5.8-UNOFFICIAL', 'ro.product.cpu.abilist32': 'armeabi-v7a,armeabi', 'ro.build.version.incremental': '19b2cd9547', 'ro.build.version.preview_sdk': '0', 'dalvik.vm.isa.arm.features': 'default', 'ro.build.tags': 'test-keys', 'dalvik.vm.isa.arm64.variant': 'kryo', 'ro.aex.device': 'lithium', 'ro.build.version.sdk': '27', 'ro.extended.releasetype': 'UNOFFICIAL', 'ro.build.date': 'Sun Aug 19 00:22:22 BST 2018', 'dalvik.vm.isa.arm.variant': 'kryo', 'dalvik.vm.lockprof.threshold': '500', 'ro.product.locale': 'en-US', 'ro.build.fingerprint': 'Xiaomi/lithium/lithium:7.0/NRD90M/V9.5.5.0.NAAMIFA:user/release-keys', 'ro.build.date.utc': '1534634542', 'ro.build.characteristics': 'default', 'ro.product.cpu.abilist64': 'arm64-v8a', 'ro.build.expect.modem': '2018-07-04 19:18:52,8.7.5', 'ro.build.user': 'nicholas', 'dalvik.vm.isa.arm64.features': 'default', 'ro.expect.recovery_id': '0xeed4e453264d12e67c8b4daa242b8636714b9623000000000000000000000000', 'ro.product.device': 'lithium', 'ro.build.version.all_codenames': 'REL', 'dalvik.vm.stack-trace-dir': '/data/anr', 'ro.build.description': 'lithium-user 7.0 NRD90M V9.5.5.0.NAAMIFA release-keys', 'ro.build.flavor': 'aosp_lithium-userdebug', 'ro.product.cpu.abi': 'arm64-v8a', 'ro.bionic.ld.warning': '1', 'ro.product.brand': 'Xiaomi', 'persist.sys.dalvik.vm.lib.2': 'libart.so', 'ro.build.version.release': '8.1.0', 'ro.build.version.codename': 'REL', 'ro.extended.version': 'v5.8', 'media.recorder.show_manufacturer_and_model': 'true', 'ro.build.type': 'userdebug', 'ro.product.model': 'MI MIX', 'ro.build.version.base_os': '', 'net.bt.name': 'Android'}
default_system_dev_certificate = (str) build/target/product/security/testkey
device_type = (str) MMC
ext_mkuserimg = (str) mkuserimg_mke2fs.sh
extfs_sparse_flag = (str) -s
fs_type = (str) ext4
fstab = (dict) {'none': <common.Partition object at 0x7f3adc335350>, '/cache': <common.Partition object at 0x7f3adc32d9d0>, '/boot': <common.Partition object at 0x7f3adc32d910>, '/system': <common.Partition object at 0x7f3adc32d990>, '/dsp': <common.Partition object at 0x7f3adc32dc10>, '/frp': <common.Partition object at 0x7f3adc3352d0>, '/persist': <common.Partition object at 0x7f3adc32da90>, '/recovery': <common.Partition object at 0x7f3adc32d950>, '/firmware': <common.Partition object at 0x7f3adc32dc50>, '/bt_firmware': <common.Partition object at 0x7f3adc32dc90>, '/misc': <common.Partition object at 0x7f3adc335310>, '/data': <common.Partition object at 0x7f3adc32da10>, '/vendor': <common.Partition object at 0x7f3adc32da50>}
fstab_version = (int) 2
mkbootimg_args = (str)
mkbootimg_version_args = (str) --os_version 8.1.0 --os_patch_level 2018-08-05
multistage_support = (str) 1
ota_override_device = (str) lithium
recovery_api_version = (int) 3
recovery_as_boot = (str)
recovery_mount_options = (str) ext4=max_batch_time=0,commit=1,data=ordered,barrier=1,errors=panic,nodelalloc
recovery_size = (int) 67108864
selinux_fc = (str) /home/nicholas/aospex/out/target/product/lithium/obj/PACKAGING/target_files_intermediates/aosp_lithium-target_files-19b2cd9547/META/file_contexts.bin
squashfs_sparse_flag = (str) -s
system_size = (int) 3221225472
tool_extensions = (str) device/xiaomi/msm8996-common
use_set_metadata = (str) 1
userdata_size = (int) 58846064640
vendor_fs_type = (str) ext4
vendor_size = (int) 872415232
(using device-specific extensions from target_files)
--- can't determine the cache partition size ---
loaded device-specific extensions from /home/nicholas/aospex/out/target/product/lithium/obj/PACKAGING/target_files_intermediates/aosp_lithium-target_files-19b2cd9547/META/releasetools.py
using prebuilt recovery.img from IMAGES...
Traceback (most recent call last):
File "./build/tools/releasetools/ota_from_target_files", line 1647, in <module>
main(sys.argv[1:])
File "./build/tools/releasetools/ota_from_target_files", line 1602, in main
WriteFullOTAPackage(input_zip, output_zip)
File "./build/tools/releasetools/ota_from_target_files", line 570, in WriteFullOTAPackage
system_tgt = GetImage("system", OPTIONS.input_tmp)
File "./build/tools/releasetools/ota_from_target_files", line 328, in GetImage
return sparse_img.SparseImage(path, mappath, clobbered_blocks)
File "/home/nicholas/aospex/build/make/tools/releasetools/sparse_img.py", line 39, in __init__
header = struct.unpack("<I4H4I", header_bin)
struct.error: unpack requires a string argument of length 28
ninja: build stopped: subcommand failed.
01:55:58 ninja failed with: exit status 1