创建squashfs
SquashFS 通常的livecd都有一个这个文件,是核心的文件系统
SquashFS 也是一个只读的文件系统,它可以将整个文件系统压缩在一起,存放在某个设备,某个分区或者普通的文件中。如果您将其压缩到一个设备中,那么您可以将其直接 mount 起来使用,而如果它仅仅是个文件的话,您可以将其当为一个 loopback 设备使用。
下面是linux启动的简单流程:
嵌入式 Linux 启动过程
本文所描述的的 Linux Image 由 BootLoader、kernel、initrd、rootfs 组成,它们共同存在于一个可以启动的存储设备中(本文以 USB 为例)。组成架构如下:
图 1. 可启动 linux 镜像文件结构
各个模块的作用如下:
- 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 |
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 |
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 |
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 |
If everything went fine, unmount /dev/hda7 (if needed) and use dd to copy /var/arch.sqsh to /dev/hda6:
bash# umount /dev/hda7 |
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 |
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:
When developing a kernel for your system, make sure you enable SquashFS support so it can mount squashed file systems
Use mksquashfs for creating read-only initial ram disks and/or root and/or other file systems
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 |
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 |
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.
Enable SquashFS in the linux kernel of the target system
Create a squashed root file system
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 |
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 |
As you can see, now you can create new files in /home/user1.
bash# cd /home/user1 |
umount the unionfs and the squashfs file systems and list the content of /home/user1 and /home/rw.fs dir
bash# cd .. |
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 |
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.
... |
参考:
https://www.ibm.com/developerworks/cn/linux/1306_qinzl_squashfs/index.html
http://www.tldp.org/HOWTO/SquashFS-HOWTO/creatingandusing.html
创建squashfs的更多相关文章
- 【Shell】总结·linux shell脚本攻略
第一章:小试牛刀 #变量赋值 var = value不同于var=value 把var=value写成var = value是一个常见的错误 前者是赋值操作,后者是相等操作 #let命令可以直接执行基 ...
- Linux内核文档翻译之Squashfs文件系统
转载:http://blog.csdn.net/gqb_driver/article/details/12946629 对于使用openwrt的嵌入式系统来说,因为硬件绝大多数采用Flash,因此一般 ...
- 基于 SquashFS 构建 Linux 可读写文件系统
转载:http://www.oschina.net/question/129540_116839 在当前的嵌入式操作系统开发中,Linux 操作系统通常被压缩成 Image 后存放在 Flash 设备 ...
- How to modify squashfs image
/********************************************************************** * How to modify squashfs ima ...
- squashfs文件系统
一.Squashfs文件系统简介 squashfs是以linux 内核源码补丁的形式发布,附带mksquashfs工具,用于创建squash文件系统.squashfs可以将整个文件系统或者某个单一的目 ...
- LVM卷的创建及案例演示
LVM:Logical Volume Manager, Version:2dm: device mapper,将一个或多个底层块设备组织成一个逻辑设备的模块. /dev/dm-# 这里的#表示数字,代 ...
- In-Memory:在内存中创建临时表和表变量
在Disk-Base数据库中,由于临时表和表变量的数据存储在tempdb中,如果系统频繁地创建和更新临时表和表变量,大量的IO操作集中在tempdb中,tempdb很可能成为系统性能的瓶颈.在SQL ...
- 创建 OVS flat network - 每天5分钟玩转 OpenStack(134)
上一节完成了 flat 的配置工作,今天创建 OVS flat network.Admin -> Networks,点击 "Create Network" 按钮. 显示创建页 ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库
在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...
随机推荐
- 【BZOJ3442】学习小组 费用流
[BZOJ3442]学习小组 Description [背景] 坑校准备鼓励学生参加学习小组. [描述] 共有n个学生,m个学习小组,每个学生有一定的喜好,只愿意参加其中的一些学习小组,但是校领导为学 ...
- Objective-C入门教材
2011-05-11 15:58 三聪 cnblogs 字号:T | T 阅读本文前,你也要了解面向对象的基本概念.对象的使用以及面象对象设计模式都是bjective-C进行面向对象编程和设计Coco ...
- 参数估计(1):从最小二乘到最小b乘
机器学习到底学习到了什么,或者说“训练”步骤到底在做些什么?在我看来答案无非是:所谓的“学习”就是把大量的数据归纳到少数的参数中,“训练”正是估计这些参数的过程.所以,除了“参数估计”, 我想不到还有 ...
- 170330、Spring中你不知道的注入方式
前言 在Spring配置文件中使用XML文件进行配置,实际上是让Spring执行了相应的代码,例如: 使用<bean>元素,实际上是让Spring执行无参或有参构造器 使用<prop ...
- Linux C 获取 文件的大小
通过Linux C库函数来获取文件的大小 #include <unistd.h> #include <sys/types.h> #include <sys/stat.h& ...
- 报错分析---->jsp自定义标签:Unable to load tag handler class
Unable to load tag handler class 无法加载标签处理程序类 处理自定义标签的类中如下: 调用自定义标签的jsp中如下:
- window下cmd的宽度调整
一直被cmd的宽度这么了好些年,刚才搜索了下,还真可以设置.标题栏右键属性,调整屏幕缓冲区宽度,只要足够长就不会换行了,然后调整窗口大小-宽度,不能超过屏幕缓冲区宽度,当小于屏幕缓冲区的时候,就会显示 ...
- rabbitMQ基本概念
一.网页登录方法 http://127.0.0.1:15672/ 用户名和密码默认为guest/guest 用java代码去连接rabbitmq用的端口是5672 二.rabbitMQ基本概念 Rab ...
- 教你编译PHP7 (nginx+mysql+php7)
# 安装编译工具: yum install gcc automake autoconf libtool gcc-c++ # 安装基础库 yum install gd zlib zlib-devel o ...
- 宏表达式与函数、#undef、条件编译、
宏表达式在预编译期被处理,编译器不知道宏表达式的存在. 宏表达式没有任何的调用开销 宏表达式中不能出现递归定义. C语言中强大的内置宏 __FILE__:被编译的文件名 //双底线 __LINE__: ...