Skip to content

Instantly share code, notes, and snippets.

@flbuddymooreiv
Last active December 16, 2015 18:23
Show Gist options
  • Save flbuddymooreiv/b980ce3c36520211f774 to your computer and use it in GitHub Desktop.
Save flbuddymooreiv/b980ce3c36520211f774 to your computer and use it in GitHub Desktop.
Expand binary disk image file and its filesystem

Using a file on a filesystem as a filesystem can be an effective way to create a workspace for a long running application to ensure it will not grow to fill a system-level filesystem and adversely affect other applications. Here, we show how to create one of these sandboxes, and how to expand it if the need arises.

This gist assumes a clean Debian 8 install with sudo installed and configured.

buddy@graybook:~/dev$ cd filetest/
buddy@graybook:~/dev/filetest$ ls
buddy@graybook:~/dev/filetest$ fallocate -l 256M image.bin
buddy@graybook:~/dev/filetest$ ls -l
total 262148
-rw-r--r-- 1 buddy buddy 268435456 Dec 16 09:16 image.bin
buddy@graybook:~/dev/filetest$ /sbin/mkfs.ext4 -T news image.bin
mke2fs 1.42.12 (29-Aug-2014)
Discarding device blocks: done                            
Creating filesystem with 65536 4k blocks and 65536 inodes
Filesystem UUID: c4cd810b-060a-4150-97ef-c8dce9cd4b73
Superblock backups stored on blocks: 
	32768

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

buddy@graybook:~/dev/filetest$ mkdir mnt
buddy@graybook:~/dev/filetest$ sudo mount -o loop image.bin mnt/
buddy@graybook:~/dev/filetest$ df | grep mnt
/dev/loop0                       229344      144    210856   1% /home/buddy/dev/filetest/mnt
buddy@graybook:~/dev/filetest$ sudo chown buddy:buddy mnt/ # so a non-superuser can work here
buddy@graybook:~/dev/filetest$ truncate -s +128M image.bin 
buddy@graybook:~/dev/filetest$ ls -l image.bin 
-rw-r--r-- 1 buddy buddy 402653184 Dec 16 09:26 image.bin
buddy@graybook:~/dev/filetest$ df | grep mnt
/dev/loop0                       229344      144    210856   1% /home/buddy/dev/filetest/mnt
buddy@graybook:~/dev/filetest$ sudo umount mnt; sudo e2fsck -f image.bin && sudo /sbin/resize2fs image.bin && sudo mount image.bin mnt
buddy@graybook:~/dev/filetest$ df | grep mnt
/dev/loop0                       372615     2054    346805   1% /home/buddy/dev/filetest/mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment