当前位置:网站首页>The ramdisk practice 1: the root file system integrated into the kernel

The ramdisk practice 1: the root file system integrated into the kernel

2022-08-11 07:04:00 Thousand volumes

https://ke.qq.com/course/4032547?flowToken=1042705

前言

ramdiskThe concept of not writing,Many other places.直接上干货,

I wanted to be an independentramdisk,Then put the root filesystem in,As a result, many experiments,没有验证成功,Instead, the root file system integrated into the kernel was measured.,独立的,wait until it's measured,Put it in the next article

先放个Makefilefile out.

all:
    @echo "create ramdisk"
    sudo rm ramdisk.img -f
    sudo dd if=/dev/zero of=ramdisk.img bs=1024 count=65536
    sudo mke2fs -F -m0 ramdisk.img
    sudo mount -t ext2 ramdisk.img /home/lkmao/imx/linux/ramdisk/loop
    sudo cp /home/lkmao/nfsroot/buildrootfs/* /home/lkmao/imx/linux/ramdisk/loop/ -afR
    sudo umount /home/lkmao/imx/linux/ramdisk/loop
ram:
    @echo "make ram"
    dd if=/dev/zero of=ram2 bs=1k count=4096
    sudo mke2fs -F -m0 ramdisk.img
    sudo mount -t ext2 ram2 /home/lkmao/imx/linux/ramdisk/loop
    sudo cp /home/lkmao/imx/linux/ramdisk/rootfs/* /home/lkmao/imx/linux/ramdisk/loop/ -afR
    sudo umount /home/lkmao/imx/linux/ramdisk/loop
    cp ram2 /home/lkmao/tftproot/ram
mount:
    @echo "mount ramdisk"
    sudo mount -t ext2 ramdisk.img /home/lkmao/imx/linux/ramdisk/loop
umount:
    @echo "umount ramdisk"
    sudo umount /home/lkmao/imx/linux/ramdisk/loop
cp:
    @echo "cp ramdisk to ram"
    cp ramdisk.img /home/lkmao/tftproot/ram

.PHONY:clean
clean:
    @echo "del ramdisk.img"
    sudo rm ramdisk.img -f

Let's start with how to integrate the file system into the kernel

一 Configuring filesystem paths in the kernel

 首先,在配置路径General setup下,Find the place shown above,Configure the absolute path of the root file system,My root filesystem is usingbuildrootfs创建的.就别用busybox了,真的很麻烦.

 其次,要强调的是,The file system should not be too large,Otherwise, the compiled kernel will be very large.,The kernel compiled by my kernel will be very large.

二 配置uboot启动参数

ramdiskSee here if you can start it.

setenv bootcmd 'tftp 80800000 zImage;tftp 86000000 emmc.dtb;bootz 80800000 - 86000000;'
setenv bootargs 'console=ttymxc0,115200  root=/dev/ram0 rootfstype=ramfs rdinit=/linuxrc rootwait rw'

The boot parameters of different chips are definitely different. The key here is thatrdinit=/linuxrc,Because I was stuck here early on.

rdinit=/linuxrc
[email protected]:~/tftproot$ ls -lsh zImage
26M -rwxrwxr-x 1 lkmao lkmao 26M 8月  10 15:00 zImage

三 启动测试

How can I prove that my file system is indeed running in memory??

只要愿意想,really found.

第一证明,这个是ubootincoming startup command

/proc/cmdline文件,This can't be fake,算一个

结论:OK

second proof,创建个文件,After restarting verify it's still there

写一个文件,重启后,this file is missing,这个吗,It can only show that this directory is powered off and lost data,The temporary directory also has this effect.

结论:不行

third proof 在根目录执行 rm -rf *

A destination.so it

连基本的lsCommands are gone.

好的,重启 

好吧,reboot也删掉了,重新上电

 启动

 结论:Not to the point

The fourth proof 使用dfCommand to see the mount status of the current partition

reasons that can be proven,There is no record of mounting external devices here

# df -h
Filesystem                Size      Used Available Use% Mounted on
tmpfs                   247.8M         0    247.8M   0% /dev/shm
tmpfs                   247.8M     28.0K    247.7M   0% /tmp
tmpfs                   247.8M     16.0K    247.7M   0% /run

结论:需要对比

fifth proof 想不到了...

四 挂载磁盘

It is impossible for the system not to store data,Or is to mount the disk


# mount -t ext4 /dev/mmcblk1p2 /mmcblk1p2/

The different board,The name of the piece of equipment is not the same,

然后df -h


# df -h
Filesystem                Size      Used Available Use% Mounted on
tmpfs                   247.8M         0    247.8M   0% /dev/shm
tmpfs                   247.8M     28.0K    247.7M   0% /tmp
tmpfs                   247.8M     16.0K    247.7M   0% /run
/dev/mmcblk1p2            6.9G    155.8M      6.4G   2% /mmcblk1p2

从结果可知,挂载成功了.

五  /dev/ram0设备的测试

see it for real


# ls /dev/ram0 -ls
     0 brw-rw----    1 root     root        1,   0 Jan  1 01:14 /dev/ram0

它是一个块设备,So it can be formatted,can be mounted,可以存储数据,从名字就可以知道,it's a ramdisk.

Below is my configuration for it,红框里的就是.

 

格式化

# mk
mkdir     mkdosfs   mke2fs    mkfifo    mknod     mkpasswd  mkswap    mktemp
# mke2fs /dev/ram0
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
16384 inodes, 65536 blocks
3276 blocks (5%) reserved for the super user
First data block=1
Maximum filesystem blocks=262144
8 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345
#

挂载

# mount -t ext2 /dev/ram0 /ram_disk/

如何验证呢,要lost+found If there is this directory, it means that the mount is successful..如下所示,挂载成功

# cd ram_disk/
# ls
lost+found

# echo "hello lkmao" > hello
# ls
hello       lost+found
# cat hello
hello lkmao

执行df -h

# df -h
Filesystem                Size      Used Available Use% Mounted on
tmpfs                   247.8M         0    247.8M   0% /dev/shm
tmpfs                   247.8M     28.0K    247.7M   0% /tmp
tmpfs                   247.8M     16.0K    247.7M   0% /run
/dev/mmcblk1p2            6.9G    155.8M      6.4G   2% /mmcblk1p2
/dev/ram0                62.0M     14.0K     58.8M   0% /ram_disk

这几天dfI use a lot of commands.

卸载

If the current directory is the mount point,umout是不会成功的,So need to exit the directory againumount.

# umount /ram_disk/
umount: can't unmount /ram_disk: Device or resource busy
# pwd
/ram_disk
# cd ..
# umount ram_disk/

再次使用df -h验证

# df -h
Filesystem                Size      Used Available Use% Mounted on
tmpfs                   247.8M         0    247.8M   0% /dev/shm
tmpfs                   247.8M     28.0K    247.7M   0% /tmp
tmpfs                   247.8M     16.0K    247.7M   0% /run
/dev/mmcblk1p2            6.9G    155.8M      6.4G   2% /mmcblk1p2

再挂载

# mount -t ext2 /dev/ram0 /ram_disk/

# cd ram_disk/
# cat hello
hello lkmao

这个说明,/dev/ram0中的数据不会因为umount以后,The data is still there until the reboot,这能说明什么呢,describe this area,在/dev/ram0after being formatted,It's memory area is protected by the system.所以可以放心使用.无论有没有mount,它都在那里,

所以 , /dev/ram0What to do?

结论

        继续写ramdisk实践2.

原网站

版权声明
本文为[Thousand volumes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110516570541.html