[HOWTO] create ext2 container

Post tutorials, HOWTO's and other useful resources here.
User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[HOWTO] create ext2 container

Post#1 by Rava » 11 Nov 2021, 04:52

You want to transport files to other systems, maybe using USB Thumbdrives, maybe external harddisks, without losing any of the Linux file attributes? You can create a tar archive or a tar.gz or tar.xz every time you need it, but you can also create an ext2 container, meaning a file containing an ext2 filesystem that can be mounted in any Linux OS and then files can be copied in and out of the container.

As example I choose a 50 MB sized container.

Creation of 50MB.ext2 is quite easy. Cave! dd can be run as any user, mkfs.ext2 and tune2fs must be run as root. Only use e.g. tune2fs when the ext2 container is umounted.

# creating: dd

Code: Select all

dd if=/dev/zero of=50MB.ext2 bs=1M count=50
50+0 records in
50+0 records out
52428800 bytes (52 MB, 50 MiB) copied, 5.02623 s, 10.4 MB/s
# creating: mkfs.ext2 (see man mkfs.ext2 for more options. E.g. giving the fs a label)

Code: Select all

mkfs.ext2 50MB.ext2 
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 51200 1k blocks and 12824 inodes
Filesystem UUID: d86ffe69-d893-4d7b-9016-0cada3261660
Superblock backups stored on blocks: 
	8193, 24577, 40961

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done
# tuning: tune2fs (see man tune2fs for more options)

Code: Select all

tune2fs -m 0.1 50MB.ext2
# mounting

Code: Select all

mkdir /TEST
mount 50MB.ext2 /TEST
# check how empty or full it is
df -m /TEST/
Filesystem     Type 1M-blocks  Used Available Use% Mounted on
/dev/loop16    ext2        49     1        48   2% /TEST
When you copied files into your ext2 container, umount it, then copy the container file (in my example that is "50MB.ext2") to your device to transport it, e.g. onto an USB stick, on the other machine either mount it directly from the USB stick or copy the container to the internal harddisk first, then mount it.

Added in 1 day 31 minutes 24 seconds:
I forgot to add:
Like any sane fs, the created ext container's root folder (aka its "/" folder) will be owned by user root group root and be read-only for others.

Let's presume you mounted your ext2 container under /TEST

The possible problem when you want to copy files into /TEST or save downloaded files into /TEST as default guest user is that you cannot, and you also cannot create a new folder in /TEST.

The reason:

Code: Select all

root@porteus:/# ls -oa /TEST/
total 13
drwxr-xr-x  3 root  1024 2021-11-11 03:45 .
drwxr-xr-x 73 root   480 2021-11-11 03:45 ..
drwx------  2 root 12288 2021-11-11 03:45 lost+found
Just do the following as root

Code: Select all

chown guest.users /TEST/.
Results of chown:

Code: Select all

root@porteus:/# ls -oa /TEST/
total 13
drwxr-xr-x  3 guest  1024 2021-11-11 03:45 .
drwxr-xr-x 73 root    480 2021-11-11 03:45 ..
drwx------  2 root  12288 2021-11-11 03:45 lost+found
Now you can save files in /TEST, create folders in /TEST and copy files into /TEST.

(Please be aware that ls -oa omits the group info of shown files and folders. Use ls -la if you want to also see group info)
Cheers!
Yours Rava