How to move a partition in GNU/Linux?

17

7

How to move a partition to the beginning of the disk a bit? Parted wants a filesystem for some reason (I don't know why), I want just to shift all sectors left...

r@l:15:32:45:~# parted /dev/sdb
GNU Parted 2.3
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p                                                                
Model: HGST HTS 541010A9E680 (scsi)
Disk /dev/sdb: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  32.2GB  32.2GB  primary  fat32
 2      32.2GB  37.6GB  5360MB  primary
 3      37.6GB  1000GB  963GB   primary

(parted) move 3                                                           
WARNING: you are attempting to use parted to operate on (move) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs.  We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Error: Could not detect file system.   

Vi.

Posted 2013-01-24T12:35:43.520

Reputation: 13 705

gparted did it well for me in a simular situation. – palacsint – 2017-10-11T11:49:53.977

Maybe finally the in the end they have implemented this? – Vi. – 2017-10-11T17:18:33.707

@Vi. I don't think gparted can do this yet (no LUKS support afaik), however KDE Partition Manager can and it's fantastically simple these days, see here

– Sam3000 – 2018-07-28T21:47:28.877

1Parted vesion 3.2 does not have "move" as an option. – Lenne – 2018-09-14T23:21:11.090

Are you using the partitions without FS? You can just set the FS to anyone with fdisk and then move them. – Peter – 2013-01-24T13:10:57.597

Thus is actually a LUKS container. But I don't think a partition mover should ever look inside the filesystem. – Vi. – 2013-01-24T13:25:03.293

Answers

4

sfdisk, which intents to be a scriptable fdisk, has since some version the --move-data option. Example from their man page:

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

gimpf

Posted 2013-01-24T12:35:43.520

Reputation: 156

16

Manual way with dd and fdisk:

# fdisk -l /dev/sdb | grep sdb3
/dev/sdb3        73402368  1953525167   940061400   83  Linux

# fdisk /dev/sdb
Command (m for help): d
Partition number (1-4): 3
Command (m for help): n
Partition number (1-4, default 3): 3
First sector (73385984-1953525167, default 73385984): 
Using default value 73385984
Last sector, +sectors or +size{K,M,G} (73385984-1953525167, default 1953525167): 
Using default value 1953525167
Command (m for help): w
The partition table has been altered!

# fdisk -l /dev/sdb | grep sdb3
/dev/sdb3        73385984  1953525167   940069592   83  Linux

# dd conv=notrunc bs=512 iflag=fullblock if=/dev/sdb3 count=100 skip=$((73402368-73385984)) seek=0 2> /dev/null | file -s -
/dev/stdin: LUKS encrypted file, ver 1 [aes, cbc-essiv:sha256, sha1] UUID: af1c47f0-4ca5-4ea7-a091-065bd263653f

# dd conv=notrunc bs=512 iflag=fullblock if=/dev/sdb3  skip=$((73402368-73385984)) seek=0 of=/dev/sdb3

# file -s /dev/sdb3
/dev/sdb3: sticky LUKS encrypted file, ver 1 [aes, cbc-essiv:sha256, sha1] UUID: af1c47f0-4ca5-4ea7-a091-065bd263653f

Now waiting for about 2h. (more looks more like 18h...)

Note: this only moves data back, not forward.

Pausing:

# pidof dd
907
# kill -STOP 907
# cat /proc/907/fdinfo/1
pos:    586921398272
flags:  0100001

# kill -9 907

remember 586921398272/512 = 1146330856

Resuming:

dd conv=notrunc bs=512 iflag=fullblock if=/dev/sdb3  skip=$((1146330856+73402368-73385984)) seek=1146330856 of=/dev/sdb3

Vi.

Posted 2013-01-24T12:35:43.520

Reputation: 13 705

3why not dd conv=notrunc bs=512 iflag=fullblock if=/dev/sdb skip=73402368 seek=0 of=/dev/sdb3 ? this way it, should work even f start of old sdb3 in not inside new sdb3 – solsTiCe – 2015-06-10T14:27:41.523

@solsTiCe, What is the difference between your command and my command? – Vi. – 2015-06-10T16:36:15.637

It copies data from /dev/sdb and not from /dev/sdb3 – solsTiCe – 2015-06-10T22:39:55.893

First things first, this is very useful. Personal taste, but I think would be perfect providing both the codes as one line per command (as current) together with an annex showing the same codes as an edited version respecting 80 columns width. – 174140 – 2015-08-13T14:28:22.517

This is excellent for moving partitions back from sector 2048 to sector 63 so older versions of Windows can boot – SystemParadox – 2016-08-13T09:43:43.740

3A larger bs didn't help me on its own, however bs=1M iflag=direct oflag=direct did. Went from 40 MB/s to 400 MB/s on my SSD. Also, use status=progress – Aleksandr Dubinsky – 2017-03-23T09:42:09.210

Having created a new larger partition with fdisk (I was able to make room for that) and filled it with dd, I see (a) MS WIndows Control Panel / Administrative Tools / Computer Management / Storage (or similar) says it is unallocated and (b) it does not appear in the various /dev/disk/by* directories. Are there extra steps needed to deal with (a) and (b)? – PJTraill – 2017-06-13T19:21:38.777

2Lifesaver! Did anyone try to use a larger bs value (for speed reasons)? – David Balažic – 2013-11-22T00:24:54.550

Ensure you recalculated the values appropriately in case of other bs size. – Vi. – 2013-11-25T12:33:14.867

3

You can always use dd.

  • Boot from a liveCD (or any other way which makes sure the partition is not mounted)
  • dd if=/dev/sdc2 of=somefile bs=1M to create a copy of the partition on a file.
  • fdisk (or whatever you like) to delete the partition
  • fdisk (or whatever you like) to create the partition in your desired place.
  • dd of=/dev/sdc2 if=somefile bs=1M to restore the contents from file.

Hennes

Posted 2013-01-24T12:35:43.520

Reputation: 60 739

3@Vi. This correct, first response of an answer deserves no one's down-vote because this "do it in place" requirement exists nowhere in the question. – zero2cx – 2017-01-15T16:40:31.507

How to do it in-place? This partition is almost as large as all other HDDs in my home taked together. – Vi. – 2013-01-24T13:24:16.897

Can I do something like dd if=/dev/sdb of=/dev/sdb bs=512 skip=N seek=M? – Vi. – 2013-01-24T13:26:39.440

Might be able tom but test it first. Esp. if you have no backup of the data on the partition. (Also note that if you do have a backup then there are way simpler solutions). – Hennes – 2013-01-24T13:40:07.187