Ubuntu – Move ext4 partition

ext4partitioning

I have an ext4 partition at the end of an hard drive, and want to resize it to the full disk size (the rest of the disk is empty). It's not the root partition, just a data partition, so it's no problem to unmount the partition. The machine has Ubuntu server running, and neither Screen, keyboard, mouse nor optical disk drive attached, so booting a live environment to use this approach would be a bit of a hassle and I'd like to avoid that.

Anybody know an easy way to do this? For me, this proves to be a far more difficult task than I had anticipated, but let me elaborate:

Of course the disks partitions were all unmounted during the following operations.
I first tried via parted: I deleted the first partition which was not needed anymore, then wanted to resize or move the ext4 partition. But parted told me it wasn't able to do so, because of some filesystem features of ext4 (with a weird error message: "Error: File system has an incompatible feature enabled. Compatible features are
has_journal, dir_index, filetype, sparse_super and large_file. Use tune2fs or
debugfs to remove features." – so what are the incompatible features?); what I got from that, and as an answer on comments in another question, it is somehow not possible for parted to change the start of an ext4 partition.

Why can gparted, however, seemingly do the trick (see first linked question) but not parted?

Well, since I had enough space at the start of the drive left (more than the size of the partition to move) I thought I'd try another approach: create a second partition at the front, copy the contents from the one at the back to this one, then delete the old partition and resize the new to the full size. However, this also proved impossible: After creating the partition and copying the contents (via dd if=/dev/sdb2 of=/dev/sdb1), the partition can't be mounted ('missing journal superblock').

So at the moment I'm fresh out of ideas. I'll probably copy everything vital to another disk, delete all partitions again and create one big one. I'm wondering that this is such a problem – or am I overlooking the obvious?

Best Answer

It is a little bit time consuming, but it can be done if:

Assuming /dev/sdb1, and that the unallocated space is bigger than the amount of data from /dev/sdb1:

Before we start, let's create some mounting point directories in /mnt/:

mkdir /mnt/old && mkdir /mnt/new

The disk now looks like this:

[(.......UNALLOCATED.........)(xxxx /dev/sdb1 xxxxxx)]
  • e2fsck -f -y -v /dev/sdb1 #just to be sure that it is error free.
  • fdisk /dev/sdb
  • Press: n p 2 wq
  • mkfs.ext4 /dev/sdb2
    Now the disk looks like this:
[(xxxxxx /dev/sdb2 xxxxxxxxxx)(xxxx /dev/sdb1 xxxxxx)]
  • mount /dev/sdb2 /mnt/new && mount /dev/sdb1 /mnt/old
  • mv /mnt/old/* /mnt/new/ (You will get an error about lost+found, you can ignore it)
  • cd / && umount /mnt/old && umount /mnt/new
  • cfdisk /dev/sdb
  • Delete sdb1
  • Choose write
  • Delete sdb2
  • Choose write
  • Create new, choose the whole size available.
  • Write and quit.
  • e2fsck -f /dev/sdb1
  • resize2fs /dev/sdb1 Now the disk looks like this:
[(xxxxxxxxxxxxxxxxxxx /dev/sdb1 xxxxxxxxxxxxxxxxxxx)]
  • mount /dev/sdb1 /your/mount/point

FINISHED!

Related Question