特殊:Badtitle/NS100:BootFromUSB/zh:修订间差异

来自Ubuntu中文
跳到导航跳到搜索
Kaixinlaohe留言 | 贡献
Dewafer留言 | 贡献
无编辑摘要
第60行: 第60行:
如果你在grub的帮助下找不到驱动器,那就只能使用linux内核了,下个部分会有解释。
如果你在grub的帮助下找不到驱动器,那就只能使用linux内核了,下个部分会有解释。


== Using a Linux Kernel ==
== Using a Linux Kernel 使用Linux内核==


你可能使用常规的CD光盘或DVD来安装Ubuntu, since you will be booting from discs which contain a Linux kernel and all of the driver modules needed to acces your USB drive. The only difference you may notice is that your drive will be called "/dev/sda" instead of the regular "/dev/hda" which you may find in many guides.
你可能使用常规的CD光盘或DVD来安装Ubuntu, since you will be booting from discs which contain a Linux kernel and all of the driver modules needed to acces your USB drive. The only difference you may notice is that your drive will be called "/dev/sda" instead of the regular "/dev/hda" which you may find in many guides.
第67行: 第67行:


To build your own boot CD you will need to mount your system from within the Live CD (this is describe here [[UbuntuHelp:Mount]]). Once that is done it is time to add the needed modules to your initrd. To do this you must run:
To build your own boot CD you will need to mount your system from within the Live CD (this is describe here [[UbuntuHelp:Mount]]). Once that is done it is time to add the needed modules to your initrd. To do this you must run:
为了建立您自己的boot CD,您将需要从Live CD内装载您的系统(请参考 [[UbuntuHelp:Mount]])。一旦装载完成,就该把需要的模块加入您的initrd中去了。您可以运行:
<pre><nowiki>
<pre><nowiki>
gksudo gedit /wherever_you_have_mounted_your_system/etc/mkinitramfs/modules
gksudo gedit /wherever_you_have_mounted_your_system/etc/mkinitramfs/modules
</nowiki></pre>
</nowiki></pre>
Which will open the text editor with that file. You need to add these lines to the end of the file:
Which will open the text editor with that file. You need to add these lines to the end of the file:
该命令会使用一个文本编辑器打开该文件,您需要在该文件末尾加入下列几行:
<pre><nowiki>
<pre><nowiki>
usbcore
usbcore
第81行: 第83行:
</nowiki></pre>
</nowiki></pre>
(It is always a good idea to put comments in any manually changed configurations so you don't get confused later) Now save the file and exit gedit.
(It is always a good idea to put comments in any manually changed configurations so you don't get confused later) Now save the file and exit gedit.
(在手动修改的配置文件中加入注释是一个很好的习惯,这样您就不会在以后搞糊涂了)现在可以保存文件并退出文本编辑器了。


There is one small problem before we carry on. USB drives take a few seconds before they are set up properly by Linux, and your boot would fail before your drive becomes accessible. To sort this we need to tell the initrd to wait a few seconds before it gets carried away and fails. You can do this by running:
There is one small problem before we carry on. USB drives take a few seconds before they are set up properly by Linux, and your boot would fail before your drive becomes accessible. To sort this we need to tell the initrd to wait a few seconds before it gets carried away and fails. You can do this by running:

2007年6月10日 (日) 14:39的版本

{{#ifexist: :BootFromUSB/zh/zh | | {{#ifexist: BootFromUSB/zh/zh | | {{#ifeq: {{#titleparts:BootFromUSB/zh|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:BootFromUSB/zh|1|-1|}} | zh | | }}


本文解释如何从一个不支持USB启动的计算机上启动已安装在USB设备上的Ubuntu系统.


介绍

如果你把Ubuntu安装在USB设备上,如记忆棒,移动硬盘,通常是不可能在老机器上启动这个系统的.本指南是解释如何制作一个可以加载USB系统的启动CD.

现在的有些电脑没有从USB启动的BIOS选项(或菜单项),但它们往往都可以检测到USB设备,这种情况下可以用grub 来启动设备.

如何工作

个人计算机使用一种叫 BIOS(基本输入/输出系统) 的系统来启动机器.过去25年来,虽然很多BIOS已经增加了少数特性,但BIOS系统的作用仍然十分有限,因为只是"启动"过程(即所谓由于计算机已经"pull itself up by its boot straps", 这是一个壮举,现实生活中不可能发生,但是它却恰如其分地描述了由电源开关控制的一个简单的电流可以启动一系列的连锁行为甚至更加复杂的电子行为,直到计算机已经启动和运行.这就是BIOS的关键,还不算太复杂)的一部份. 目前有很多BIOS系统的替代品,例如Amiga Kickstart, 它使用高达50M的ROM芯片来存储操作系统的大部分东西(但是这使在Amiga上启动Linux变得更加困难,因此我们不使用在Kickstart芯片上的AmigaOS),以及最近的EFI系统使用的是基于Intel的苹果操作系统,但是现在的个人计算机仍然停留在BIOS.本指南给那些计算机的"额外特征"没有USB支持的扩展功能的用户.然而,我们可以从这些在计算机启动系统中固有的限制里得到什么呢?答案是不能.相反我们还要在启动过程增加其他的层通过增加BIOS能访问的软件,接着就可访问USB设备.幸运的是,已经有一款优秀的软件可以读取USB设备,它就是Linux.只要在设备里写入BIOS能识别的Linux内核,我们就可以让Linux内核接管我们想要做的事.另外,一些情况下,grub启动引导器可以不用Linux内核而直接工作.

精简了的Linux内核可以放进一张高密度 (1.44MB) 软盘,但标准的Ubuntu内核则不大可能。而且,标准的Ubuntu内核并没有从USB设备启动所需要的所有驱动程序,因此这些额外的驱动模块必须被放进 "initial RAM disk" 映像 (缩写为 "initrd"),这一映像在启动过程中被复制到RAM ,以使内核能够读取它所包含的那些额外模块。 多数PC都能从CDROM启动,可写的CD能够存储比足够数据更多的内容,这使得Linux和一个initrd可以很容易地被写入其中(这也就是“live CD"的原理),因此我们就将使用这种方法来启动。既然内核和initrd在启动时被复制到了RAM并在那里运行(一旦内核可以读取主要的驱动器,那么以RAM为基础的initrd就不再需要了),启动完毕后就没有必要把CD继续留在光驱里了,所以如果你只有一个CDROM驱动器,那么除了使用live CD时必须用到它,光驱并非一刻也离不开的。

那么我们现在已经知道Ubuntu理论上如何才能从CD启动到USB驱动器,下一步应该将其付诸实践了。

通过grub启动

这是最简单的办法了,从2002-2005就已经被证明适合于数种笔记本电脑。要想检查你的BIOS是否能检测USB驱动器并交付给grub,只需运行grub(如果已经安装,那就在硬盘上运行,也可以从grub启动软件或CD运行)。在grub菜单中,选'c'即可进入命令行模式。现在搜索USB驱动器,用root命令选择一个驱动器或分区,用find命令查看是否找到了正确的那一个。可以像下面这个例子一样浏览各个设备:

grub> root (hd0,0)   # first harddrive, first partition
grub> find /[tab]    # type the slash then press [tab], and it will try to list files on this partition
Error 17: Cannot mount selected partition   # Oops no file system here
grub> root (hd0,1)   # first harddrive, second partition
grub> find /[tab]
 Possible files are: lost+found var etc media ...   # That was my hard drive with my linux install
grub> root (hd1,0)   # second hard drive usually is the USB drive if you have only one internal drive
grub> find /[tab]
 Possible files are: ldlinux.sys mydoc myfile mystick syslinux.cfg  # Bingo, that's the USB stick

(如果你有两个内部驱动器,USB驱动器可能是hd2,0等。)

或者如果你知道在驱动器上存在某个文件,比如叫作"MYDRIVE",那么只要运行

grub> find /MYDRIVE

这样grub就可以搜索它能访问的所有驱动器和分区。它会列出root这一命令使用的正确设备。

启动这一驱动器,输入如下内容:

chainloader +1
boot

为方便起见,可把这些命令加到你的grub配置中去(通常在/boot/grub/menu.lst):

title    Boot USB drive
root     (hd1,0)
chainloader +1
boot

当然如果你的硬盘上没有安装grub,那就把你的grub软盘或CD上的menu.lst改一下。

如果你在grub的帮助下找不到驱动器,那就只能使用linux内核了,下个部分会有解释。

Using a Linux Kernel 使用Linux内核

你可能使用常规的CD光盘或DVD来安装Ubuntu, since you will be booting from discs which contain a Linux kernel and all of the driver modules needed to acces your USB drive. The only difference you may notice is that your drive will be called "/dev/sda" instead of the regular "/dev/hda" which you may find in many guides.

Once the installation has finished using the disc you must reboot into your new USB system. This is where you need a bootable CD. It is easiest to make such a CD from another Ubuntu system, slightly harder from a different Linux system, and quite hard on a Windows system. It is usually easiest to boot from an Ubuntu Live CD.

To build your own boot CD you will need to mount your system from within the Live CD (this is describe here UbuntuHelp:Mount). Once that is done it is time to add the needed modules to your initrd. To do this you must run: 为了建立您自己的boot CD,您将需要从Live CD内装载您的系统(请参考 UbuntuHelp:Mount)。一旦装载完成,就该把需要的模块加入您的initrd中去了。您可以运行:

gksudo gedit /wherever_you_have_mounted_your_system/etc/mkinitramfs/modules

Which will open the text editor with that file. You need to add these lines to the end of the file: 该命令会使用一个文本编辑器打开该文件,您需要在该文件末尾加入下列几行:

usbcore
sd_mod
ehci_hcd
uhci_hcd
ohci_hcd
usb-storage
scsi_mod

(It is always a good idea to put comments in any manually changed configurations so you don't get confused later) Now save the file and exit gedit. (在手动修改的配置文件中加入注释是一个很好的习惯,这样您就不会在以后搞糊涂了)现在可以保存文件并退出文本编辑器了。

There is one small problem before we carry on. USB drives take a few seconds before they are set up properly by Linux, and your boot would fail before your drive becomes accessible. To sort this we need to tell the initrd to wait a few seconds before it gets carried away and fails. You can do this by running:

gksudo gedit /wherever_you_have_mounted_your_system/etc/mkinitramfs/initramfs.conf

Now to this you should add at the very top the lines:

WAIT=15

Then save the file and exit the text editor.

Now that we have corrected the initrd's setup we must use this setup to rebuild the initrd using our new guidelines. To do this you must enter your system by running:

sudo mount --bind /dev /wherever_you_have_mounted_your_system/dev
sudo chroot /wherever_you_have_mounted_your_system
mount -a

Now that you are in your system you can rebuild your initrd by reconfiguring Linux with the command

dpkg-reconfigure linux-image-<kernelversion>

Where <kernelversion> is the version of Linux you have installed that you wish to reconfigure. The default for Dapper is 2.6.15-23-386, but you can see all of the possible versions with (from within your system):

apt-cache search linux-image-2.6

Or

ls /lib/modules

If the reconfigure command was successful there should be some lines about rebuilding initrd, updating GRUB, etc. Now you can find your new initrd and matching kernel in /boot of your system (that is /wherever_you_mounted_your_system/boot in the Live CD's system) and they are called initrd.img-<kernelversion> and vmlinuz-<kernelversion>. You should exit from the chroot now by pressing ctrl-d. Now you need to copy these files, along with a configuration file for the bootloader GRUB, to the Live CD's home folder with the commands:

cp /wherever_your_system_is_mounted/boot/initrd.img-<kernelversion> ~
cp /wherever_your_system_is_mounted/boot/vmlinuz-<kernelversion> ~
cp /wherever_your_system_is_mounted/boot/grub/menu.lst ~

Now that you have a kernel which can boot your USB drive it is time to put it on a bootable CD. You will need to download this archive of the GRUB source code and extract it to obtain the "stage2_eltorito" file inside. Now that you have the needed files we can make your CD.

Make a folder in the Live CD's home folder called "bootcd", then inside there make another called "boot" and inside there one called "grub". Now copy the stage2_eltorito and menu.lst to this new "grub" folder and copy the vmlinuz and initrd files into the "boot" folder above it. Rename the vmlinuz-<version> file to simply "vmlinuz" and the initrd.img-<version> file to simply "initrd.img" (this means you won't have to change your configuration later to point to any new filenames) Edit the menu.lst file with the text editor and look at the bottom where the actual OS entries are. Delete what is there and make two new entries (replace /dev/sda1 with your root partition if this is not correct):

title          Ubuntu
root           (cd)
kernel         /boot/vmlinuz root=/dev/sda1 ro quiet splash
initrd         /boot/initrd.img
boot

title          Ubuntu Recovery Mode
root           (cd)
kernel         /boot/vmlinuz root=/dev/sda1 ro single
initrd         /boot/initrd.img
boot

By default the top entry will be booted. You can change some of the other options here if you want a hidden menu or a different timeout before the default entry is booted (never set this to zero or you will not be able to enter recovery mode). That is the entire contents of your bootable CD, so now we have to build it. Open a terminal (by default it will be in the home folder) and run (all on one line):

sudo mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o UbuntuBootCDForUSB.iso bootcd

You should now have a CD image called UbuntuBootCDForUSB.iso which you can write to a recordable CD using many common CD writing programs such as the one built into the Nautilus file manager (Ubuntu's default file manager), GnomeBaker, k3b, Nero and many more. If you don't have your CDR/W drive accessible (because you are running a LiveCD in it) then you can copy the CD image to /wherever_you_have_mounted_your_system to keep it safely stored on a real drive. You should also copy over the "bootcd" folder. This can be done with:

sudo cp UbuntuBootCDForUSB.iso /wherever_you_have_mounted_your_system
sudo cp -r bootcd /wherever_you_have_mounted_your_system

They will now be accessible in your / directory (move them out of there once your system is set up as this is an important directory and should not be cluttered). You can save the iso file to a FAT formatted USB stick (obviously not one you have just set up with Linux) to transfer the CD image to Windows where it can be written by Nero.

Now you just have to boot off the CD you have written whilst your USB drive is plugged in and (depending on how you set up GRUB) you will be sent into Ubuntu's boot procedure. (Notice the "Waiting for root filesystem" line? You did that!). If GRUB gives an error then you can edit the bootup lines by pressing "e" with the right entry highlighted in the menu.

这个系统的缺点

当Ubuntu内核更新的时候你并不会注意到,这是因为你仍是从你的CD上运行旧内核。这很容易纠正,只需把"vmlinuz"和"initrd.img"这两个文件用"bootcd/boot"文件夹中的新版本代替,并重新运行上述"sudo mkisofs"命令来创建一个新的映像(新内核将自动包含我们所做的改动,因为我们已经把变化加入到了系统的配置文件中)。将新映像写到磁盘上之后,你就可以用它来启动新内核了。(我不推荐只使用一张CDRW光盘、每次都覆盖写入,因为如果这样无效的话你就会遇到麻烦。我建议使用两张CDRW光盘,更新一张。一旦这张更新光盘已经被检测并可正常使用,那么你就应该更新第二张光盘了。)

See Also

Building a bootable GRUB CD| 创建一张可启动的GRUM CD - 这是我写这个指南时使用的GNU GRUB的官方文件。

Comments

上述指南假设只在一个分区上安装Ubuntu系统。我曾用另一个分区作为/boot,结果遇到了麻烦。为避免这一点,在从/ 一般挂载的Ubuntu系统中创建CD。

  • Thanks for the info, I added a line to bind /dev to the chroot and a mount -a command, which should take care of that. A /boot partition becomes basically redundant if you use this LiveCD method, so I don't think it would be a common situation. Personally I have only made /boot seperate when using LVM, but with a few extra modules this CD could boot directly to LVM, I may add that later (the CD image is tiny, so more features wouldn't hurt).