Mounting Compressed Archives as a User

Why splat a compressed archive in your storage when you can just mount it like a storage device?

I have written previously about userspace filesystems, particularly focusing on SquashFS. Filesystem in userspace (FUSE) is a fantastic way to create targeted functional filesystems without having to get into the Linux kernel. A couple of my earlier articles that mention FUSE address data encryption and working with S3QL object storage. As evident by these applications, FUSE can be very useful, but I want to talk about another tool I’ve been using lately called archivemount.

archivemount

As mentioned before, FUSE can fill the void for niche requirements. One requirement I had was to mount a compressed archive so I could interact with it without having to splat it all over my /home directory. Previously, I would download something like a .tar.gz file, uncompress it, and untar it, so I could look at the documentation and code. I could take a small file, perhaps a few megabytes in size and explode it into a few hundred megabytes or even a few gigabytes! Once I did this, I could then peruse the files and find what I wanted. If I got distracted for a few days, I could easily forget to come back and erase the exploded file. After a few weeks, I might have several of these eating space in my /home directory.

What I needed was a way to look through the files for things I found interesting or useful. (Most of the time the documentation is buried in subdirectories.) Before anyone tells me to just walk through the GitHub repository, I’ve found doing that is a pain and counterproductive. I needed a better way, which led me to archivemount.

Archivemount is a FUSE filesystem that can mount an archive such as a .tar.gz file in a user-owned mountpoint. You can edit files in the mountpoint (the archive), add files, remove files, and so on. To me, this has advantages compared with unarchiving: It’s faster, it doesn't take up too much additional space, and it’s simple.

Installing archivemount

Installing archivemount really depends on your distribution. I'm using Ubuntu 20.04 LTS, so installation is simple:

$ sudo apt-get install archivemount

You might have to build it from scratch if your distribution does not have a prebuilt binary; you can find directions on various sites.

Using archivemount

In this example I begin with the directory of my projects shown in Listing 1, which I then archive. I leave creating a compressed TAR archive of this directory (a .tar.gz file) to you.

Listing 1: Directory of Projects

$ ls -s
total 4548
   4 BCACHE                          4 INTERNET002             4 OCFS2
   4 BEFORE                          4 IOSTAT                  4 PEARC21
   4 BTRFS_TEST                    272 iozone                  4 reiser4_defaults.txt.gz
   4 CFQ                            12 iozone_notes.txt        4 reiser4_notes.txt.gz
   4 CFQ_TUNING                      4 LVM_GUI               184 results.tar.gz
  20 cfq_tuning.tar.gz            1556 lvm_gui.tar.gz          4 runit.fdtree.bash
   4 COLLECTL                        4 LVM_VS_MD               4 SANDFORCE
   4 CRYPT-2                        20 lvm_vs_md_tar.gz     1368 sandforce.tar.gz
  24 et4_64GB_journal.txt            4 MD_ARTICLE              4 SMARTMONTOOLS
   4 ext2_defaults.txt.gz            4 MD_ARTICLE_2            4 squashfs_notes.txt
   4 ext2_notes.txt.gz              12 md_article_2.tar.gz     4 squashfs_notes.txt~
   4 EXT4_JOURNAL_ARTICLE            4 METARATES               4 STORAGE038
 424 ext4_journal_article.tar.gz    48 metarates.tar.gz        4 STORAGE039
  32 fdtree_gkrellm.png              4 NEW-MONITORING          4 TRIM_WORKS
   4 HOME1_DATA                      4 NFS_1                 440 trim_works.tar.gz
   4 INTEL_SSD                       4 note_10_15.txt

After creating the archive, I copied it to my root directory and created a directory in the root of /home named archivetest. I simple used the mkdir command and didn’t alter the default permissions on that directory. Next, I mounted the archive and checked that I could list the contents (Listing 2).

Listing 2: Mount and List the Archive

$ archivemount articles.tar.gz archivetest
$ ls -s archivetest/ARTICLES
total 4406
   0 BCACHE                          0 INTERNET002             0 OCFS2
   0 BEFORE                          0 IOSTAT                  0 PEARC21
   0 BTRFS_TEST                    270 iozone                  4 reiser4_defaults.txt.gz
   0 CFQ                             9 iozone_notes.txt        1 reiser4_notes.txt.gz
   0 CFQ_TUNING                      0 LVM_GUI               184 results.tar.gz
  18 cfq_tuning.tar.gz            1554 lvm_gui.tar.gz          3 runit.fdtree.bash
   0 COLLECTL                        0 LVM_VS_MD               0 SANDFORCE
   0 CRYPT-2                        17 lvm_vs_md_tar.gz     1365 sandforce.tar.gz
  22 et4_64GB_journal.txt            0 MD_ARTICLE              0 SMARTMONTOOLS
   4 ext2_defaults.txt.gz            0 MD_ARTICLE_2            4 squashfs_notes.txt
   1 ext2_notes.txt.gz              12 md_article_2.tar.gz     2 squashfs_notes.txt~
   0 EXT4_JOURNAL_ARTICLE            0 METARATES               0 STORAGE038
 424 ext4_journal_article.tar.gz    47 metarates.tar.gz        0 STORAGE039
  29 fdtree_gkrellm.png              0 NEW-MONITORING          0 TRIM_WORKS
   0 HOME1_DATA                      0 NFS_1                 439 trim_works.tar.gz
   0 INTEL_SSD                       2 note_10_15.txt

