Comment 2 for bug 1569337

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

I tried this to solve this issue:

    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)

            result = subprocess.check_call(
                'file {} --mime-type'.format(
                    os.path.join(temp_dir, 'squashfs-root', initrd_path)),
                cwd=initrd_unpacked_path)
            mime_type = result.split()[-1]
            decompressor = 'gzip'
            if "gzip" in mime_type:
                decompressor = 'gzip'
            elif "x-xz" in mime_type:
                decompressor = 'xz'

            subprocess.check_call(
                'cat {0} | {1} -dc | cpio -i'.format(
                    os.path.join(temp_dir, 'squashfs-root', initrd_path), decompressor),
                shell=True, cwd=initrd_unpacked_path)

        return initrd_unpacked_path