使用 GParted 调整分区映像的大小?

使用 GParted 调整分区映像的大小?

我之前在 GParted 中调整过分区大小。现在我想将一个很大但大部分是空的分区移动到一个较小的驱动器上。我不想就地调整大小,而是更愿意在第三个驱动器上制作副本(使用 dd 的驱动器映像),调整副本大小,然后将副本 dd 到新驱动器。但我认为 Gparted 不会对驱动器映像进行操作?我该怎么做?我可以直接使用 GParted 使用的工具吗?

哦!或者我可以使用回送功能使映像文件看起来像 gparted 可以操作的物理驱动器吗?

答案1

您无法使用 GParted 执行此操作,但您可以ntfsresize直接在磁盘映像上使用该命令。

首先,克隆磁盘映像。您可以使用blkid或 gParted 找到磁盘设备名称,然后运行:

sudo dd if=<disk device> of=<image file>

例如:

$ sudo dd if=/dev/sda2 of=disk.img
1000000+0 records in
1000000+0 records out
512000000 bytes (512 MB) copied, 9.33361 s, 54.9 MB/s

然后,通过运行以下命令验证是否可以将其调整为所需的大小:

ntfsresize -i <image file>

例如:

$ ntfsresize -i disk.img
ntfsresize v2011.4.12 (libntfs-3g)
Device name        : disk.img
NTFS volume version: 3.1
Cluster size       : 4096 bytes
Current volume size: 511996416 bytes (512 MB)
Current device size: 512000000 bytes (512 MB)
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use       : 4 MB (0.6%)
Collecting resizing constraints ...
You might resize at 3035136 bytes or 4 MB (freeing 508 MB).
Please make a test run using both the -n and -s options before real resizing!

在这个例子中,我可以将大小一直调整到 4MB,因为我只是为了说明目的而制作了一个空白磁盘映像!

现在,您可以根据需要进行测试运行,尽管由于您正在操作图像,因此这并不是绝对必要的。您可以通过分别附加 、 或 来指定大小(以千字节、兆字节或千兆字节为k单位MG

ntfsresize -cnvs <size> <image file>

例如:

$ ntfsresize -cnvs 256M disk.img 
ntfsresize v2011.4.12 (libntfs-3g)

没有输出就是好输出!现在开始实际操作:

ntfsresize -vs <size> <image file>

最后一个例子:

$ ntfsresize -vs 256M disk.img 
ntfsresize v2011.4.12 (libntfs-3g)
Device name        : disk.img
NTFS volume version: 3.1
Cluster size       : 4096 bytes
Current volume size: 511996416 bytes (512 MB)
Current device size: 512000000 bytes (512 MB)
New volume size    : 255996416 bytes (256 MB)
Checking for bad sectors ...
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use       : 4 MB (0.6%)
Collecting resizing constraints ...
Needed relocations : 625 (3 MB)
WARNING: Every sanity check passed and only the dangerous operations left.
Make sure that important data has been backed up! Power outage or computer
crash may result major data loss!
Are you sure you want to proceed (y/[n])? y
Schedule chkdsk for NTFS consistency check at Windows boot time ...
Resetting $LogFile ... (this might take a while)
Relocating needed data ...
Relocate record       1:0x80:00000001:0x00000000:0x0000f423 --> 0x00007a11
Relocate record       2:0x80:00000624:0x00000000:0x0000f424 --> 0x00007a12
100.00 percent completed
Updating $BadClust file ...
Updating $Bitmap file ...
Updating Boot record ...
Syncing device ...
Successfully resized NTFS on device 'disk.img'.
You can go on to shrink the device for example with Linux fdisk.
IMPORTANT: When recreating the partition, make sure that you
  1)  create it at the same disk sector (use sector as the unit!)
  2)  create it with the same partition type (usually 7, HPFS/NTFS)
  3)  do not make it smaller than the new NTFS filesystem size
  4)  set the bootable flag for the partition if it existed before
Otherwise you won't be able to access NTFS or can't boot from the disk!
If you make a mistake and don't have a partition table backup then you
can recover the partition table by TestDisk or Parted's rescue mode.

就这样!现在您可以将该磁盘映像复制到新设备。

答案2

我不确定五年前的情况是什么,但今天它是蛋糕。

$ sudo udisksctl loop-setup -f disk.img
Mapped file disk.img as /dev/loop0.
$ gparted /dev/loop0
$ sudo udisksctl loop-delete -d /dev/loop0

您可以使用 来增大或缩小磁盘空间truncate -s。请记住,如果使用不当,这将清除磁盘末尾的任何备份分区表或其他数据。

与使用 losetup 相比,此方法的好处是可以显示分配了哪个循环设备。它还会查看分区表并为每个分区设置一个循环设备。这对于在磁盘上使用 gparted 来说并不是特别有用,但可以非常轻松地获取驱动器映像并在该映像中安装特定分区。

答案3

作为准备,请通过运行以下命令检查系统上的文件系统块列表:lsblk

用于losetup从磁盘映像创建循环块设备(以下称为disk.img

sudo losetup --find --partscan disk.img

然后运行lsblk以查看当前系统上的块列表。将输出与之前的输出进行比较lsblk,新输出就是您的循环块设备。使用它来调用gparted并继续调整大小操作。

sudo gparted /dev/loopXX

PS. 使用udisksctl loop-setup --file disk.img确实会为您创建一个循环设备,但您无法使用来调整此循环设备的大小,gparted因为它将是一个已安装的文件系统。您可以尝试卸载它,但一旦您这样做,循环设备就会消失,您就没有循环设备可以调用了gparted

答案4

我已经使用 GParted 很多年了,从来没有丢失过一点。我也喜欢易思Windows 的免费家庭版。

您的分区有多大/多小?您可以将数据复制到 USB/DVD/免费在线存储系统,以免丢失 - 然后就地调整大小吗?上述两个工具将不是如果让它们运行完成,则会丢失数据。

您会说I've resized partitions in GParted before- 那现在为什么还要担心呢?而且 DD 会逐位复制...所以无论如何您都必须调整大小(首先进行碎片整理)。

相关内容