系统相关
首页 > 系统相关> > linux – 任何不起作用的相对路径(从initramfs运行时)

linux – 任何不起作用的相对路径(从initramfs运行时)

作者:互联网

我正在使用嵌入式Linux系统,试图让它使用initramfs在ram中启动它的根文件系统.系统大部分都出现了,但是在init脚本中遇到了麻烦.我把问题缩小到以下几点.

系统无法识别任何相对路径.让我解释一下……

符号链接不仅指向相对位置的文件被破坏,而且只是运行这样的简单命令也不起作用:

$pwd
/etc/network
$cat ../inittab
cat: can't open '../inittab': No such file or directory

但这很好用:

$cat /etc/inittab
<inittab output ...>

知道会发生什么吗?

UPDATE1

标准ls ..命令似乎按预期运行.另外,我认为inode引用看起来不错?

   $ls ..
    default/                inputrc                 moduli                  random-seed             ssh_config              sshd_config
    dhcp/                   issue                   mtab@                   resolv.conf@            ssh_host_dsa_key        ssl/
    fstab                   ld.so.conf              network/                rsyslog.conf            ssh_host_dsa_key.pub    sysconfig/
    fstab.bak               ld.so.conf.d/           nsswitch.conf           rsyslog.d/              ssh_host_ecdsa_key      ts.conf
    group                   logrotate.conf          os-release              screenrc*               ssh_host_ecdsa_key.pub  udev/
    hostname                logrotate.d/            passwd                  securetty               ssh_host_key
    hosts                   ltrace.conf             passwd-                 services                ssh_host_key.pub
    init.d/                 memstat.conf            profile                 shadow                  ssh_host_rsa_key
    inittab                 mke2fs.conf             protocols               shadow-                 ssh_host_rsa_key.pub
    $cd / ; ls -lid /etc
       1547 drwxr-xr-x   12 root     root             0 Jan  1 00:49 /etc/
    $cd /etc ; ls -lid .
       1547 drwxr-xr-x   12 root     root             0 Jan  1 00:49 ./
    $cd /etc/network ; ls -lid ..
       1547 drwxr-xr-x   12 root     root             0 Jan  1 00:49 ../

通过更多的挖掘,我发现相对路径的工作时间很长,因为您没有跨越文件系统根的“边界”:

$cd usr/
$ls ../etc
ls: ../etc: No such file or directory
$cd ../etc
$cd network/
$ls ..
default/                inputrc                 moduli                  random-seed             ssh_config              sshd_config
dhcp/                   issue                   mtab@                   resolv.conf@            ssh_host_dsa_key        ssl/
fstab                   ld.so.conf              network/                rsyslog.conf            ssh_host_dsa_key.pub    sysconfig/
fstab.bak               ld.so.conf.d/           nsswitch.conf           rsyslog.d/              ssh_host_ecdsa_key      ts.conf
group                   logrotate.conf          os-release              screenrc*               ssh_host_ecdsa_key.pub  udev/
hostname                logrotate.d/            passwd                  securetty               ssh_host_key
hosts                   ltrace.conf             passwd-                 services                ssh_host_key.pub
init.d/                 memstat.conf            profile                 shadow                  ssh_host_rsa_key
inittab                 mke2fs.conf             protocols               shadow-                 ssh_host_rsa_key.pub
$ls ../../usr
ls: ../../usr: No such file or directory

这让我相信我没有正确安装根文件系统.也许这个输出是最有说服力的?

$df
Filesystem                Size      Used Available Use% Mounted on
devtmpfs                204.2M         0    204.2M   0% /dev
tmpfs                   251.7M         0    251.7M   0% /dev/shm
tmpfs                   251.7M     76.0K    251.6M   0% /tmp

UPDATE2

经过额外的搜索,我相信the following最能描述我的场景:

2) The newer initial ramfs image, initramfs. Here one populates a
directory, and then creates a compressed cpio archive which is
expanded into ramfs upon boot and becomes the root filesystem. The
kernel must be configured with CONFIG_BLK_DEV_INITRD=y but one does
not need to set CONFIG_BLK_DEV_RAM_SIZE, nor does one need to set
CONFIG_TMPFS=y. When the system is up, “df” does not report the root
filesystem and one cannot interact with it by doing things like “mount
–bind / dir”. Also the distinction between what RAM is set aside for the filesystem and what RAM is used for processes is blurred. “df”
reports nothing and “free” reports total usage without distinction,
ie. used RAM = RAM used for files (as reported by “du”) plus RAM used
for processes.

但是,我对此感到有些惊讶.这是否暗示我在使用initramfs时无法在文件系统的根目录之间进行交互?

UPDATE3

This post表明我想要完成的事情并非不合理:

Now normally an initramfs is temporary, only used to run some programs
extremely early in the boot process. After those programs run, control
is turned over to the real filesystem running on a physical disk.
However you do not have to do that. There is nothing stopping you from
running out of the initramfs indefinitely

如何无限期地用完initramfs但又能够遍历文件系统的根目录?

解决方法:

阅读this post后,问题解决了!

我注意到在运行mount命令时出现了两个条目/:

rootfs on / type rootfs (rw,relatime)
devtmpfs on /dev type devtmpfs (rw,relatime,size=209064k,nr_inodes=52266,mode=755)
proc on /proc type proc (rw,relatime)
devpts on /dev/pts type devpts (rw,relatime,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,relatime,mode=777)
tmpfs on /tmp type tmpfs (rw,relatime)
sysfs on /sys type sysfs (rw,relatime)
none on / type tmpfs (rw,relatime)

我添加了一个我需要删除的fstab条目:

# /etc/fstab: static file system information.
#
# <file system> <mount pt>     <type>   <options>         <dump> <pass>
none            /              tmpfs    defaults          0      0
proc            /proc          proc     defaults          0      0
devpts          /dev/pts       devpts   defaults,gid=5,mode=620   0      0
tmpfs           /dev/shm       tmpfs    mode=0777         0      0
tmpfs           /tmp           tmpfs    defaults          0      0
sysfs           /sys           sysfs    defaults          0      0

删除条目(并执行重新启动)后:

none            /              tmpfs    defaults          0      0

问题消失了!

标签:linux,embedded,path,busybox,initramfs
来源: https://codeday.me/bug/20190815/1662179.html