Does anyone know if there exists a tool that can convert tarballs to filesystems and back. I know you can make a loopback device, but it can be pretty hard/impossible to do inside a container, and often requires special flags and privileges.
You can extract the tarball to a directory, then run mkfs.ext4 -d DIRECTORY FILENAME BLOCKS to create a filesystem[0]. You'll need to know how many blocks your filesystem needs to be in advance.
Unfortunately, mkfs.ext4 only works on Linux. There is no port for other operating systems.
I've been doing something similar recently, though it doesn't directly convert a tar to an ext4 FS. But maybe this can help you get to where you want to be.
On Alpine Linux:
```
apk add --no-cache coreutils e2fsprogs
```
```
#!/bin/sh
# Untar the tar file
mkdir -p /tmp/my_untarred_files_dir
tar -xvf my_tar_file.tar -C /tmp/my_untarred_files_dir
# Make an empty image file.
dd if=/dev/zero of="fs.raw" bs=1M count=1024
# Format the file as ext4 (with journaling) and copy untarred files into it
Comments
Does anyone know if there exists a tool that can convert tarballs to filesystems and back. I know you can make a loopback device, but it can be pretty hard/impossible to do inside a container, and often requires special flags and privileges.
For squashfs there is `tar2sqfs`[1] and `sqfs2tar`[2] (and now also `sqfstar`[3]) that can go from tarball to filesystem and back again.
For ext4, I recently saw a patch set on the mailing list[4].
Not sure about other filesystems tough.
[1] https://manpages.debian.org/unstable/squashfs-tools-ng/tar2s...
[2] https://manpages.debian.org/unstable/squashfs-tools-ng/sqfs2...
[3] https://manpages.debian.org/unstable/squashfs-tools/sqfstar....
[4] https://lore.kernel.org/linux-ext4/20230812150204.462962-2-j...
You can extract the tarball to a directory, then run mkfs.ext4 -d DIRECTORY FILENAME BLOCKS to create a filesystem[0]. You'll need to know how many blocks your filesystem needs to be in advance.
Unfortunately, mkfs.ext4 only works on Linux. There is no port for other operating systems.
[0] https://github.com/cedws/concrete-ubuntu/blob/0ae3f076c5a20d...
If you want to create read-only, use ext2. No point in using ext4 if you don't want journaling.
Extents are a useful feature even for readonly although the gain is smaller.
You can also use ISO9660.
I've been doing something similar recently, though it doesn't directly convert a tar to an ext4 FS. But maybe this can help you get to where you want to be.
On Alpine Linux:
```
apk add --no-cache coreutils e2fsprogs
```
```
#!/bin/sh
# Untar the tar file
mkdir -p /tmp/my_untarred_files_dir
tar -xvf my_tar_file.tar -C /tmp/my_untarred_files_dir
# Make an empty image file.
dd if=/dev/zero of="fs.raw" bs=1M count=1024
# Format the file as ext4 (with journaling) and copy untarred files into it
mke2fs -t ext4 -j -d "/tmp/my_untarred_files_dir" "fs.raw"
```
If you want to make a qcow2 image, you can then do this:
```
apk add --no-cache qemu-img
qemu-img convert -O qcow2 "fs.raw" "fs.qcow2"
```
7z can list and extract from ext4 images. Respectively:
$ 7z l <image>
$ 7z x <image>
You can use fuse2fs to mount any ext[24] file system in user space. You can even use "-o fakeroot" to get complete control over UIDs and modes.
Maybe you are looking for something like fsarchiver?
https://www.fsarchiver.org
guestfish and libarchive are sort of in this space. Neither is a canned tool for exactly this.