- 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.
- 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.