Move ext4 partition

19,528

Solution 1

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!

Solution 2

Here's how I've done it on headless machines with SSH access only: use VNC and get gparted to do it. Use openbox for a minimal environment, and remove it all once done if this is one time only.

sudo apt-get install vnc4server openbox gparted
vnc4server       #set a password, create initial config
vnc4server -kill :1
sed -i -e 's/x-window-manager/openbox-session/g' ~/.vnc/xstartup
vnc4server

Open port 5901 incoming, and access with a VNC client. Right-click to start the terminal within openbox, and you'll be able to run any GTK+ apps, including gparted. apt-get remove and autoremove if you want to when you're done.

Solution 3

I went in the GUI direction:

1) Run the a live linux CD (Mint in my case)

2) Connect to Internet

3) Get gparted

sudo apt-get install gparted

4) After you've done all resizing in the neat gparted GUI, run the boot-repair.

5) https://help.ubuntu.com/community/Boot-Repair Basically comes down to getting it and dunning it:

sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt-get update

sudo apt-get install -y boot-repair && boot-repair

6) Select the fixing of common options and select NO if it asks you if your drive is a removable (well if it isn't).

Solved my problem.

Share:
19,528

Related videos on Youtube

codeling
Author by

codeling

Always trying to improve my skills!

Updated on September 18, 2022

Comments

  • codeling
    codeling over 1 year

    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?

    • Eliah Kagan
      Eliah Kagan about 12 years
      Please provide a complete transcript of all the commands you ran and the messages you got. You can use script and pastebinit with the -i flag for this. Then edit your question to provide either a link to the pastebin, or (if it's only a couple hundred lines or less) the text itself. ...Do you have SSH access to this server from another Ubuntu system (or any Unix-like system)? If so, you may be able to install gparted and run it via ssh -X.
    • codeling
      codeling about 12 years
      I have ssh access, but no X Server installed (and neither need nor wish to do so), but I guess I'd need one to be able to run ssh -X, right? I'll try to provide the exact commands & error messages, but it might take a while to recreate them, I didn't take notes since nothing really vital is on that disk at the moment (for the most part, "just" the backup of backups). Basically I'm doing this out of curiosity - it must be possible, right? But I'm feeling more and more confident that copying the contents, deleting, and recreating is the simplest solution...
    • Eliah Kagan
      Eliah Kagan about 12 years
      For ssh -X, the X server has to be installed on the SSH client, not the SSH server. The vast majority of Unix-like systems with GUI's have what is needed to connect as an SSH client with -X (except Mac OS X, if you didn't install the X11 component). See en.wikipedia.org/wiki/X_server#Design for further explanation.
  • Eliah Kagan
    Eliah Kagan about 12 years
    As is stated in the question, nyarlathotep's partition starts at the end of the drive, so the starting point has to be moved and the technique you propose will not work without data loss.
  • codeling
    codeling about 12 years
    yes, unfortunately doesn't help me (start can't stay the same)
  • m. öztürk
    m. öztürk about 12 years
    Ok, give me a few minutes to think again, I missed the moving starting point, sorry...
  • codeling
    codeling about 12 years
    Thanks! Any idea why gparted can do it but not the command-line based parted? They should be based on the same library (libparted) as far as I know, so why the different functionality?
  • ish
    ish about 12 years
    I can't look into this right now, but I would assume that while libparted provides this functionality, parted may not expose it while gparted does. Maybe try one of the other text front-ends like python-parted (IIRC)?
  • codeling
    codeling about 12 years
    Although it actually sounds a bit paradoxical for linux - there is a graphical tool which does the job but none for the command line ;)
  • m. öztürk
    m. öztürk about 12 years
    Oh, I forgot to mention that I have tested this solution. :)
  • codeling
    codeling about 12 years
    I have vaguely thought in the direction of LVM as well, but I know too little about it at the moment to make any use of it ;). But I guess sooner or later I'll look into it
  • Anders
    Anders about 12 years
    It isn't that hard. You have physical volumes, which are where you store data. Usually a parititon with type set to 8e. They are put into a volume group, which contine one or more physical volumes. You can think of this as a logical disk. From a volume group you can create logical volumes, which you can think of as partitions. It's them you make file systems in and mount. Here is an introduction (from a good site): debian-administration.org/articles/410
  • void.pointer
    void.pointer over 9 years
    This solution is BROKEN. The e2fsck -f /dev/sdb1 step did NOT go well. It was asking me about journals/inodes with data on them, and asking me to clear them, there were hundreds of prompts. This resulted in me losing data.
  • m. öztürk
    m. öztürk over 9 years
    No, it is your partition broken, not the solution. This is why you issued that command, to fix an eventually broken partition before resizing.
  • void.pointer
    void.pointer over 9 years
    My disk has one billion blocks and I only process about fifty million a day. It's simply not worth it to wait on this to finish. I'm advising everyone to stay away from this solution. I was informed by several people that it is indeed possible to resize a partition when its start point needs to change. So this whole thing is pointless.
  • m. öztürk
    m. öztürk over 9 years
    Sorry, but your comment is still nonsense. Sorry that the solution provided didn't worked. Perhaps you should ask someone with deeper knowledge to help you.
  • Vadim Fint
    Vadim Fint about 9 years
    this solution is good, but instead of removing sdb2 after sdb1 it is much easier to sort partitions in direct way using fdisk/gdisk (e.g. fdisk -> x -> f -> w)
  • realtebo
    realtebo over 4 years
    I got command not found after succesfully installing vnc4server. On Debian 10 the command is vncserver