Mount has the option offset
to specify that a file system does not start at the beginning of a device but some specific amount of bytes after.
How can I use resize2fs
, which does not have that option, to resize such a file system which does not start at the device's beginning?
2 Answers
The offset
option of mount
does not get passed to mount directly, but to losetup
which sets up a loop device which refers to the offsetted location of the underlaying block device. Mount then performs its operations on that loop device rather than the raw block device itself.
You can also use losetup
to make resize2fs
play which such file systems:
# losetup --offset=<offset> --find --show /dev/<device>
/dev/loop0
# resize2fs /dev/loop0 <newsize>
# losetup --detach /dev/loop0
(Example may not be complete in means of resize2fs operations)
losetup
searchs for the first free loop device (in that example /dev/loop0) as --find
was passed. --show
outputs that loop device to STDOUT.
Before I extended an ext4 filesystem from the free space of the beginning of the block device, I performed a search to see how others did it and it looks like your question was the closest match I could find. But the provided answer is not exactly what you have asked or I needed. Because I have just managed to resize/grow from the beginning of the disk successfully, I decided to share my thoughts.
The procedure in a nutshell:
1.) The filesystem to be grown/extended: ext4 filesystem on a partition starting at 1026048 sector and ending at the end of the block device (/dev/sdb
in my case)
2.) To allocate the unused sectors at the beginning of the block device for this filesystem, I had to move the data using dd
to the start of my future/new partition
3.) Delete the old partition and recreate it with the new start sector
4.) resize2fs
to extend to the new partition size
Commands used and details on the method:
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
(Above info revealed by fdisk -l /dev/sdb
.)
Calculate dd
seek and skip offsets and bs
(in our case bs
matches sector size):
# dd seek=2048 skip=1026048 if=/dev/sdb bs=512 of=/dev/sdb
So we want to move our data from sector 1026048 and onwards to start at sector 2048 where our new partition is going to start. We should not be able to lose anything as the more we read from the input, the further we get, so by the time we start overwriting with the output (it will eventually reach sector 1026048), we would have copied what was there at the beginning of the device and move on.
Then use fdisk
to delete the old partition and recreate it to start at sector 2048.
Once that is done, you can run:
# partprobe /dev/sdb or
# blockdev --rereadpt /dev/sdb
# resize2fs /dev/sdb<num>
to extend the filesystem.
Please, always make backups of your data before doing low-level procedures like that. Failure in some of the steps or system crash or power failure will result in data loss. This is an example which describes a single partition on a block device. Your setups may vary, so do not use as it is. Learn and understand how it works, then test, and if you get the expected results, run on your live data at your own risk!
Hope that helps others as well. Feedback and where to improve/make more verbose is welcome.
Thank you!