首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不损坏GPT分区表(结束指针)的情况下截断未使用空间的磁盘映像文件

如何在不损坏GPT分区表(结束指针)的情况下截断未使用空间的磁盘映像文件
EN

Ask Ubuntu用户
提问于 2019-11-18 05:40:58
回答 1查看 6K关注 0票数 3

大量关于获取磁盘映像和缩小根(仿生)文件系统分区的信息,然后截断图像以删除成为空闲空间的部分。就像https://softwarebakery.com//shrinking-images-on-linux

基本上有三个步骤。使用resize2fs缩小分区上的文件系统,然后减小分区的大小。然后,最后删除图像文件中现在未使用的空间。

这是我有问题的第三部分。每次我尝试dd或截断时,图像的所有分区都会被移除,就好像图像文件中的GPT分区表被破坏了一样。

这是原图

代码语言:javascript
复制
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk rock64-base.img: 30310400 sectors, 14.5 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 159DCEDE-DBEA-4657-96D9-2CE178A96B7E
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 30310366
Partitions will be aligned on 64-sector boundaries
Total free space is 30 sectors (15.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              64            8063   3.9 MiB     8300  loader1
   2            8064            8191   64.0 KiB    8300  reserved1
   3            8192           16383   4.0 MiB     8300  reserved2
   4           16384           24575   4.0 MiB     8300  loader2
   5           24576           32767   4.0 MiB     8300  atf
   6           32768          262143   112.0 MiB   0700  boot
   7          262144        30310366   14.3 GiB    8300  root

然后在前两步之后

代码语言:javascript
复制
Found valid GPT with protective MBR; using GPT.
Disk test.img: 30310400 sectors, 14.5 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 159DCEDE-DBEA-4657-96D9-2CE178A96B7E
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 30310366
Partitions will be aligned on 64-sector boundaries
Total free space is 23555002 sectors (11.2 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              64            8063   3.9 MiB     8300  loader1
   2            8064            8191   64.0 KiB    8300  reserved1
   3            8192           16383   4.0 MiB     8300  reserved2
   4           16384           24575   4.0 MiB     8300  loader2
   5           24576           32767   4.0 MiB     8300  atf
   6           32768          262143   112.0 MiB   0700  boot
   7          262144         6755394   3.1 GiB     8300  primary

您可以看到根文件系统分区已经减少到3.1G。

我可以很好地装载那个图像。我可以把它放回SD卡并启动我的设备。所以步骤1和步骤2都很好。

现在..。

如果我完成了执行‘截断大小=$(6755394+1)*512 'test.img’的过程

代码语言:javascript
复制
GPT fdisk (gdisk) version 1.0.3

Warning! Disk size is smaller than the main header indicates! Loading
secondary header from the last sector of the disk! You should use 'v' to
verify disk integrity, and perhaps options on the experts' menu to repair
the disk.
Caution: invalid backup GPT header, but valid main header; regenerating
backup header from main header.

Warning! Error 25 reading partition table for CRC check!
Warning! One or more CRCs don't match. You should repair the disk!

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: damaged

****************************************************************************
Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk
verification and recovery are STRONGLY recommended.
****************************************************************************
Disk test2.img: 6755395 sectors, 3.2 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 159DCEDE-DBEA-4657-96D9-2CE178A96B7E
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 30310366
Partitions will be aligned on 64-sector boundaries
Total free space is 23555002 sectors (11.2 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              64            8063   3.9 MiB     8300  loader1
   2            8064            8191   64.0 KiB    8300  reserved1
   3            8192           16383   4.0 MiB     8300  reserved2
   4           16384           24575   4.0 MiB     8300  loader2
   5           24576           32767   4.0 MiB     8300  atf
   6           32768          262143   112.0 MiB   0700  boot
   7          262144         6755394   3.1 GiB     8300  primary

显然,截断(或dd在这件事上)不是很好地发挥与GPT。在此文件上运行gdisk确认gpt已被销毁。

如果我在截断的文件上运行gdisk,它会报告一个废弃的GPT。

所以很明显我漏掉了什么。显然,当磁盘文件的末尾发生变化时,GPT会损坏或需要修复(不匹配)。尽管在第2步结束时它很好/存在,但我在gdisk上乱搞,无法修复它。此外,我想要一个解决方案,我可以用一个脚本和这个GPT,它不工作。

因此,因为它是一个GPT,所以我需要使用截断或dd以外的其他东西,或者在截断后必须手动“修复”GPT。

下面是gdisk验证报告的内容

代码语言:javascript
复制
Caution: The CRC for the backup partition table is invalid. This table may
be corrupt. This program will automatically create a new backup partition
table when you save your partitions.

Problem: The secondary header's self-pointer indicates that it doesn't reside
at the end of the disk. If you've added a disk to a RAID array, use the 'e'
option on the experts' menu to adjust the secondary header's and partition
table's locations.

Problem: Disk is too small to hold all the data!
(Disk size is 6755395 sectors, needs to be 30310400 sectors.)
The 'e' option on the experts' menu may fix this problem.

Problem: GPT claims the disk is larger than it is! (Claimed last usable
sector is 30310366, but backup header is at
30310399 and disk size is 6755395 sectors.
The 'e' option on the experts' menu will probably fix this problem

Partition(s) in the protective MBR are too big for the disk! Creating a
fresh protective or hybrid MBR is recommended.
EN

回答 1

Ask Ubuntu用户

回答已采纳

发布于 2019-11-18 18:13:18

GPT包括磁盘开头的主分区表和磁盘末尾的备份分区表。(实际上是磁盘的末尾--磁盘的最后几个扇区。)因此,在截断磁盘映像时,删除了备份分区表。更重要的是,主分区表中的一些指针和元数据变得无效,因为它们指向了(虚拟)磁盘的末尾。这正是v命令在gdisk中所抱怨的。

所有这些都不意味着磁盘被完全破坏。只要主分区表的数据是有效的,gdisk (和大多数其他GPT分区工具)就可以恢复。在gdisk中,您可以键入x以输入专家菜单,然后键入e将备份分区表数据移动到磁盘的新端,然后键入w将更改写入磁盘。(我建议在v之前再次输入w,以防我忽略了另一个问题;另外,请参见下面.)

但是,请注意,还有一个问题:根据gdisk,磁盘映像大小为6,755,395个扇区;但最后一个分区的最后扇区是6,755,394扇区。这没有为备份分区表留出足够的空间(默认情况下,它消耗33个扇区)。如果您可以重新执行整个过程,您可能应该将磁盘映像缩小一个较小的数量。如果没有,在用gdisk重新创建备份分区表之前,尝试将磁盘映像扩展33*512 (16,896)字节。

票数 3
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1189682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档