根文件系统的制作 + mount
写在前面
——为什么要做这些?
——1.QEMU需要挂载根目录,用busybox+脚本还是比较方便制作rootfs的。还要学着理解啥是rootfs
2. 就是为了测不同类型不同版本的文件系统才用QEMU跑kernel的!所以之前的工作全是铺垫,今天做的也是铺垫。
busybox + 脚本 制作简单的rootfs
- busybox 的编译安装
busybox的源码安装过程写在这里了
- busybox制作fs脚本
可以直接上脚本
这里 版本号和路径以实际为准
#!/bin/sh
busybox_folder="./busybox-1.34.0"
rootfs="rootfs"
echo $base_path
if [ ! -d $rootfs ]; then #判断文件是否存在
mkdir $rootfs
fi
cp $busybox_folder/_install/* $rootfs/ -rf
cd $rootfs
if [ ! -d proc ] && [ ! -d sys ] && [ ! -d dev ] && [ ! -d etc/init.d ]; then
mkdir proc sys dev etc etc/init.d
fi
if [ -f etc/init.d/rcS ]; then
rm etc/init.d/rcS
fi
echo "#!/bin/sh" > etc/init.d/rcS
echo "mount -t proc none /proc" >> etc/init.d/rcS
echo "mount -t sysfs none /sys" >> etc/init.d/rcS
echo "/sbin/mdev -s" >> etc/init.d/rcS
chmod +x etc/init.d/rcS
if [ -f ../fsimg/rootfs.img ]; then
rm ../fsimg/rootfs.img
.sh执行完毕,生成了rootfs和fsimg/rootfs.img:
dafei@ubuntu:~/qemu$ tree -L 1 rootfs
rootfs
├── bin
├── dev
├── etc
├── linuxrc -> bin/busybox
├── proc
├── sbin
├── sys
└── usr
- 起QEMU
qemu-system-x86_64 -kernel path/to/bzImage -initrd path/to/fsimg
跑不起来,得用append添加内核命令
qemu-system-x86_64 -kernel path/to/bzImage -initrd path/to/fsimg -append "root=/dev/ram rdinit=sbin/init”
- 顺利跑起来了,不过
busybox支持的指令也太少了吧,就缺我想要的mkfs
busybox是可扩展的,但需要相关命令源代码以及一系列操作…
busybox命令扩展
busybox简介
创建分区、并mkfs
首先声明这一部分和上一部分是无关联的,是我从头再来的一次,呜呜
因为busybox不支持mkfs且扩展命令有难度,另一种思路是放弃busybox,起qemu时用自制的特定类型的文件系统
后记:但经过仔细考虑以后,因为目前在我的理解上,mkfs制作的文件系统版本是由kernel确定的,我“在qemu之前mkfs不同版本的ext4” 这个想法恐怕还是行不通的。 有没有人可以解答我这方面的疑惑,哭泣!
在我的Ubuntu上先mkfs试试吧
在VMware中新加一个1G的硬盘(第一次试试,捡了个1g),识别为/dev/sdb
fdisk /dev/sdb
dafei@ubuntu:~$ sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x12e5bdef.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
创建了两个小分区
sdb 8:16 0 1G 0 disk
├─sdb1 8:17 0 19M 0 part
└─sdb2 8:18 0 20M 0 part
snopshot 和 clone 的区别:
快照是创建可读
克隆是创建可写快照
mount
sudo mount /dev/sdb1 /myextfs
//查看结果,df --help学习详细命令
dafei@ubuntu:/$ df -TH /myextfs/
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 ext4 19M 324k 17M 2% /extfs
//挂载点持久化
sudo vim /etc/fstab
//添加一行(各个字段可以用空格或tab键隔开)
/dev/sdb1 /myextfs ext4 defaults 0 0
直接用sdb1号不能确保安全,最好用UUID(Universally Unique Identifier)取代
用法: UUID=*** /myextfs ext4 default 0 0
查看UUID的命令:
vol_id /dev/sdb5
最终可见
dafei@ubuntu:/$ ls /extfs
lost+found