在红帽linux培训 swich root:/#输入命令后怎么换行后出现swich root

博客访问: 571341
博文数量: 164
博客积分: 7117
博客等级: 少将
技术积分: 1856
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: LINUX
switch_root 命令 除了基于initramfs的系统(如第四节的mini
linux),通常initramfs都是为安装最终的根文件系统做准备工作,它的最后一步需要安装最终的根文件系统,然后切换到新根文件系统上去。以往
的基于ramdisk 的initrd
使用pivot_root命令切换到新的根文件系统,然后卸载ramdisk。但是initramfs是rootfs,而rootfs既不能
pivot_root,也不能umount。为了从initramfs中切换到新根文件系统,需要作如下处理: (1)删除rootfs的全部内容,释放空间 find -xdev / -exec rm '{}' ';' (2)安装新的根文件系统,并切换 cd / mount --move . /; chroot . (3)把stdin/stdout/stderr 附加到新的/dev/console,然后执行新文件系统的init程序 上述步骤比较麻烦,而且要解决一个重要的问题:第一步删除rootfs的所有内容也删除了所有的命令,那么后续如何再使用这些命令完成其他步骤?busybox的解决方案是,提供了switch_root命令,完成全部的处理过程,使用起来非常方便。 switch_root命令的格式是: switch_root [-c /dev/console] NEW_ROOT NEW_INIT [ARGUMENTS_TO_INIT] 其中NEW_ROOT是实际的根文件系统的挂载目录,执行switch_root命令前需要挂载到系统中;NEW_INIT是实际根文件系统的init程序的路径,一般是/sbin/init;-c /dev/console是可选参数,用于重定向实际的根文件系统的设备文件,一般情况我们不会使用;而ARGUMENTS_TO_INIT则是传递给实际的根文件系统的init程序的参数,也是可选的。 需要特别注意的是:switch_root命令必须由PID=1的进程调用,也就是必须由initramfs的init程序直接调用,不能由init派生的其他进程调用,否则会出错,提示: switch_root: not rootfs 也是同样的原因,init脚本调用switch_root命令必须用exec命令调用,否则也会出错,提示: switch_root: not rootfs 二十一、实践:用initramfs安装CLFS根文件系统 现在实践一下switch_root命令,用它切换一个CLFS的根文件系统硬盘分区。我的CLFS安装在/dev/sda8硬盘分区,我们就以此为例说明。 我们还是在以前的image目录中构建 (1)改写init脚本 #!/bin/sh mount -t proc proc /proc mount -t sysfs sysfs /sys mdev -s mount /dev/sda8 /mnt (注意:为了简单,我们直接把CLFS分区写死在init脚本中了) exec switch_root /mnt /sbin/init & (2)生成新的initrd 按上一节描述的cpio命令生成新的initrd。 (3)把新的initrd拷贝到CLFS分区的/boot目录下,改名为clfs-initrd (4)在GRUB的menu.lst配置文件中增加一个启动项 #test for initramfs of CLFS title test for initramfs of CLFS (on /dev/sda8) root (hd0,7) kernel /boot/clfskernel-2.6.17.13 (注意:并没有向内核传递root参数信息) initrd /boot/clfs-initrd 全部做完后,重启机器,选择 test for initramfs of CLFS 启动项,机器顺利进入了CLFS系统,我们构建的initramfs用switch_root命令完成了CLFS实际根文件系统的安装和切换。 ---下节预告---在
这节里我们直接使用了CLFS本身的内核,它已经包含了CLFS根文件系统的硬盘驱动和文件系统驱动,所以可以顺利地安装CLFS的根文件系统。实际
上,initramfs的最重要的功能就是包含大量的硬盘驱动和文件系统驱动,而不需要把所有的驱动程序编译进内核,减少内核大小。initramfs负
责在安装实际根文件系统前根据具体的文件系统设备情况加载合适的驱动到内核中,使系统能够正常安装实际的根文件系统。
阅读(3605) | 评论(0) | 转发(1) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。linux - Trouble getting busybox switch_root to work - Unix & Linux Stack Exchange
to customize your list.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. J it only takes a minute:
Here's how it works:
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
I'm working on an embedded ARM Linux system that boots using initramfs. (Here's some background from a couple
, if you're interested.) So far, thanks in part to help received here, I can boot the kernel via TFTP with an embedded initramfs. The MMC driver detects an SD card containing a new root filesystem, which I can then mount. However, I cannot get the final step, using busybox switch_root to switch to the filesystem on the SD card, to work.
At the initramfs shell prompt, I think this should make the kernel switch to the new filesystem:
switch_root -c /dev/console /mnt/root /sbin/init.sysvinit
However, it just makes busybox (to which switch_root is aliased) print its man page, like this:
/ # switch_root -c /dev/console /mnt/root /sbin/init.sysvinit
BusyBox v1.17.4 ( 17:01:07 EST) multi-call binary.
Usage: switch_root [-c /dev/console] NEW_ROOT NEW_INIT [ARGS]
Free initramfs and switch to another root fs:
chroot to NEW_ROOT, delete all in /, move NEW_ROOT to /,
execute NEW_INIT. PID must be 1. NEW_ROOT must be a mountpoint.
Reopen stdio to DEV after switch
I think the -c option is correct, since it's the same as what is contained in the example, and /dev/console exists.
/ # ls -l /dev
crw-r--r--
1 00:28 console
brw-r--r--
2010 loop0
brw-r--r--
2010 mmcblk0
brw-r--r--
2010 mmcblk0p1
brw-r--r--
2010 mmcblk0p2
brw-r--r--
2010 mmcblk0p3
brw-r--r--
2010 mmcblk0p4
/mnt/root also exists.
/ # ls /mnt/root
lost+found
The init executable exists:
/ # ls -lh /mnt/root/sbin/
lrwxrwxrwx
2010 init -& /sbin/init.sysvinit
-rwxr-xr-x
22.8K Dec 21
2010 init.sysvinit
But here's something strange:
/mnt/root/sbin # pwd
/mnt/root/sbin
/mnt/root/sbin # ls -l | grep init.sysvinit
lrwxrwxrwx
2010 init -& /sbin/init.sysvinit
-rwxr-xr-x
23364 Dec 21
2010 init.sysvinit
/mnt/root/sbin # ./init.sysvinit
/bin/sh: ./init.sysvinit: not found
/mnt/root/sbin # /mnt/root/sbin/init.sysvinit
/bin/sh: /mnt/root/sbin/init.sysvinit: not found
That's totally mystifying. I'm not sure where I'm going wrong. I've looked at the source, which is at
It's not just the init.sysvinit executable. I can't execute anything from the SD card. For example:
/mnt/root/bin # ./busybox
/bin/sh: ./busybox: not found
/mnt/root/bin # /mnt/root/busybox
/bin/sh: /mnt/root/busybox: not found
/mnt/root/bin # ls -l | grep "2010 busybox"
-rwxr-xr-x
462028 Dec 21
2010 busybox
Anyone have a clue what's amiss here? I thought it might be some problem with mounting the card noexec, but I believe exec is the default, and I tried passing the exec option explicitly at mount with no success.
The reason that switch_root is not working on the command line is this code in busybox:
if (st.st_dev == rootdev || getpid() != 1) {
// Show usage, it says new root must be a mountpoint
// and we must be PID 1
bb_show_usage();
You are not PID 1, so you are falling though into this bb_show_usage. The implication is that the switch_root command in your initramfs init script should run switch_root with exec. i.e.
exec switch_root ...
The other issue with your "not found" errors is likely because the shared libraries needed by the executables are not found, because the initramfs root filesystem does not have them. If you can get switch_root to work with exec, then is likely the "not found" error will go away.
17.1k34943
Did you find this question interesting? Try our newsletter
Sign up for our newsletter and get our top new questions delivered to your inbox ().
Subscribed!
Success! Please click the link in the confirmation email to activate your subscription.
switch_root -c /dev/console /mnt/root /mnt/root/sbin/init.sysvinit worked for me.
I had the same problem but switch_root -c /dev/console /mnt/root /sbin/init.sysvinit did not work.
Am sorry None worked for me.
I made a ext2 file system
copied statically built busybox with all its softlinks. And busybox has sticky bit.(-rwsr-sr-x permissions).
I have a /linuxrc and i dont have much in the /etc/ directory.
And i make image from the ext2 filesystem with the following command.
mkimage -C gzip -A ppc -O linux -T ramdisk -a 0x2000000 -n "ramdisk" -d initrd-ext2 initrd.img
I keep the initrd.img as a seperate file in /boot/ and is not part of kernel.
The contents of linuxrc are
(Also tried #!/bin/busybox sh)
mkdir -p /proc /dev /sys /mnt /tmp
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mknod /dev/misc/rtc0 c 254 0
mkdir -p /new_root
mount /dev/mmcblk0p2 /new_root
exec switch_root -c /dev/console /new_root /sbin/init
My kernel gets loaded properly and the initrd.img also gets loaded and the linuxrc gets executed but at the end switch_root gives usage help.
But then the my system continues to boot and loads the new rootfs.
The following is the sequence.
RAMDISK: ext2 filesystem found at block 0
RAMDISK: Loading 10240KiB [1 disk] into ram disk... done.
VFS: Mounted root (ext2 filesystem) on device 1:0.
Starting initramfs boot...
Waiting 5 seconds for devices to settle...
kjournald starting.
Commit interval 5 seconds
EXT3 FS on mmcblk0p2, internal journal
EXT3-fs: mounted filesystem with writeback data mode.
BusyBox v1.21.0.git ( 00:34:21 PDT) multi-call binary.
Usage: switch_root [-c /dev/console] NEW_ROOT NEW_INIT [ARGS]
Free initramfs and switch to another root fs:
chroot to NEW_ROOT, delete all in /, move NEW_ROOT to /,
execute NEW_INIT. PID must be 1. NEW_ROOT must be a mountpoint.
Reopen stdio to DEV after switch
VFS: Mounted root (ext3 filesystem) on device 179:2.
Trying to move old root to /initrd ... /initrd does not exist. Ignored.
Unmounting old root
Trying to free ramdisk memory ... okay
Freeing unused kernel memory: 200k init
INIT: version 2.86 booting
Please wait: booting...
mount: sysfs already mounted or /sys busy
mount: according to mtab, sysfs is already mounted on /sys
Starting udev
udev: starting version 154
Root filesystem already rw, not remounting
Caching udev devnodes
Is my ram freed after exiting the initrd.img in the above sequence?
What if i do not do switch_root at the end of linuxrc and just put
an exit? Wont that free up my ram from my old initrd img.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group. This site is not affiliated with Linus Torvalds or The Open Group in any way.
Unix & Linux Stack Exchange works best with JavaScript enabled博客访问: 35125
博文数量: 69
博客积分: 2720
博客等级: 少校
技术积分: 680
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: LINUX
让我们从启动开始,看看FC6都做了些什么。众所周知,和所有别的发行版本一样,FC6是由grab引导的,grab通常被安装在主引导
扇区,也就是说,如果你在主板的bios中设置了从硬盘启动,那么主板自检以后所执行的第一部分代码就是grub,grub将在其安装时候指定的位置寻找
menu.lst这个文件,并且根据这个文件的配置,加载相应的内核,启动linux。
这里值得我们注意的是,由于grub的这种机制,即使是格式化你觉得已经完全不再使用的硬盘分区,也可能造成灾难性的后果,假设我们把grub安装在了
mbr,并且将配置文件放置在hda2,hda1安装了一套windows操作系统,通过grub实现多重引导,但是现在我们想放弃hda2的linux
系统,或者想把它换成另外一套linux发行版,我们可能会选择格式化hda2,虽然grub被安装在mbr,但是hda2的被格式化仍然会破坏其配置文
件所在的目录,grub将无法正常启动,你也就无法正常引导位于hda1的windows系统了,因为grub会提示错误,不给你选择系统的机会。
这种情况在实际的双系统使用过程中,可能经常会遇到。 遇到这种问题,常见的修复方法是使用软盘启动windows,使用fdisk
/mbr命令使用windows系统提供的mbr覆盖gurb的mbr代码,或者使用其他方式启动linux(软盘,U盘或者光盘),重新安装grub。
虽然天不会塌下来,但是相信也会让你很不爽了,所以要小心。内核是所有linux的核心,grub在成功的读取了配置文件以后,将会找到
kernel所在的位置,加载内核并且把电脑的控制权交给kernel程序,在FC对应的grub的menu.lst文件中,我们通常可以看到类似这样的
语句:root (hd0,2) 这句话告诉我们,从现在开始,根路径将被设置为第一个硬盘的第3个分区,然后是kernel
/boot/vmlinuz-2.6.18-1.2798.fc6 ro root=LABEL=/ rhgb
quiet,这句话告诉我们,从根分区的boot目录的vmlinuz-2.6.18-1.2798.fc6这个文件中读取内核,执行内核的时候使用
"ro root=LABEL=/ rhgb
quiet"这样的参数,内核的执行参数可以控制内核的行为,比如ro参数告诉内核,以只读方式挂载根分区,而quiet则告诉内核,启动的时候不要打印
任何信息。这些参数不光影响内核的执行,大多数的发行版也使用这些参数控制启动完毕以后后续的动作。
这些参数可以在任何时候从/proc/cmdline这个文件中获得。现在,grub找到了内核(hd0,2)
/boot/vmlinuz-2.6.18-1.2798.fc6,它将整个电脑的控制权交给了这个程序,内核开始进行各种初始化的动作,你可以将
quiet参数去掉,以便看看内核都做了哪些事情,也可以在系统启动成功以后,使用dmesg这个命令查看内核启动的时候,都打印了哪些东西,总的来说,
内核做的都是一些和硬件打交道的事情,比如初始化内存,检测并初始化硬件等,在内核启动的最后,它将寻找init程序并将电脑的控制权交给这个程序。有
越来越多的新硬件需要linux的支持,如果把所有的硬件检测工作都放在内核中完成,内核会变得无比巨大,这不光是没有效率的,事实上也是不可能和不允许
的,因此,如果你清楚的知道你的电脑都拥有哪些硬件,并且在未来不会添加新的硬件,你可以只将那些你需要的硬件编译到内核中去,然后直接启动你的
linux系统(事实上,早期的Gentoo系统要求每个安装者在安装的时候编译自己的内核),但是对于FC6这样的发行版来说,为了让全球大多数的PC
都可以顺利使用它,它使用模块的方式编译了尽可能多的硬件支持,并且在启动的时候在grub的配置文件中指定了initrd参数。initrd
参数指定一个小的文件系统,这个文件系统虽然很小,但是比起内核来可以大很多,如果指定了initrd参数,内核在进行完自己的任务之后,将会运行
initrd这个小文件系统中的init程序,由这个程序完成进一步的系统初始化动作,加载更多的硬件支持以便找到真正的根文件系统。在menu.lst
文件中,这是通过initrd
/boot/initrd-2.6.18-1.2798.fc6.img这一行来完成的,扩展名img通常预示着这是一个小的系统镜像文件。使
用file命令,我们可以看到/boot/initrd-2.6.18-1.2798.fc6.img是一个使用gzip压缩过的文件,解压缩以后再使用
file命令,看到这是一个cpio文件,再解压缩这个文件,我们就可以看到initrd文件系统了,这个系统中的文件不多,在根目录中包含一个init
文件,这就是内核初始化完毕以后要运行的文件,这是一个脚本文件,它使用nash解释执行,nash是专门为initrd定制的脚本解释器,它的功能小而
专业,内建了很多initrd很需要的命令,我们在FC6启动的时候看到的"Red Hat nash version xxx starting
"这句话,就是这个时候打印出来的。我们来具体看看FC6的initrd做了哪些事情,首先为了让包含在initrd镜像中的那些程序顺
利执行,它需要完备当前的文件系统,包括挂载proc和sys文件系统(这些是内核支持的系统目录,需要将其挂载到用户区),创建/dev目录,并且在
/dev目录中创建系统初始化所需的那些设备,最典型的设备比如console,有了这个设备,echo命令才能把信息显示到终端上,这个阶段FC6的
initrd中的init程序创建的设备达到数十个之多。然后启动hotplug支持热插拔,这里的hotplug是nash内建支持的命令之一,然后使
用内建的mkblkdevs命令根据/sys/block目录下的文件信息创建/dev目录中对应的设备文件,然后加载usb和ext3相关的模块,在这
个过程中可能又有新的设备被发现,因此需要使用mkblkdevs命令再次更新设备目录,在准备好了/dev设备目录以后,init程序开始调用内建的
mkrootdev命令来创建/dev/root这个设备作为后续操作的根分区,这个命令的大致逻辑是:如果内核命令行中指定了root参数,则使用其指
定的那个参数作为root设备,如果指定的为LABEL,则检查所有的块设备并且寻找卷标为指定值的设备作为root设备,如果没有指定root参数,则
使用/proc/sys/kernel/real-root-dev指定的设备,这个命令除了将创建/dev目录中相应的root设备文件以外,还将更新
fstab文件,将当前找到的root文件的mount参数写入/etc/fstab文件,这样在接下来的命令中,可以直接使用mount命令加载根分
区,成功加载完根分区以后,init使用nash内建的setuproot命令,将所有的sys,proc,dev等这些已经挂载在initrd文件系统
中的目录重新转移至新的根分区,然后使用nash内建的switchroot命令(内核2.6以上的版本)将当前文件系统切换至新的根分区,并且执行新的
根分区的init命令,这样.initrd也完成了自己的使命,剩下的事情就是真实的根分区中的init程序的工作了。
阅读(442) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。

我要回帖

更多关于 红帽linux系统下载 的文章

 

随机推荐