Score:2

Can we create two different filesystems on a single partition?

cw flag

I'm currently using Ubuntu 20.04 on my machine.

  1. Can we create two different filesystems on a single partition? Why?
  2. Also if the size of the filesystem created in a partition is less than the partition size, can we extend the filesystem size in the future?
guiverc avatar
cn flag
A partition usually has a single file-system only on it; though its possible to have others thru '*tricks*'. I've an old laptop where I didn't want to erase the windows & NTFS partition; thus I create a EXT *file-system* within a file stored on that NTFS partition & boot the system using external media. You can also store *file-systems* within VMs etc that are stored as files on the host partition etc.. (FYI: *I'd call both my examples a single file-system on a partition, but technically there are also more than one*)
oldfred avatar
cn flag
If you used LVM or LVM with encryption it uses one entire partition. Default install does not expand / volume to full size of partition, so you have options to create separate volumes or expand it. Post this: `lsblk -e 7 -o name,fstype,size,fsused,label,UUID,mountpoint`
Peter Cordes avatar
fr flag
2. Yes, most FSes come with a resize tool, and normally it works by growing the FS to fill the block device it's on, if you don't specify a smaller size. e.g. `xfs_growfs`. https://man7.org/linux/man-pages/man8/xfs_growfs.8.html#PRACTICAL_USE
Score:13
vn flag

Disclaimer: Somewhat philosophical elaboration on what's out of scope from @Raffa's answer.

As is the case with advances in technology, the clear borders of old definitions are still becoming more blurred. Traditionally, one partition is formatted with one filesystem - and this is still normally the case.

However new technologies give rise to new opportunities. Here are a few:

  • Virtual machines host virtual filesystems inside other files, so it's possible to have several nested filesystems in this way.
  • Even .iso or .img files that contains disk images of CD or DVD media are in a similar way nested and virtual filesystems.
  • The same is true for containers. For instance, Docker containers have a separate overlay filesystem (special filesystem with special mount) on top of a static container image (file).
  • ZFS can use files as block devices - so you can again create a "nested" ZFS filesystem on top of another filesystem (will mostly make sense for testing purposes).
  • And I'm sure there are many other examples I don't know about.

At the same time, additional technologies like LVM (and also ZFS) exist to do the opposite - to let a single filesystem span several partitions or block devices.

So at the end, only imagination sets the limit for what's possible using the right tools. The hard part is to clearly identify your needs, and find the right tool to do it.

Raffa avatar
jp flag
For completeness … It is theoretically possible to actually have two or more filesystems on the same end partition although might require a lot of plumbing … Then mount and use each one of them specifying the start sector of a certain filesystem as the argument to `mount`s `-o offset=` and the filesystem type as the argument to `-t` and the driver should be able to detect where the filesystem ends … But that’s not a recommended solution for the common user IMO as it’s prone to all sorts of problems including potential data loss not to mention usability/performance downside +1
Artur Meinild avatar
vn flag
@Raffa that sounds ridiculously complex and as you say, probably not something that has many uses in real life (unless under highly specialized circumstances I can't really imagine for now)!
ilkkachu avatar
co flag
Don't even need all those virtualism machines and all those other fancy new toys all the youngsters hip on about... even an ISO image file (or a floppy disk image) is a filesystem in a file.
cm flag
And then there is isohybrid which is not exactly what the OP asked about but it's pretty close, isohybrid with UEFI is a hybrid of an iso9660 filesystem for optical disk boot, a PC boot sector for traditional PC boot from HD-media and a GPT partition table with a fat16 partition for UEFI boot from HD-media.
filo avatar
it flag
For more completeness … brtfs volumes (essentially behave like independent filesystems and can physically reside on a single partition, eg. for snapshots) + BSD slices (not exactly Linux).
Score:5
jp flag

Assuming, by "partition", you mean the last level defined and continuous group of physical sectors on a hard disk ... That's a mouthful, I know ... But partitions can be actually containers of other partitions e.g. an extended partition(containing logical partitions) or an LVM physical volume(containing logical volumes) ... Those are out of the scope of this answer ... This answer's scope is limited to those end partitions that can be formatted with a filesystem(on "bare metal/sectors" not on another filesystem) and mounted under your Operating System as data(files under a directory structure) storage volumes.

Can we create two different filesystems on a single partition? Why?

No, (I mean: practically usable by an OS user ... Excluding research/forensic/data recovery situation).

To create and utilize(mount and use) a filesystem, physical/logical boundaries must be defined/identifiable either by means of the partition table or the actual whole disk boundaries(yes, a hard disk can be formatted, mounted and used without any partition at all ... although in this case you might need to specify a filesystem type when mounting it) ... The kernel reads the partition table on the disk to decide partitions boundaries, partition types and filesystems ... etc. ... A partition table might contain only one filesystem type for each partition ... When a partition gets mounted, the kernel needs to have a module/driver for the filesystem type on that partition or otherwise it wont be usable ... The kernel can only associate one single filesystem module/driver per partition/mount point.

Also if the size of the file system created in a partition is less than the partition size, can we extend the file system size in the future?

Yes, this is a common practice for example when an underlying partition with an existing filesystem is manually enlarged to utilize unused neighboring disk sectors/space ... Then the filesystem can be extended to fill the new extra space in the partition ... You, however, need to use filesystem specific tools for this task like e.g. resize2fs for ext2, ext3, and ext4 filesystems.

Score:4
cn flag
pts

In this answer I'm attempting answer the following question: is it possible to create and use two different filesystems directly on the same partition, starting at offset 0 (beginning of the partition), sharing the file data between the two filesystems?

For most practical purposes, the answer is no. For read-only use, it's possible (see below), but not practical, because a single filesystem is enough: most modern operating systems can read a UDF filesystem (ufs) or an exFAT filesystem. For read-write use, it's not yet possible, because for writing the operating system would have to maintain filesystem metadata of two filesystems at the same time, and popular operating systems don't have such a multi-filesystem driver. A driver or tool for writing those filesystems would have to be written, there are no such tools I know of readily available.

However, for academic, hacking and demonstration purposes, it's possible to have two filesystems at the beginning of a partition, creating and populating both at the same time, and after that using both as read-only, on Linux mounting at most one filesystem at the same time, and specifying the filesystem type when mounting (e.g. mount -o ro -t ext2 and mount -o ro -t vfat). Below I demonstrate this with a proof-of-concept which creates 2 filesystems, both correct (no fsck errors) and empty.

Filesystem type, size and statistics:

  • The first filesystem is ext2 (could easily be ext3, ext4 or even minix), the second filesystem is FAT16 (could easily be FAT32 or exFAT).

  • FAT16 data cluster size == ext2 block size == 4 KiB

  • FAT16 sector size == 512 bytes, for compatibility with old FAT16 drivers

  • partition (disk image) size == filesystem size == 256 MiB == 65536 blocks * 4 KiB per block

  • 65536 ext2 blocks in 2 ext2 block groups (32768 blocks each)

    maximum file size == 65296 * 4 KiB == 267452416 bytes

    65296 blocks of data in the file

    the inode itself points to 12 file data blocks

    64 indirect blocks, each pointing to 1024 file data blocks (each 1 block number == 4 bytes)

    1 doubly-indirect block, pointing to the 64 indirect blocks (each 1 block number == 4 bytes)

    total number of data blocks == 65296 + 64 + 1 == 65361

  • 65536 4-KiB blocks in FAT16: 3 reserved blocks + 32 FAT blocks + 1 root directory block + 139 data clusters marked as bad + 65361 good data clusters

    maximum file size == 65361 * 4 KiB == 267718656 bytes =~ 255.316 MiB

Run these commands on Linux to create filesystem image bothsh.img:

dd if=/dev/zero bs=1M count=256 of=bothsh.img
# `*4' to convert from 4-KiB blocks to 1-KiB blocks.
(seq $((36*4)) $((106*4)) && seq $((32768*4)) $((32835*4))) >bb256m_fat16.lst
mkfs.fat -v -s 8 -S 512 -f 1 -F 16 -r 128 -R 24 -l bb256m_fat16.lst bothsh.img
# Copy (save) the FAT16 superblock, mke2fs will overwrite it.
dd if=bothsh.img of=bothsh.img.sb.tmp bs=512 count=1
# Copy (save) the FAT16 FAT, mke2fs will overwrite it.  
dd if=bothsh.img of=bothsh.img.fat.tmp bs=4K count=32 skip=3
seq 3 35 >bb256m_ext2.lst  # Counts 4 KiB blocks.
mke2fs -t ext2 -b 4096 -m 0 -O ^resize_inode -O ^dir_index -O ^sparse_super -I 128 -i 65536 -l bb256m_ext2.lst -F bothsh.img
dumpe2fs bothsh.img
# Restore the FAT16 superblock.
dd if=bothsh.img.sb.tmp of=bothsh.img bs=512 count=1 conv=notrunc
# Restore the FAT16 FAT.
dd if=bothsh.img.fat.tmp of=bothsh.img bs=4K count=32 conv=notrunc seek=3
rm -f bothsh.img.sb.tmp bothsh.img.fat.tmp
rm -f bb256m_fat16.lst bb256m_ext2.lst

See full Linux shell script at https://github.com/pts/mkfs_multi/blob/master/mkfs_ext2_fat16_shared_256m.sh

Filesystem layout:

  • ext2 block group 0: (ext2 blocks 0..32767)
    • block 0:
      • first 512 bytes: FAT16 superblock (BPB, boot sector), ignored by ext2 (because ext2 ignores the first 1024 bytes of the filesystem)
      • next 512 bytes: ignored by FAT16 (because it's in a reserved sector) and ext2 (because ext2 ignores the first 1024 bytes of the filesystem)
      • next 1024 bytes: ext2 primary superblock (always at offset 1024), ignored by FAT16 (because it's in a reserved sector)
      • last 2048 bytes: ignored by FAT16 (because it's in a reserved sector) and ext2 (because the next ext2 block starts at offset 4096)
    • block 1: ext2 group descriptors, ignored by FAT16 (because it's in a reserved sector)
    • block 2: ext2 block bitmap (1 bit per block in this ext2 block group), ignored by FAT16 (because it's in a reserved sector)
    • block 3..34: marked as bad block in ext2, FAT16 FAT (65536 * 2 bytes: room for 65536 data clusters, 2 bytes per data cluster)
    • block 35: marked as bad block in ext2, FAT16 root directory (128 * 32 bytes: room for 128 entries, 32 bytes per entry)
    • block 36: ext2 inode bitmap, marked as bad block in FAT16, first FAT16 data cluster (data cluster 2) starts here
    • block 37..100: ext2 inode table (2048 * 128 bytes: room for 2048 inodes, 128 bytes per inode, 11 inodes in use, why so many?), marked as bad block in FAT16
    • block 101: ext2 data block containing directory entries for the root directory (/), marked as bad block in FAT16
    • block 102..105: ext2 data block containing directory entries for the /lost+found directory, marked as bad block in FAT16
    • block 106: ext2 data block containing indirect block used by inode <1> (bad blocks), marked as bad block in FAT16
    • block 107..32767: free data blocks in both ext2 and FAT16 (32661 blocks)
  • ext2 block group 1: (ext2 blocks 32768..65535)
    • block 32768: ext2 backup superblock, marked as bad block in FAT16
    • block 32769: ext2 group descriptors (why do we need it here?), marked as bad block in FAT16
    • block 32770: ext2 block bitmap (1 bit per block in this ext2 block group), marked as bad block in FAT16
    • block 32771: ext2 inode bitmap, marked as bad block in FAT16
    • block 32772..32835: ext2 inode table (2048 * 128 bytes: room for 2048 inodes, 128 bytes per inode, all free), marked as bad block in FAT16
    • block 32836..65535: free data blocks in both ext2 and FAT16 (32700 blocks)

ext2 inodes (128 bytes each):

  • inode <0>: 0 is an invalid inode number, it's not even stored in the filesystem
  • inode <1> == EXT2_BAD_INO at offset 151552: bad blocks: mode == 0, size == 135168 (total number of bytes in 33 bad blocks), 33 data blocks: (0..11):3..14, (12..32):15..35, 1 indirect block: 106 (contains the block numbers 15..35, 4 bytes each)
  • inode <2> == EXT2_ROOT_INO at offset 151680: directory /: mode == 0x41ed, size == 4096, 1 data block: (0):101
  • inode <3> == EXT2_ACL_IDX_INO == EXT4_USR_QUOTA_INO at offset 151808: mode == size == 0, unused, not defined in ext2.h
  • inode <4> == EXT2_ACL_DATA_INO == EXT4_GRP_QUOTA_INO at offset 151936: mode == size == 0, unused, not defined in linux/fs/ext2/ext2.h
  • inode <5> == EXT2_BOOT_LOADER_INO at offset 152064: mode == size == 0, unused, not defined in linux/fs/ext2/ext2.h
  • inode <6> == EXT2_UNDEL_DIR_INO at offset 152192: mode == size == 0, unused
  • inode <7> == EXT4_RESIZE_INO at offset 152320: mode == size == 0, unused, reserved group descriptors
  • inode <8> == EXT4_JOURNAL_INO at offset 152448: mode == size == 0, unused
  • inode <9>..<10> at offset 152576: mode == size == 0, unused, remaining reserved inode (there are 10 in total)
  • inode <11> at offset 152832: directory /lost+found: mode == 0x41c0, size == 16384 (4096 could be enough, or no lost+found at all, but mke2fs leaves enough room free on purpose), 4 data blocks: (0..3):102-105
  • inode <12>..<2048> at offset 152960: free inodes
  • inode <2049>..<4096> at offset 134234112: free inodes

ext2 directory entries (dentry):

  • for inode <2> (/):
    • data block 101 at offset 413696:
      • inode <2> (.) 12 bytes: inode=<2> size=12 name_size=1 type=2=DT_DIR name="."
      • inode <2> (..) 12 bytes: inode=<2> size=12 name_size=2 type=2=DT_DIR name=".."
      • inode <11> (lost+found): inode=<11> size=4072 name_size=10 type=2=DT_DIR name="lost+found"
  • for inode <11> (/lost+found):
    • data block 102 at offset 417792
      • inode <11> (.) 12 bytes: inode=<11> size=12 name_size=1 type=2=DT_DIR name="."
      • inode <2> (..) 4084 bytes: inode=<2> size=4084 name_size=2 type=2=DT_DIR name=".."
    • data block 103 at offset 421888: empty
      • empty: 4096 bytes: inode=0 size=4096 name_size=0 type=0=DT_UNKNOWN
    • data block 104 at offset 425984: empty
      • empty: 4096 bytes: inode=0 size=4096 name_size=0 type=0=DT_UNKNOWN
    • data block 105 at offset 430080: empty
      • empty: 4096 bytes: inode=0 size=4096 name_size=0 type=0=DT_UNKNOWN

One way to populate the filesystem with files (needs software development):

  • Mount the ext2 filesystem as read-write. Do all modifications. Unmount the ext2 filesystem.
  • Run the recreator tool, which does a recursive listing on the ext2 filesystem (read-only), recreates and writes all the FAT16 metadata based on the listing, and it marks some ext2 blocks (corresponding to FAT16 subdirectory clusters) as a bad block. The recreator tool doesn't exist yet, but it can be written given enough motivation. it will work like this:
    • It reads and analyzes the ext2 superblock, the ext2 block groups and FAT16 superblock (BPB, boot sector), and fails if they don't correspond to each other.
    • It does a recursive listing on the ext2 filesystem without mounting it. (It understands the metadata.) As part of the recursive listing, it discovers the data block list of each regular file.
    • It marks most ext2 bad blocks as free, removing them from the list of bad blocks. The only remaining ext2 bad blocks are those which correspond to the FAT16 FAT and the FAT16 root directory.
    • It creates an empty FAT16 FAT with all clusters free. Based on the ext2 block bitmap, it marks all used ext2 blocks as a bad block in the FAT16 FAT. It overwrites the FAT16 FAT accordingly.
    • For each regular file discovered during the recursive ext2 listing, it builds a FAT16 FAT data cluster chain containing the ext2 file data blocks (not the ext2 indirect blocks) of that file. It updates or overwrites the FAT16 FAT accordingly.
    • Based on the directories and regular files discovered during the recursive ext2 listing, it builds the FAT16 long filenames (VFAT, UTF-16, UCS-2) and directory entries from scratch in memory, and it organizes them into FAT16 data clusters (at least one cluster per subdirectory, with a special cluster offset for the root directory). It writes the FAT16 data clusters and the root directory. For each subdirectory, it builds a FAT16 FAT data cluster chain. It updates or overwrites the FAT16 FAT accordingly. It marks each FAT16 data cluster (used by newly created FAT16 subdirectories) as an ext2 bad block. For that it may have to extend the ext2 inode storing the list of bad blocks with more ext2 file indirect blocks. For each new ext2 file indirect block, it marks the corresponding FAT16 cluser as a bad block in the FAT16 FAT.
    • Maybe some of the ext2 operations above can be done by using the debugfs tool, thus making the implementation of the recreator tool simpler. However, the recreator tool needs to fully understand the FAT16 filesystem (which is relatively easy).

Maximum filesystem size with this technique:

  • Use FAT32 instead of FAT16.
  • Maximum FAT32 FAT is limited by the ext2 block group size: each group can contain up to 32695 data blocks (with the current inode ratio), if we mark them all bad in ext2, then we have 32695 4 KiB-blocks for the FAT32 FAT + root directory, thus 32694 4-KiB blocks for the FAT32 FAT, thus (32694 * 4096 / 4) - 2 == 33478654 FAT32 data clusters, thus 33478654 * 32 KiB == 1071316928 bytes =~ 0.99774 TiB of free data.
  • 0.99774 TiB is possible for both ext2 and FAT32.
  • Using exFAT instead of FAT32 doesn't help, the ext2 block group limit above still applies.
Gyrfalcon avatar
gq flag
An example from real life: Zip drives (https://en.wikipedia.org/wiki/Zip_drive) were delivered with one driver/utility disk formatted for Windows as well as Mac using one partition. After the first boot it was then only usable for the system where it had been booted.
cn flag
pts
@Gyrfalcon: This is possible because the HFS filesystem on the Mac has its superblock at offset 1024, and FAT has its superblock at offset 0, and it's possible to organize other blocks to avoid the other filesystem.
Score:1
lk flag

I believe that the Linux kernel separates "Filesystem" logic from "Block device" logic (some filesystems like NFS and FUSE do not use a block devices). I am calling that out because it helpful to understand the distinction between a block device and a file system.

  • A block device is just a contiguous block of bytes.
  • Most filesystems take the block device and use it to "back" the filesystem (set of directories, files, etc) that they expose.

Also if the size of the filesystem created in a partition is less than the partition size, can we extend the filesystem size in the future?

I think it is safe to assume that all filesystems start at (are formatted to start at) the beginning of the block device. However they may end short of, the end of the block device. For filesystems that support it (such as XFS) it is possible to grow (see xfs_grow) the filesystem to use more of or all remaining data in the block device.

Note: It is technically possible to format a block device at an offset (see the mke2fs command), but I am not aware of a reason to do that.

Can we create two different filesystems on a single partition? Why?

A partition is not synonymous with a block device - the system typically creates standard block devices (/dev/sda1) for partitions, but there are other ways of creating them.

The solution you are probably looking for is LVM - Logical Volume Management. LVM creates an abstraction:

  • The first layer is a PV (Physical Volume) this could be a disk partition or a whole drive.
  • The PVs are merged into VGs (Volume Groups)
  • You can then allocate space from the VG to form a LV (Logical Volume)

The logical volume is exposed as a block device which you can then format and mount.

If you need more space on the LV you can just allocate it from the VG. If there is no more space on the VG you can just add more disks/partitions to the VG.

The critical point is that you can allocate additional space from the VG to an LV even if it is non-continuous / comes from another physical drive the VG hides that fact from the LV - so the block device remains contiguous, now with additional storage - hence the filesystem can then be re-sized to use that additional space.

Summary: If use LVM correctly it removes the need to worry about future proofing your disk partitioning (unless you have a non Linux OS on your system).

Tricks / Hacks

As others have pointed out as long as you can identify a contiguous block of data somewhere on one of your hard drive, that isn't going to be overwritten by another OS. You can use tools like losetup to expose that as a block device - its far from a recommended approach but it can get you out of a bind.

Score:0
cn flag
pts

In this answer I'm attempting answer the following question: is it possible to create and use more than two different filesystems directly on the same partition, starting at offset 0 (beginning of the partition), sharing the file data between the two filesystems?

It's possible to have 3+1 filesystems (ISO 9660 with Rock Ridge extensions, ISO 9660 with Joliet extensions, UDF and Apple HFS) on the same partition, starting at the beginning of the partition (at offset 0), and mount each of these filesystems on Linux (separately, one at a time) for read-only access. This is useful for compatibility with old operating systems (DOS supports ISO 9660 without extensions, old Windows supports Joliet, old Linux and Unix support Rock Ridge, old macOS supports HFS, many systems support UDF). Also UDF can store file larger than 2 GiB (ISO 9660 can't). A more restricted variant is used by DVD-Video.

How is having multiple filesystems on the same partition, starting at offset 0 possible? It works by storing filesystem metadata at different locations (or having non-conflicting values). The most interesting part of the metadata is the filesystem superblock, a starting point which determines where data and other metadata resides. Superblock locations of the filesystems above:

  • ISO 9660: at offset any of 2048 .. 202752 (in 2048-byte increments). The Rock Ridge and Joliet extensions are stored in different parts of the filesystem.
  • UDF: at offset any of 2048 .. 8386560 (in 2048-byte increments). Distinguished from the ISO 9660 superblock by a 5-byte identifier string.
  • HFS: at offset 1024
  • It would be possible to add FAT (FAT12, FAT16 or FAT32), storing its superblock at offset 0.

UDF and HFS supports read-write access (ISO 9660 doesn't), but changing one of those filesystems while mounted likely ruins the other filesystems, because it doesn't update the metadata of the other filesystems correctly. (In fact, it doesn't update the metadata at all, and probably also overwrites some of it.)

Here is how to generate a filesystem image on Linux with the genisoimage tool (part of cdrkit, see also source of cdrkit 1.1.11, released on 2010-10-17, latest version as of 2023-04-27):

genisoimage -R -J -hfs -udf -o bash.iso /bin/bash

To include other files in the image, replace /bin/bash above with names of those files (as program arguments separated by whitespace).

Mount the image file bash.iso as various filesystems on Linux:

mkdir p
sudo mount -o loop,ro,nojoliet -t iso9660 bash.img p  # Use Rock Ridge.
sudo umount p
sudo mount -o loop,ro,norock -t iso9660 bash.img p  # Use Joliet.
sudo umount p
sudo mount -o loop,ro -t udf bash.img p
sudo umount p
sudo mount -o loop,ro -t hfs bash.img p
sudo umount p

Then, optionally copy it to a partition (replace sdX1 with an actual partition device):

sudo dd if=bash.iso of=/dev/sdX1 bs=1M

Debian and Ubuntu bootable install and live CDs also use ISO 9660 as their filesystem. Maybe they also have multiple filesystems. In fact, modern Debian and Ubuntu CDs have only ISO 9660 (no HFS or UDF) as a mountable filesystem, but they also employ some cool tricks at the beginning of the .iso image to make it bootable from a USB stick:

  • When the system is booted from CD or DVD, the system finds the boot code within the ISO 9660 filesysem using the El Torito extensions of ISO 9660. The boot code contains code from the ISOLINUX bootloader, and it will load Linux from the ISO 9660 filesystem.

  • When the system is booted from a USB stick using legacy BIOS boot, the system loads and executes the boot code from the MBR within the first 512 bytes of the filesystem image. The boot code contains code from the ISOLINUX bootloder, and it will load Linux from the ISO 9660 filesystem. Luckily the first 512 bytes of the filesystem image (actually, a few kilobytes) are ignored by ISO 9660, so the MBR can reside there.

  • When the system is booted from a USB stick using UEFI boot, the system finds an .efi file on the EFI partition, and loads and executes it. On this .iso image, there is an MBR containing a single small partition with a FAT12 filesystem containing a file named /efi/boot/bootia32.efi, which contains code from the GRUB bootloader, and it will load Linux from the ISO 9660 filesystem. The FAT12 filesystem is located in a part of the .iso image unused by the ISO 9660 filesystem.

Score:0
cn flag
pts
  1. Also if the size of the filesystem created in a partition is less than the partition size, can we extend the filesystem size in the future?

It depends on the filesystem type. To get the filesystem type of the root filesystem on Linux, run (without the leading $):

$ awk '$2=="/"&&/^\//{print$3}' </proc/mounts
ext4

Then do a Google search to find out which tool to use resize the filesystem of that type. The result can be any of: 1. the tool can resize even without unmounting; 2. unmount it (you may have to reboot from USB stick), run the tool to resize, mount it again; 3. no resize tool exists (copy the data somewhere else, create empty filesystem, copy the data back).

In the example above, the filesystem type is ext4, and the Google search says that the tool is resize2fs, and the manual page of resize2fs says:

It can be used to enlarge or shrink an unmounted file system located on device. If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing.

Data safety advice: to prevent data loss, don't power off the computer while resizing the filesystem; keep the laptop charged before resizing; if there is a high risk of a power outage, postpone the resizing until later.

  1. Can we create two different filesystems on a single partition? Why?

Other answers are also relevant to this, please read them as well. In this answer I'm attempting answer the following question: is it possible to create and use two different filesystems directly on the same partition, starting at offset 0 (beginning of the partition)?

For most practical purposes, the answer is no. The mainstream tools which create filesystems create a single filesystem, and while doing so they may overwrite some blocks on the partition, which may be metadata blocks of the previous filesystem, thus they ruin the previous filesystem. It's also not practical for autodetection: mainstream autodetection tools (e.g. blkid) used by Linux distributions will detect one of the two filesystems. After Linux mounts a filesystem, it is free to modify the entire partition (covered by the filesystem) to accommodate changes (e.g. new files, appends to existing files). Such modifications will eventually ruin the other filesystem.

However, for academic, hacking and demonstration purposes, it's possible to have two filesystems at the beginning of a partition, mounting at most one filesystem at the same time, and specifying the filesystem type when mounting (e.g. mount -t ext2 and mount -t vfat). No file data will be shared between the two filesystems, so each file has to be copied separately, and its data will be duplicated. (See my other answer to avoid this data duplication.) The hard part is creating and populating the filesystems in a way that they don't overlap, because there are no mainstream tools for this. It's possible to write such a tool though, for example if filesystem 1 is FAT16 (or FAT12 or FAT32) and filesystem 2 is ext2 (or minix or some others). That's because FAT16 stores its filesystem headers (superblock) in the first 512 bytes, and ext2 ignores the first 1024 bytes. The FAT16 filesystem has a 2-byte reserved sector count field (at offset 14), thus it's possible to have almost 32 MiB of reserved sectors (assuming that sector size is 512 bytes). That means that the first 32 MiB of the partition (except for the first 512 bytes) is ignored by FAT16. It's possible to fit the entire ext2 filesystem there. With a smarter arrangement of multiple non-overlapping regions it would be possible to have an ext2 filesystem larger than 32 MiB.

Run these commands on Linux to create a combined FAT16 + ext2 filesystem (around 32 MiB for each filesystem) to image file bothfs.img:

BLKDEV=bothfs.img
dd if=/dev/zero bs=1M count=64 of="$BLKDEV"
mkfs.fat -v -s 8 -S 512 -f 1 -F 16 -r 64 -R 65528 "$BLKDEV" 65536
# Copy (save) the FAT16 superblock, mke2fs will overwrite it.
dd if="$BLKDEV" of="$BLKDEV".bs.tmp bs=1K count=1
# See https://unix.stackexchange.com/q/122771 about `mke2fs -E resize=...`
# and `mke2fs -O ^resize_inode`.
mke2fs -t ext2 -b 4096 -m 0 -O ^resize_inode -O ^dir_index -I 128 -i 65536 -F "$BLKDEV" 8191
# Restore the FAT16 superblock.
dd if="$BLKDEV".bs.tmp of="$BLKDEV" bs=1K count=1 conv=notrunc
rm -f "$BLKDEV".bs.tmp
dumpe2fs "$BLKDEV"

See full Linux shell script at https://github.com/pts/mkfs_multi/blob/master/mkfs_ext2_fat16.sh

And here is how to mount it:

mkdir p
sudo mount -o loop -t ext2 bothfs.img p
sudo umount p
sudo mount -o loop -t vfat bothfs.img p
sudo umount p

By using exFAT instead of FAT, it would be possible to make the other filesystem (e.g. ext2) larger than 32 MiB, because the exFAT filesystem stores the reserved size in 8 bytes. However, the mkfs.exfat tool (source code) doesn't have a command-line flag to specify a nonzero reserved size.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.