2

I have a partition of size X GB filled by an ext4 filesystem. Now, I want to shrink this partition (and the filesystem) to only take up the latter X/2 GB. I'm using command-line tools and no parted. So, I've read (here and here for example) that I need to resize2fs and then use fdisk to shrink the partition. However, what about moving the filesystem by X/2 GB forward in the partition? Isn't that a necessary step?

Also, assuming this is necessary - should I use dd for moving the data, or is something else necessary/better?

0

1 Answer 1

0

Essential filesystem structures are addressed relatively to the start of the filesystem, which happens to be the same as the start of the block device. Since your filesystem is implicitly expected to start on the first block of the block device, it tolerates no offset at any time between the first block of the block device and the first block of the filesystem.

Your filesystem's end can be changed (grown and shrank) relatively to the starting point, but there's no easy way to extend beyond the first block (or to shrink a filesystem 'backwards'), as you'd introduce an offset between the block device and the start of file system on it. Theoretically this would of course be possible, however the current toolset and the Linux kernel is not prepared for this type of operation.

In the comment to this answer you stated that you only interested relocating your filesystem on a new partition covering only the second half of the current parititon.

Is a filesystem depends on which block device it uses? No. Relocation of the filesystem with different tools, such as dd is possible.

The procedure would look like this:

  1. make a backup, it never hurts
  2. shrink the existing filesystem down (or under) the size of the new partition
  3. calculate the filesystem's current starting position on the disk (not on the partition)
  4. repartition your disk
  5. dd the amount of data on the disk to it's right place (since you know the old starting point, the new starting point and the lenght)
  6. extend your filesystem (in case in step 2 you made it smaller)

I would like to highlight that this procedure will not work, if your shrinked-down filesystem (point 2) and it's desired new place (the new partition, in point 4) overlaps on the disk.

Consider using LVM to mitigate these issues like this and many others. With LVM, you could simply:

  1. shrink your filesystem
  2. shrink your logical volume (and let LVM handle all this mess)
4
  • Perhaps I should clarify - I don't need to the filesystem to be usable before the entire procedure is complete. So, I supposedly never need for it to support offsets. I just want to move the bytes in 0...X/2 G-1 to X/2 G ... X - 1. Theoretically, when I then resize the partition to only contain what were previously its last X/2 GB, the filesystem should fit perfectly. Or am I wrong?
    – einpoklum
    Commented Jan 28, 2015 at 23:24
  • the part with "0...X/2 G-1 to X/2 G ... X - 1" was not really clear, sorry :(
    – asdmin
    Commented Jan 30, 2015 at 14:04
  • I meant, from the first half of the partition to the second have of the partition. The "-1"s are because the indices are 0-based.
    – einpoklum
    Commented Jan 30, 2015 at 14:42
  • i hope it answers your question now ;)
    – asdmin
    Commented Jan 30, 2015 at 15:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .