To do this correctly you need to:
- First expand the
.img
file.
- Next expand the filesystem within.
The best way to do the first thing is with dd
. For whatever reason some people attribute a kind of mystery to the way dd
works, but there really is none. For example, to append a hole to the end of your .img
file:
dd bs=1kx1k seek=100 of=.img </dev/null
On any POSIX system that will truncate the file to 100MiBs. On a GNU system the 1kx1k
bit can be shortened to just M
. dd
seeks 100MiBs into the file, encounters EOF on its first read, and closes the file. It is a single action, and requires no reads (beyond the first empty one) or writes - it's very nearly atomic.
If the file was 50MiBs before, it will now be allocated 50MiBs more. If the file was 150MiBs before, that will chop the last 50MiBs off the tail. On a filesystem which understands sparse files, the appended file hole will use no disk-space, really, and will only use what is necessary as you fill it.
Other ways to do the same on some systems:
fallocate -l100M .img
truncate -s100M .img
...both of those commands will do the exact same thing dd
will. I recommend dd
because neither of those tools is portable where dd
's behavior is POSIX-spec'd, and once you learn how to use the disk-destroyer properly, no disk will ever again dare to stand in your way.
If you are merely adding to your .img
you can do the above thing whether or not it is mounted (though if you were to take some of a mounted .img
away it may not work as expected), but you will very likely need to umount
.img
first to resize its constituent filesystem anyway, and so might as well. You do not need to -d
estroy the loop device, though.
How you handle the second thing depends on whether .img
is partitioned or not. If it is not, as I guess is the case based on your comments elsewhere, then you'll only need to address the fs by its type. For an ext[234]
.img
file you should use resize2fs
and be done with it. For others you'll want to look at the relevant user-space tools and their man
pages.
If .img
is partitioned it can be more complicated. In such cases how you handle the situation will depend on what kind of partition table is used (such as GPT vs MBR vs hybrid-MBR), whether it is the last partition in the file's partition table and much else. I hesitate to venture any specifics here without more information: if you require advice on how to handle a partitioned .img
please let me know with some more details and I will offer what I can.
df -h
command and yes what @StephenKitt asked too! – Munai Das Udasin Jul 19 '15 at 15:14mount -oloop file /path
that does the losetup bit for you automatically. – sourcejedi Jul 20 '15 at 20:24