The archive could be listed, so I next checked it with mount:

$ mount
...
archivemount on /home/laytonjb/archivetest type fuse.archivemount (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)

The output from the mount command looks correct, so the archive is mounted on the system.

To test that I can write to this archive, I create a file in archivetest/ARTICLE and list the files in the archive to make sure the file is there (sixth line of the output in Listing 3). You can see that the file jeff.test is there, so it was added to archivemount.

Listing 3: List the Archive

$ vi archivetest/ARTICLES/jeff.test
$ ls -s archivetest/ARTICLES
total 4406
   0 BCACHE                          0 INTERNET002             2 note_10_15.txt
   0 BEFORE                          0 IOSTAT                  0 OCFS2
   0 BTRFS_TEST                    270 iozone                  0 PEARC21
   0 CFQ                             9 iozone_notes.txt        4 reiser4_defaults.txt.gz
   0 CFQ_TUNING                      1 jeff.test               1 reiser4_notes.txt.gz
  18 cfq_tuning.tar.gz               0 LVM_GUI               184 results.tar.gz
   0 COLLECTL                     1554 lvm_gui.tar.gz          3 runit.fdtree.bash
   0 CRYPT-2                         0 LVM_VS_MD               0 SANDFORCE
  22 et4_64GB_journal.txt           17 lvm_vs_md_tar.gz     1365 sandforce.tar.gz
   4 ext2_defaults.txt.gz            0 MD_ARTICLE              0 SMARTMONTOOLS
   1 ext2_notes.txt.gz               0 MD_ARTICLE_2            4 squashfs_notes.txt
   0 EXT4_JOURNAL_ARTICLE           12 md_article_2.tar.gz     2 squashfs_notes.txt~
 424 ext4_journal_article.tar.gz     0 METARATES               0 STORAGE038
  29 fdtree_gkrellm.png             47 metarates.tar.gz        0 STORAGE039
   0 HOME1_DATA                      0 NEW-MONITORING          0 TRIM_WORKS
   0 INTEL_SSD                       0 NFS_1                 439 trim_works.tar.gz

Now you can unmount the archive with the umount command:

$ umount archivetest 
$ mount
...

Note that I did not show all of the output from the mount command because it is quite long; however, it does not show archivetest as being mounted, so I need to check the ls output of my /home directory (Listing 4). Notice the new archive, articles.tar.gz.orig, which is slightly smaller than articles.tar.gz.

Listing 4: Check /home Directory

$ ls -s
total 106148
...
    4  archivetest                                           8  new_system_notes.txt
15620  articles.tar.gz                                       4  notes.txt
15568  articles.tar.gz.orig                                  4  NV-DCGM
...

The orig archive is the original archive, and articles.tar.gz is the new archive that contains the file I added. I really like this archivemount feature because it lets me keep both copies, if I want both, or it allows me to delete the original archive (or I could delete the new archive).

ZIP Example

When working with new applications, rather than clone a repository in GitHub, I like to download the current state of the code into a .zip file. Therefore, I decided to check whether archivemount worked with ZIP files.

To begin, I created a ZIP archive of the same directory as in the previous example; then, I mounted the ZIP archive with the archivemount command and listed the contents of the mountpoint. As the output in Listing 5 shows, you can mount ZIP archives, which is helpful when interacting with GitHub code.

Listing 5: Mount and List ZIP Archive

$ archivemount articles.zip archivetest
$ ls -s archivetest/ARTICLES/
total 4406
   0 BCACHE                          0 INTERNET002             0 OCFS2
   0 BEFORE                          0 IOSTAT                  0 PEARC21
   0 BTRFS_TEST                    270 iozone                  4 reiser4_defaults.txt.gz
   0 CFQ                             9 iozone_notes.txt        1 reiser4_notes.txt.gz
   0 CFQ_TUNING                      0 LVM_GUI               184 results.tar.gz
  18 cfq_tuning.tar.gz            1554 lvm_gui.tar.gz          3 runit.fdtree.bash
   0 COLLECTL                        0 LVM_VS_MD               0 SANDFORCE
   0 CRYPT-2                        17 lvm_vs_md_tar.gz     1365 sandforce.tar.gz
  22 et4_64GB_journal.txt            0 MD_ARTICLE              0 SMARTMONTOOLS
   4 ext2_defaults.txt.gz            0 MD_ARTICLE_2            4 squashfs_notes.txt
   1 ext2_notes.txt.gz              12 md_article_2.tar.gz     2 squashfs_notes.txt~
   0 EXT4_JOURNAL_ARTICLE            0 METARATES               0 STORAGE038
 424 ext4_journal_article.tar.gz    47 metarates.tar.gz        0 STORAGE039
  29 fdtree_gkrellm.png              0 NEW-MONITORING          0 TRIM_WORKS
   0 HOME1_DATA                      0 NFS_1                 439 trim_works.tar.gz
   0 INTEL_SSD                       2 note_10_15.txt

By the way, I tested writing to the archive and unmounting it. I was successful in adding files to the archive and issuing archivemount to save the new archive as articles.zip.orig.

Summary

I find that archivemount allows me to examine what is in archive files without having to splat the archive in my /home directory. For me, this works particularly well with ZIP archives from GitHub.

In a previous article I showed you how to create simple scripts that walk user directories and compress files or directories that are old and have not been accessed in a long period of time. The archivemount tool would then allow you to mount and interact with those archives.