| #!/usr/bin/env bash | |
| # | |
| # This file is part of util-linux. | |
| # | |
| # This file is free software; you can redistribute it and/or modify | |
| # it under the terms of the GNU General Public License as published by | |
| # the Free Software Foundation; either version 2 of the License, or | |
| # (at your option) any later version. | |
| # | |
| # This file is distributed in the hope that it will be useful, | |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| # GNU General Public License for more details. | |
| # | |
| # | |
| # The "expert" command "print the raw data of the disklabel" (D) on a GPT | |
| # whose partition entries array size is not a multiple of 16 bytes. | |
| # | |
| TS_TOPDIR="${0%/*}/../.." | |
| TS_DESC="gpt-dump" | |
| . "$TS_TOPDIR"/functions.sh | |
| ts_init "$*" | |
| ts_check_test_command "$TS_CMD_FDISK" | |
| ts_check_test_command "$TS_CMD_SFDISK" | |
| ts_check_prog "python3" | |
| ts_init_subtest "entry-size-136" | |
| TEST_IMAGE_NAME=$(ts_image_init 1) | |
| printf '%s\n' "label: gpt" \ | |
| "label-id: 01234567-89AB-CDEF-0123-456789ABCDEF" \ | |
| "start=34, size=64, type=L, uuid=00000000-0000-0000-0000-000000000001" \ | |
| | $TS_CMD_SFDISK --no-reread --no-tell-kernel ${TEST_IMAGE_NAME} &> /dev/null | |
| # One partition entry of 136 bytes. The backup header is cleared, so fdisk | |
| # uses the primary one. | |
| python3 - "$TEST_IMAGE_NAME" <<'PYEOF' >&2 | |
| import struct, zlib, os, sys | |
| SEC = 512 | |
| def crc32(b): return zlib.crc32(b) & 0xffffffff | |
| path = sys.argv[1] | |
| total = os.path.getsize(path) // SEC | |
| with open(path, 'r+b') as f: | |
| f.seek(SEC) | |
| ph = bytearray(f.read(SEC)) | |
| lba = struct.unpack_from('<Q', ph, 72)[0] | |
| f.seek(lba * SEC) | |
| ents = f.read(136) | |
| struct.pack_into('<III', ph, 80, 1, 136, crc32(ents)) | |
| hsz = struct.unpack_from('<I', ph, 12)[0] | |
| struct.pack_into('<I', ph, 16, 0) | |
| struct.pack_into('<I', ph, 16, crc32(bytes(ph[:hsz]))) | |
| f.seek(SEC) | |
| f.write(ph) | |
| f.seek((total - 1) * SEC) | |
| f.write(bytes(SEC)) | |
| PYEOF | |
| echo -e "x\nD\nq\n" | $TS_CMD_FDISK ${TEST_IMAGE_NAME} >> "$TS_OUTPUT" 2>> "$TS_ERRLOG" | |
| ts_fdisk_clean ${TEST_IMAGE_NAME} | |
| ts_finalize_subtest | |
| ts_finalize |