Resize Partition and Filesystem Within Disk Image

2

1

I have a RAW disk image which has two partitions, with an ext2 and ext3 filesystem respectively. I want to add a gigabyte to the size of the latter partition, and resize its filesystem. How can I do this?

Duncan X Simpson

Posted 2017-04-27T21:52:04.643

Reputation: 1 171

Hi.  I just rejected your suggested edit to Asterisk/FreePBX behind pfSense – no audio in/out for two reasons: (1) The question seems to be more about Asterisk than it is about FreePBX, so deleting the [tag:asterisk] tag to replace it with [tag:freepbx] seemed inappropriate.  I don’t even know what FreePBX has to do with the question — it’s mentioned only in the title, not in the question body.  (Suggestion: if you want to add a tag to a question, and it already has five relevant tags,  … (Cont’d)

– Scott – 2018-05-31T03:28:40.397

(Cont’d) …  consider deleting the most general one — [tag:networking], in this case.)   (2) You ignored a few problems in the question.  If you’re going to edit a post (a question or an answer), try to improve it as much as you can. – Scott – 2018-05-31T03:28:51.417

@Scott Thank you for the feedback. The part that says "These are the accompanied settings I have on Asterisk:" is incorrect, as that is a FreePBX screenshot. When you use FreePBX, Asterisk answers are less likely to be helpful because FreePBX's interface does ridiculous things to the config files, and any changes you make directly to Asterisk will be overwritten the next time you apply changes in FreePBX. The reason I chose to delete asterisk is because the question is mostly about networking. That being said, I can see asterisk answers being applicable there so I'll make a different edit. – Duncan X Simpson – 2018-05-31T16:24:58.887

Answers

1

First off, there is risk in this process, and things might be slightly different depending on some assumptions I've made which could be wrong. BACK UP YOUR DATA before attempting this.

Note about below : Steps 3 - 6 might be avoidable if you use something like gparted to automate these steps using a GUI.

After backing up, the steps are:

  1. Ensure image is unmounted and not in use.

  2. Expand the size of the virtual disk image. I generally use qemu-resize to do this. You can probably just dd -bs=X count=y >> iso.img to expand the image. The double arrow is very important, a single arrow will nuke your existing image.

  3. Depending on your tools you may or may not need to create a loopback device to convert the raw image into a virtual disk. To do this you can use losetup /dev/loop0 /path/to/block - This will create /dev/loop0 which you can then use instead of referring to the underlying file.

  4. Use your partition manager to resize the disk. I typically use fdisk. The first step here is to dump out the information of the partitions. the second step is to delete the last partition and recreate it using the identical starting block and ensuring the filesystem type attribute is correct, and the bootable flag is set correctly (probably off). You can then write this to disk.

  5. Type kpartx /dev/loop0 - this will create /dev/mapper/loop0pX so you can then access the partitions directly.

  6. use resize2fs /dev/mapper/loop0pX to resize it. Note that this command might be called something else. resize4fs is a common alternative.

  7. Use dmsetup remove /dev/mapper/loop0pX if required

  8. Use losetup -D /dev/loop0 to remove the block device.

davidgo

Posted 2017-04-27T21:52:04.643

Reputation: 49 152

Thanks! The kpartx tool is the key I was missing. Upvoted, and I'll accept once tested. – Duncan X Simpson – 2017-04-27T22:31:48.290

I ended up figuring out a different way to use losetup so it would make the partition devices (I couldn't get kpartx on my Arch box), but this led me to the answer. – Duncan X Simpson – 2017-04-30T13:31:43.647