創建squashfs


SquashFS 通常的livecd都有一個這個文件,是核心的文件系統

SquashFS 也是一個只讀的文件系統,它可以將整個文件系統壓縮在一起,存放在某個設備,某個分區或者普通的文件中。如果您將其壓縮到一個設備中,那么您可以將其直接 mount 起來使用,而如果它僅僅是個文件的話,您可以將其當為一個 loopback 設備使用。

下面是linux啟動的簡單流程:

嵌入式 Linux 啟動過程

本文所描述的的 Linux Image 由 BootLoader、kernel、initrd、rootfs 組成,它們共同存在於一個可以啟動的存儲設備中(本文以 USB 為例)。組成架構如下:

圖 1. 可啟動 linux 鏡像文件結構

aaa

各個模塊的作用如下:

  • Boot Loader:由 BIOS 加載,用於將后續的 Kernel 和 initrd 的裝載到內存中
  • kernel:為 initrd 運行提供基礎的運行環境
  • initrd:檢測並加載各種驅動程序
  • rootfs:根文件系統,用戶的各種操作都是基於這個被最后加載的文件系統

其調用順序是 Boot Loader->kernel->initrd->rootfs。

當機器上電時首先 BIOS 會啟動,然后裝載 USB 設備中的 Boot Loader、kernel,、nitrd 到內存中,由於這些文件大小總和小於 10M,所以我們直接拷貝到內存中再執行不會有問題。

最后要加載的 rootfs 是用戶最終進行讀寫操作的文件系統。

  • 在非嵌入式系統中,這部分文件通常儲存在可直接讀寫的硬盤上,因此直接掛載到根目錄后(例如:mount /dev/sda1 /mnt)就可以進行讀寫操作。
  • 在嵌入式系統中,它是一個壓縮的文件系統,大小通常是好幾百兆,解壓后的大小都超過 1G,如果直接 mount 到系統目錄,那么系統目錄是只讀的,不可進行寫入操作。而如果把它加壓到內存中可以實現讀寫的操作,但是這么大的文件直接解壓到內存中對於嵌入式設備來說是不可接受的。因此我們需要找到一種不拷貝 rootfs 到內存中,同時又可以對最終的根文件系統進行讀寫的方法。

 

下面是如何制作一個這樣的文件系統

 

Creating and using squashed file systems

4.1. Basic steps

In order to create a squashed file system out of a single directory (say, /some/dir), and output it to a regular file (thus, producing a file system image), you need to say only one magic phrase:

	bash# mksquashfs /some/dir dir.sqsh

mksquashfs will perform the squashing and print the resulting number of inodes and size of data written, as well as the average compression ratio. Now you have your /some/dir directory image in the dir.sqsh file. You can now use the mount command to mount it using a loopback device:

	bash# mkdir /mnt/dir
	bash# mount dir.sqsh /mnt/dir -t squashfs -o loop

To check if you have what's expected:

	bash# ls /mnt/dir

If you want to output the file system directly into a device (say, your floppy at /dev/fd0):

	bash# mksquashfs /some/dir /dev/fd0

Then just mount the device:

	bash# mount /dev/fd0 /mnt/floppy -t squashfs

And check if it's okay:

	bash# ls /mnt/floppy

4.2. Squashing file systems

Operations described here correspond to most cases where a read-only compressed file system can be used, whether you want it to be on a block device or in a file. This could be anything from large FTP/HTTP-served archives that don't change often, to having a squashed /usr partition and anything alike with these.

4.2.1. Example 1

Let's suppose you have a /var/arch directory with lots of files and that you want to turn it into a squashed file system and keep it on your root partition as a file (it will be a file system image that you will mount via a loopback device). The operations needed to perform are as follows.

Squash the directory, then mount it via loopback to test it:

	bash# mksquashfs /var/arch /var/arch.sqsh
	bash# mkdir /mnt/tmp
	bash# mount /var/arch.sqsh /mnt/tmp -t squashfs -o loop
	bash# ls /mnt/tmp

If everything is as expected, make this file system mount automatically at boot time by adding this line to your /etc/fstab:

	/var/arch.sqsh	/var/arch	squashfs	ro,defaults	0 0

Unmount the file system from the temporary mount point, and mount using it's fstab entry:

	bash# umount /mnt/tmp
	bash# mount /var/arch

Now just ensure that everything works fine:

	bash# ls /var/arch

4.2.2. Example 2

Say you have two hard disk partitions, /dev/hda6 (which is empty) and /dev/hda7 (which is bigger than /dev/hda6, mounted at /var/arch, contains some data and is full). Now, say you want to squash the /dev/hda7 file system and move it to /dev/hda6, then use /dev/hda7 for some other purposes. We will suppose you have the following line in /etc/fstab (reiserfs is just an example file system used on /dev/hda7):

	/dev/hda7	/var/arch	reiserfs	defaults	0 0

In the same fashion as with the previous example:

	bash# mksquashfs /var/arch /var/arch.sqsh
	bash# mkdir /mnt/tmp
	bash# mount /var/arch.sqsh /mnt/tmp -t squashfs -o loop
	bash# ls /mnt/tmp

If everything went fine, unmount /dev/hda7 (if needed) and use dd to copy /var/arch.sqsh to /dev/hda6:

	bash# umount /dev/hda7
	bash# dd if=/var/arch.sqsh of=/dev/hda6

