Comment 3 for bug 1569337

Revision history for this message
Gunther Laure (gunther-laure-e) wrote :

Better variant (and this time working)

    def _unpack_generic_initrd(self):
        initrd_path = os.path.join(
            'usr', 'lib', 'ubuntu-core-generic-initrd', 'initrd.img-core')
        initrd_unpacked_path = os.path.join(self.builddir, 'initrd-staging')
        if os.path.exists(initrd_unpacked_path):
            shutil.rmtree(initrd_unpacked_path)
        os.makedirs(initrd_unpacked_path)

        with tempfile.TemporaryDirectory() as temp_dir:
            subprocess.check_call([
                'unsquashfs', self.os_snap, os.path.dirname(initrd_path)],
                cwd=temp_dir)

            tmp_initrd_path = os.path.join(temp_dir, 'squashfs-root', initrd_path)
            cmd = shlex.split('file -L --mime-type {}'.format(tmp_initrd_path))
            result = subprocess.check_output(cmd)
            mime_type = str(result.split()[-1])
            logger.info('initrd mime_type: {} {}'.format(tmp_initrd_path, mime_type))
            decompressor = 'gzip'
            if "gzip" in mime_type:
                decompressor = 'gzip'
            elif "x-xz" in mime_type:
                decompressor = 'xz'
            elif "x-lzma" in mime_type:
                decompressor = 'xz'

            subprocess.check_call(
                'cat {0} | {1} -dc | cpio -i'.format(tmp_initrd_path, decompressor),
                shell=True, cwd=initrd_unpacked_path)

        return initrd_unpacked_path