1

I have a partition, say 190 GB, followed by 100 GB of unpartitioned disk free space.

I want to move this partition 100GB "to the right" (towards a higher sector number), so that the free unpartitioned free space would then be on the left of it.

This partition cannot just be copied carelessly, because:

  • it contains encrypted data that must be preserved (on the block level),
  • there is an overlap between the old and new position of the partition.

I know GParted should be able to handle this. However I would like to do it myself with command line tools. I know sfdisk has something like this to move partition 100M to the right:

echo '+100M,' | sfdisk --move-data /dev/sdc -N 1

I'd like to know 2 things:

  • If I was to move data manually, using tools like dd (or others), how would I do that? I believe the end of the partition must be copied before the start of it, like a backward read-and-copy (of course, I would also need to edit the GPT partition table).
  • If I want to use sfdisk, how can I use it so that I shift by a very precise sector number, so that it properly "touches" the partition that is on the far end of the disk? (so that they are really next to each other with no gap in between)
Totor
  • 19,302
  • 17
  • 75
  • 102
  • `dd` is not the right tool for this. You would have to manually make sure the partition sizes are correct before and after the copy and that the partition tables match what your just dd'ed to the raw disk. If `sfdisk` does what you want and their commands have been vetted, go with an accepted tool. – doneal24 Apr 26 '23 at 01:19
  • Side note: you say "move partition", but you want to move the partition and the filesystem within it. Windows often *pretends* partitions are filesystems, in Linux we should know better. Moving a partition is fast and easy, all you need to do is to update the partition table. If there was no filesystem (yet or at all) inside the partition, there would be no problem. Moving the existing *filesystem* to match the new partition is the non-trivial task your question is about. Thus I say the title should be "move a partition with filesystem…" or so. – Kamil Maciorowski Apr 26 '23 at 03:16
  • @KamilMaciorowski Yes, the partition contains encrypted data that must be preserved, I edited my question. – Totor Apr 26 '23 at 08:05
  • @doneal24 so, what is the right tool then? `dd` can work pretty well for moving a partition "backwards" (as it reads forward by default) – Totor Apr 26 '23 at 13:45
  • Probably GNU `ddrescue` with `--reverse` and `--max-read-errors=0` can be (ab)used. It won't be as straightforward as `sfdisk --move-data`, but if you overcome few obstacles and set all the relevant parameters right then one invocation of `ddrescue` will do the job. So it's not as "manually" as with `dd`, where you need to run a loop and calculate offsets in each iteration. Note [you should only ask *practical*, answerable questions based on *actual* problems that you face](https://unix.stackexchange.com/help/dont-ask). "If I was…?" does not fit. "I don't have `sfdisk` …" may fit if it's true. – Kamil Maciorowski Apr 27 '23 at 06:03
  • Thank you @KamilMaciorowski! With `sfdisk` I wouldn't be sure of the offset I can use and I fear that I cannot control exactly where on the disk the partition is shifted... So I felt more confident with other tools. But, yes, a precise answer with `sfdisk` would also be interesting for me (as the doc is ok, but not so precise). I updated my question accordingly. – Totor Apr 27 '23 at 11:47
  • The way to do stuff like this is to start by taking backups :) The fact that the data is encrypted _shouldn't_ be an issue, as it's still data, and anyway _most_ (if not all) stuff used inside partitions considers disk positions relative the start or end of the surrounding partition (or whatever container it is, could be e.g. a full disk too). So the problem is just about moving the data to the correct position. But the overlap makes it a bit iffy, it would be much easier if you could copy the data off, recreate the partition in the new place and copy the data back. – ilkkachu Apr 27 '23 at 13:07

0 Answers0