Friday, March 11, 2016

Enlarging an ext2 partition (and filesystem) without data loss on Centos 6

I have tested the procedure using qemu-kvm.

qemu-kvm -boot once=d -cdrom CentOS-6.7-i386-LiveCD.iso -hda centos_2G.img -m 2G

Using a live CD image, to do the resizing, and a USB flash disk image as the test subject.

The general procedure described for doing this is as below -

Check that filesystem is OK
e2fsck /dev/sda1

Use fdisk to delete and recreate the partition, in place, but with increased size
fdisk /dev/sda
p - to show table, note details
d - to delete partition
n - to create new partition, using details from before, increasing the end block as needed
a - to toggle boot flag
w - to write partition table back to disk

Re-check that filesystem is OK
e2fsck /dev/sda1

At this point the process fails for me, with the error -
Superblock invalid
Bad magic number in super-block

and from here I have not found a way to recover the filesystem.
I have tried a similar process using parted, and also failed.

Using gdisk (GPT) instead of fdisk

[root@livecd ~]# e2fsck /dev/sda1
e2fsck 1.41.12 (17-May-2010)
/dev/sda1: clean, 18823/65536 files, 209783/261888 blocks

[root@livecd ~]# yum install gdisk
[root@livecd ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present


***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************

Command (? for help): p
Disk /dev/sda: 3913728 sectors, 1.9 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): F2EA2B62-4D1C-4D0A-8274-24009DC5353D
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 3913694
Partitions will be aligned on 2048-sector boundaries
Total free space is 1818557 sectors (888.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2097151   1023.0 MiB  8300  Linux filesystem

Command (? for help): d
Using 1

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-3913694, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-3913694, default = 3913694) or {+-}size{KMGTP}: 1700M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): x

Expert command (? for help): a
Using 1
Known attributes are:
0: system partition
1: hide from EFI
2: legacy BIOS bootable
60: read-only
62: hidden
63: do not automount

Attribute value is 0000000000000000. Set fields are:
  No fields set

Toggle which attribute field (0-63, 64 or to exit): 2
Have enabled the 'legacy BIOS bootable' attribute.
Attribute value is 0000000000000004. Set fields are:
2 (legacy BIOS bootable)

Toggle which attribute field (0-63, 64 or to exit): 

Expert command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.
[root@livecd ~]# e2fsck -f /dev/sda1
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 3A: Optimizing directories
Pass 4: Checking reference counts
Pass 5: Checking group summary information

/dev/sda1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda1: 18823/65536 files (0.7% non-contiguous), 209783/261888 blocks


[root@livecd ~]# resize2fs /dev/sda1
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sda1 to 434944 (4k) blocks.
The filesystem on /dev/sda1 is now 434944 blocks long.

[root@livecd ~]# e2fsck /dev/sda1
e2fsck 1.41.12 (17-May-2010)
/dev/sda1: clean, 18823/114688 files, 212932/434944 blocks

All good, now reboot, and see that the image boots successfully.

[root@usbhda ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       1.7G  804M  783M  51% /
tmpfs           947M     0  947M   0% /dev/shm


No comments: