March 12, 2013

How to resize FAT partition in Linux without losing data

I have a small external hard drive that had three partitions on it. One I created for TimeMachine backups, one for keeping movies and one that I was not aware of. It had a boot flag so I suspect it was created by TimeMachine itself.

So once I got a bigger HD just for TimeMachine I no longer needed the allocated 100GB on that older drive. Getting rid of it however without losing data seemed a bit too complicated in Mac OS X. I was also too lazy to back up my data so I could just reformat the whole drive with Disk Utility.

Couple of days ago I finally set up my headless home media server running Ubuntu 12.04 LTS (more on this later) and I thought it’s a good time to revisit the partitioning problem.

GParted was the first obvious thing, except I had no X11 installed. GParted has a command line interface as well, but installing all those gtk lib dependencies just for that didn’t seem reasonable.

A lot of searches later I wasn’t still much smarter. There are many ways how to accomplish the task, but none of them actually said that you might not even experience any data loss.

So I had to pull myself together and start with the backup. After that I decided to try my luck with Parted. It came with the server and should work on pretty much on all Linux distros. I have version 2.3 and I think it’s the latest that works, because in the Parted user manual they say: “Note that after version 2.4, the following commands were removed: check, cp, mkfs, mkpartfs, move, resize.”

So this was my ‘fdisk -l’ output:

    Disk /dev/sdb: 250.1 GB, 250059350016 bytes
    255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x8d7fe87c

    Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1      409639      204819+  ee  GPT
    /dev/sdb2          409640   195722143    97656252   af  HFS / HFS+
    /dev/sdb3       195984288   488134983   146075348    b  W95 FAT32

I needed to preserve /dev/sdb3, have all my files still accessible and extend the size of the partition form 150GB to the whole disk 250GB.

I started with unmounting everything (every command below was performed as a root):

    umount /dev/sdb1
    umount /dev/sdb2
    umount /dev/sdb3

Then I started Parted with ‘parted /dev/sdb’. After typing ‘print’ I saw:

    Model: WDC WD25 00BEVT-00SCST0 (scsi)
    Disk /dev/sdb: 250GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt

    Number  Start   End    Size   File system  Name                   Flags
    1      20.5kB  210MB  210MB  fat32        EFI System Partition   boot
    2      210MB   100GB  100GB  hfs+         MacBook
    3      100GB   250GB  150GB  fat32        DOS_FAT_32_Untitled_2

Ok, let’s remove unnecessary partitions:

    (parted) rm 1
    (parted) rm 2
    (parted) print
    Model: WDC WD25 00BEVT-00SCST0 (scsi)
    Disk /dev/sdb: 250GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt

    Number  Start  End    Size   File system  Name                   Flags
    3      100GB  250GB  150GB  fat32        DOS_FAT_32_Untitled_2

That was easy. Now I just closed my eyes, prayed and typed:

    (parted) resize 3
    WARNING: you are attempting to use parted to operate on (resize) 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.
    Start?  [100GB]? 0GB
    End?  [250GB]?

    (parted) print
    Model: WDC WD25 00BEVT-00SCST0 (scsi)
    Disk /dev/sdb: 250GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt

    Number  Start   End    Size   File system  Name                   Flags
    3      17.4kB  250GB  250GB  fat32        DOS_FAT_32_Untitled_2

    (parted) quit

Resize worked nicely. I mounted the drive and were much pleased. All my data still there and accessible. I hope you will be as fortunate as me (:

Powered by Hugo & Kiss.