Now change the line in /etc/fstab for /dev/hda7 to:

	/dev/hda6	/var/arch	squashfs	ro,defaults	0 0

Mount the new file system and check to see if all went fine:

	bash# mount /var/arch
	bash# ls /var/arch

Don't forget to erase the unneeded file system image:

	bash# rm /var/arch.sqsh

4.3. Creating tiny/embedded systems

By saying "tiny/embedded", I mean Linux systems that are being built for booting from floppy disks, IDE/USB flash disks, iso9660 CD-ROMs, small-sized hard drives and the like. Whether you want to have your whole root file system on a single media (a single partition, a single floppy), or have a modular system (several floppies or disk partitions), the procedure is almost identical. Creating such Linux systems themselves is out of scope of this HOWTO - there are dedicated HOWTOs and guides for this (like the Bootdisk HOWTO and Linux From Scratch - visit www.tldp.org to retrieve these documents).

4.3.1. Squashed file systems on floppy/flash/hard disks

In order to use SquashFS for creating Linux systems on small disks, you just have to follow the usual steps for creating a minimal system, performing the following operations at respective points:

  1. When developing a kernel for your system, make sure you enable SquashFS support so it can mount squashed file systems

  2. Use mksquashfs for creating read-only initial ram disks and/or root and/or other file systems

  3. Don't forget to set file system types to squashfs in /etc/fstab and/or the startup scripts of your system for mounting squashed file systems

Floppy example. Let's say you have your floppy system tree at /home/user/floppylinux and you want to place the root file system on one floppy and /usr on another. What you should do is:

	bash# cd /home/user
	bash# mksquashfs floppylinux root.sqsh -e usr
	bash# mksquashfs floppylinux/usr usr.sqsh

Note 1: you can see here how we use the -e option to exclude the /usr directory for root file system's image.

Note 2: don't forget to specify squashfs in your root disk's /etc/fstab or startup scripts when mounting the /usr file system.

Insert a root disk in your 3.5" floppy drive (I assume you have a lilo or grub on it, and, thus, a file system exists on this floppy, and the root file system will reside under the /boot directory of this file system):

	bash# mount /mnt/floppy
	bash# cp root.sqsh /mnt/floppy/boot

When done, unmount the root floppy, change the floppy to a /usr disk and use dd to transfer the usr file system:

	bash# dd if=usr.sqsh of=/dev/fd0

4.3.2. Squashed file systems on CD-ROMs

With SquashFS, you can compress large file systems that will be used in live CDs (just as an example). For this purpose SquashFS is also used with UnionFS.

  1. Enable SquashFS in the linux kernel of the target system

  2. Create a squashed root file system

  3. Modify the /etc/fstab or startup scripts of the target system to mount the squashd file system when you need it

If you create a root file system out of a running Linux system, use the -e option for mksquashfs to exclude all pseudo-filesystems such as /proc, /sys (on linux kernels after 2.5.x) and /dev (when using DevFS). Also, don't forget to add the file system image itself that is being created with mksquashfs (I think you know the reasons for these exclusions).

4.4. Making it writeble

As mentioned, another interesting use for SquashFS is with Unionfs filesystem, which provides copy-on-write semantics for the read-only file systems, enahancing the possibilities. (For unionfs you can look at http://www.filesystems.org/project-unionfs.html)

Just to make an example, you may want to make your /home/user squashed, to compress and backup your files without losing the possibility to apply changes or writing new files.

Create the ro.fs squashed file system and the rw.fs dir.

bash# mksquashfs /home/user1 ro.fs
bash# mkdir /home/rw.fs

Mount the squashed ro.fs file system using the loopback device

bash# mount -t squashfs ro.fs /mnt -o loop

mount the unionfs file system, that makes /mnt and /home/rw.fs apparently merged under /home/user1 location.

bash# cd /home
bash# mount -t unionfs -o dirs=rw.fs=rw:/mnt=ro unionfs user1

As you can see, now you can create new files in /home/user1.

bash# cd /home/user1
bash# touch file1
bash# ls

umount the unionfs and the squashfs file systems and list the content of /home/user1 and /home/rw.fs dir

bash# cd ..
bash# umount /home/user1
bash# umount /mnt

bash# ls /home/user1
bash# ls /home/rw.fs 

You can see that the new file1 was created in /home/rw.fs

When you want to add the new created files to the stable and compressed squashed file system, you have to add them to the exsisting one.

bash# mksquashfs /home/rw.fs /home/ro.fs

Now, to mount your squashed user home directory at system startup, you can do as follow:

Make squashfs and unionfs modules loaded at boot time.

bash# echo squashfs >> /etc/modules
bash# echo unionfs >> /etc/modules

Change the owner of the writeble branch to match user1.

chown user1 /home/rw.fs

Add these lines to /etc/fstab file to mount squashfs and unionfs at boot time.

...
/home/ro.fs  /mnt squashfs loop 0 0
unionfs /home/user1 unionfs dirs=/home/rw.fs=rw:/mnt=ro 0 0

參考:

https://www.ibm.com/developerworks/cn/linux/1306_qinzl_squashfs/index.html

http://www.tldp.org/HOWTO/SquashFS-HOWTO/creatingandusing.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM