安裝Linux操作系統到移動硬盤(內功心法)

英文版
Boot Linux from a FireWire device (also for USB device)

--------------------------------------------------------------------------------


This is a very good article from IBM developerworks that clearly explain the principle on how to boot Linux from Firewire and USB devices.

Hope it is useful for anyone interested in installing Linux to external devices.

Original URL http://www-128.ibm.com/developerwor...l-fireboot.html

**************************************************************
Boot Linux from a FireWire device

Installing Linux on removable drives

Level: Intermediate

Martyn Honeyford ([email protected]), Software Engineer, IBM UK Labs

15 Jul 2004

Obtaining an external drive is a great way to breathe new life into older hardware, or allow you to use Linux on machines on which you can't (or don't want to) alter the internal hard drives.

Say you want to use Linux in a dual-boot arrangement, but you don't have any free space on your computer's hard drive. One solution would be to use a "live" Linux distribution such as Knoppix, which can be run directly from CD. This is certainly viable for occasional use, but it has a number of serious drawbacks:

* You will still require some permanent storage for your data files. A floppy disk may be suitable if you only work with very small files, or a USB flash memory key may suffice for medium-sized files, but neither of these is ideal.
* When using a "live" CD, it is at best difficult -- and at worst, impossible -- to install your own applications, or to customize existing applications.
* There is a performance penalty to using live distributions, most notably on start-up while all of the devices are being detected -- but also while running (as everything has to be loaded from CD, which is usually much slower than from the hard drive).

To be sure, other options also exist. For instance, you could buy another internal drive and install Linux on that. But often, you may not have any free drive bays in your case (this is particularly true of laptops, which typically only allow one internal hard drive).

Or, you could replace the current drive with a larger one, and install Linux in the resulting extra space. This, however, is a very time-intensive option, as it requires you to reinstall your existing OS on the new drive, reinstall and reconfigure all of your applications, and restore all of your data.

A much better solution is to purchase an external hard drive and install Linux on that. This lets you leave your existing hardware and software untouched and simply connect the external drive when you want to use Linux.

Removable drive options

The range of removable devices onto which Linux may be installed ranges from floppy drives, through USB-flash devices, on to USB/FireWire hard drives, and more.

While it is certainly possible to install Linux on a small-capacity device such as a 1.44MB floppy or a 32MB USB-key, these are usually (necessarily) specialized, cut-down distributions intended for, for instance, rescuing broken installations.

External hard drives, however, offer the most flexibility for a general purpose Linux distribution at a reasonable cost.

External drives are available in a large number of different sizes from a number of different manufacturers (Maxtor, Western Digital, and so on). These drives tend to consist of an external box, which holds a standard 3-1/2 inch or 2-1/2 inch IDE drive. These drives then are typically connected to the computer via USB or IEEE1394 (FireWire) connection.

USB comes in two main versions, 1.1 and 2.0. Version 1.1 has a maximum transfer speed of 12 Mbit/s (megabits per second), whereas version 2.0 supports transfer speeds of up to 480 Mbit/s. While most 2.0-compatible drives are backwardly compatible with 1.1, it is generally advisable to avoid using 1.1 unless there is no other option (due to its slow speed).

The FireWire standard also defines a number of different possible speeds, but in reality, whenever people say FireWire, they mean "FireWire400," which supports transfers up to 400 Mbit/s.

There is little to choose between USB 2.0 and FireWire in terms of speed: although USB 2.0 has a higher quoted speed, in practice they both tend to be comparable due to differences in the protocols. If your machine has both, it is probably worth going for USB rather than FireWire (for reasons I'll explain later), but if it has only FireWire, then of course you will go for that. For maximum flexibility, choose one of a number of drives that support both USB 2.0 and FireWire (such as the one I use later in this article).

For machines that do not have the required ports, PCI (for desktop) and PCMCIA (for laptop), FireWire and USB 2.0 cards are now available very inexpensively: for example, the PCMCIA FireWire card I use later in this article was purchased for approximately 10 GBP (under $20 US).

For the purposes of this article, I have purchased a 5-1/4 inch external drive enclosure. This is a very flexible enclosure, which is supplied with no drive and can be filled with any standard IDE device, including 3-1/2 inch hard drives and 5-1/4 inch IDE devices such as CD-RW/DVD-RW drives. The enclosure has both USB 2.0 and FireWire connections.

In order to connect the enclosure to my IBM Thinkpad T30 laptop, I also purchased a PCMCIA FireWire card (as the built-in USB ports only support USB 1.1).

Both the enclosure and the FireWire card were relatively inexpensive (approximately 50 GBP and 10 GBP, respectively).

For testing purposes, I fitted the enclosure with a 13GB 3-1/2 inch IDE drive I had laying around -- for real world usage, I would buy a larger capacity drive, which again are now very cheap (approximately 50 GBP per gig!)

Linux support

As you may expect, Linux support for these enclosures is very good indeed. Any device that adheres to the SBP (Serial Bus Protocol) standard for "Mass Storage Devices" can easily be used with Linux.

In general, to enable support for these devices you will need to have a number of things supported in your kernel (either directly compiled in or via modules).

For both USB and FireWire, SBP device support is implemented via SCSI emulation -- that is to say, the devices appear to Linux as though they were SCSI disks. This is a common way to abstract storage devices within the Linux world (for instance, IDE CD/DVD drives are typically also connected using SCSI emulation). For this reason, the following kernel support is required:

* SCSI support
* SCSI emulation
* SCSI disk support

In addition, the following support will be required according to the connection method:

* For FireWire:
* IEEE1394 support
* OHCI1394 support
* RAW1394 support
* SBP-2 support

* For USB:
* (host-side) USB support
* OHCI support
* UHCI support
* USB mass-storage support

You will obviously have to have all the normal support for the rest of your hardware (graphics cards and so on), and may require some additional modules, depending on your exact hardware.

For instance, I am using a PCMCIA (cardbus) FireWire card, so I needed to add:

* PCMCIA support
* cardbus support

Installation

Now that we have our nice external drive, we shall start by installing Linux on it.

The easiest way to install Linux these days (certainly in my opinion), is to connect all of your hardware (in my case, this consisted of plugging in the PCMCIA FireWire card, attaching the FireWire cable to the PCMCIA card and the drive, and turning on the drive's power switch); then boot up your machine with the installation CD from your distribution of choice.

My distribution of choice is Gentoo (see Resources for a link), so I used the latest "Universal" x86 Live CD (2004.1). The steps required for other distributions should be more or less comparable to those outlined here.

Once you have booted with the install CD, with a bit of luck it should have recognized your drive. The drive should appear as a disk under /dev/sdX, where X is a lowercase letter starting at "a." On my system, the external drive was detected as /dev/sda, but this will vary if you have other SCSI disks (or emulated SCSI disks); in that case, it might be /dev/sdb or some other letter. If your drive is not detected automatically, some further steps may be required -- for instance, you might have to pass boot options to enable FireWire or PCMCIA, or you might have to manually load some kernel modules, or other things of that sort (see Resources for links to troubleshooting guides).

Once the drive has been recognized, it should behave exactly like an internal hard drive as far as the rest of the installation is concerned; so you should be able to partition it as required and install Linux as normal.

One word of caution, however: care needs to be taken when deciding on where to install the boot loader (usually GRUB or LILO) -- I would recommend not installing it in the Master Boot Record (MBR) (which is usually the default). Rather, it should be installed in the root partition (or boot partition, if you use a separate one) of the external drive.

Now that we have Linux installed on the device, we want to boot it up. This is where things can start to get a little tricky.

Booting

Before I discuss booting up your new drive, a little boot loader theory is required.

Boot loaders are usually installed in the MBR of the first hard disk in the machine. When the boot loader is invoked (the BIOS automatically executes the code in the MBR), it usually displays a menu of possible OSes to boot. Selecting a given OS causes it to boot up.

Two things should be noted about this scenario:

* The menu of OS choices is (usually) loaded from disk
* To boot the relevant OS, the bootloader needs to read the relevant kernel from disk

As the above takes place before the OS has been loaded, it means that all of the disk reading must take place by way of BIOS calls. This has a very serious implication: namely, that in order to boot the disk directly, your BIOS must support disks connected via FireWire or USB. This can typically be seen as a BIOS option to boot from these types of disk. FireWire BIOS support is currently very rare indeed, but USB support is becoming reasonably common. Therefore, if you are using USB on a relatively recent machine, it should be possible to boot the drive into Linux directly.

After installing GRUB in the MBR of the external drive, I was able to boot it directly when connected via USB. Simply enter the BIOS setup utility while booting with the disk connected. The external disk will appear as a regular hard drive: move it so it is before the internal drive in the boot order.

I was also able to install a boot loader in the MBR of the internal drive and use that to boot the USB drive (where it appeared as hd1 in GRUB). If you are using FireWire, chances are that your BIOS will not be able to boot the drive directly, and a little more work will be required.

Luckily, due to the flexibility of Linux, there is a fairly simple solution if you cannot boot directly (which is certainly the case in my situation, with a PCMCIA FireWire card!) You can perform the preliminary boot steps from a supported device such as a floppy drive, CD, USB key, or a tiny partition on the main drive, and then use the external drive for everything else.

Building the boot image

There are two methods we can use to boot:

* One-phase boot
The kernel boots, mounts the root filesystem, and continues initialization by calling the initialization scripts (usually /sbin/init)
* Two-phase (initrd) boot
The kernel boots, mounts an initial ram disk (initrd), performs further customizable initialization, then mounts the root filesystem and continues initialization (again, usually by calling /sbin/init)

Each of these methods has its own advantages and disadvantages.

One-phase boot

In order to use the one-phase boot, we need to build a kernel that has all the drivers needed to mount the root file system built in (any other drivers can be built at modules that can be loaded from the root partition during normal initialization).

If we are attempting to boot from a very small device, such as a floppy disk, the best approach is to build a kernel with just enough drivers built in to allow us to mount the root external file system -- and build everything else as modules. For example, I have the SCSI support, PCMCIA support, IEE1394, SBP, and like support built in, but everything else -- including graphics card support, networking device support, and so on -- is built as modules that are stored on the root partition (on the external drive), rather than on the floppy.

With a simple (one-phase) boot procedure, we should be able to build a kernel with the required support, put it on a floppy drive, install a boot loader on the floppy (I use GRUB, but there are other options, such as LILO), then boot with something similar to this (for GRUB):

root (fd0)
kernel (fd0)/boot/bzImage root=/dev/sda1

This almost works, except for two problems:

1. Because of the fact that SBP support uses SCSI emulation, the emulated SCSI bus need to be "rescanned" in order to detect the disk and allow /dev/sda1 to be mounted. This scanning is performed with a couple of simple commands. Unfortunately, however, using a one-phase boot, we cannot run any commands until the kernel has finished booting, and the kernel cannot finish booting until the root file system is mounted -- a classic Catch-22 situation. Thankfully, there is a patch available for 2.4 kernels that causes the SCSI bus to be scanned on startup (see Resources for more details). By applying this patch, I was able to have the external drive automatically detected by the kernel during bootup with no rescanning commands required. This leads us to the next problem.
2. There is a timing window within the kernel, which means that often, the kernel will proceed to try to mount the root device before it has had a chance to be properly detected or initialized. Again, there is a patch available for this (please see Resources for a link) that simply makes the kernel wait for a short period of time on startup, and retry if it fails to mount the root filesystem (to give the external drive time to be recognized).

By applying these two patches, I was successfully able to build a kernel on a bootable floppy disk, which would boot and then use the external FireWire drive as root.

The main problem with this approach is that it requires us to patch the kernel source -- which is at best a pain (when new kernel versions are released) and at worst, a real problem (if the patches are not maintained to keep in step with the other changes occurring to the kernel).

It may have occurred to you that we can avoid these two problems if our BIOS supports USB or FireWire and we are booting directly. Unfortunately, this is not the case: while this method uses BIOS calls to access the disk during boot up, once the kernel begins to initialize, the BIOS is no longer used, and the disk is accessed using the kernel drivers -- so the same problems are encountered.

Two-phase boot

As of kernel version 2.0.X, an interesting capability was added to the Linux kernel -- the ability to use an "initial RAM disk" (or initrd) to give a two-phase boot process.

In a nutshell, the kernel is booted as normal; but instead of mounting the "real" root file system, a miniature root filesystem is created in RAM, and that is mounted. Any number of arbitrary steps can then be performed in this initial environment before the real root is mounted and we switch to using the real root and destroy the initial RAM disk.

This is useful in all sorts of circumstances, but for our purposes we will simply be using our mini environment to rescan the SCSI bus, wait for the external disk to be recognized, then switch to using this as our real root and continue to boot.

In order to use this method, we need to create two things, a kernel and an initrd image.

The kernel is just a regular kernel which has initrd support built in. The initrd image is a loopback filesystem image which contains our mini-root filesystem (this image can optionally compressed with gzip to reduce its size).

You can check the Resources section for more information on creating or customizing your own initrd image.

Within the initrd image, there is a file called linuxrc. This is the file that is executed when initrd is loaded, so make sure it has execute permission! For our purposes, the linuxrc is very simple:

Listing 1. initrd linuxrc


#!/bin/sh
REAL_ROOT=/dev/sda1
# mount the /proc filesystem
mount -t proc none /proc

#for scsi-emulation
# modprobe sd_mod

#for pcmcia
# modprobe pcmcia_core

#for FireWire
# modprobe ieee1394
# modprobe ohci1394
# modprobe raw1394
# modprobe sbp2

#for USB
# modprobe usbcore
# modprobe ohci-hcd
# modprobe uhci-hcd
# modprobe usb-storage

# loop rescanning the scsi bus + rerunning devfsd
retries=5
i=1
until [ -e $REAL_ROOT ]
do
if [ $i -gt $retries ]
then
echo "Unable to mount real root ($REAL_ROOT) - Giving up!"
/bin/ash
exit
fi

echo "Real root ($REAL_ROOT) not found, retrying ($i)"
sleep 1
echo "scsi add-single-device 0 0 0" > /proc/scsi/scsi
echo "scsi add-single-device 1 0 0" > /proc/scsi/scsi
echo "scsi add-single-device 2 0 0" > /proc/scsi/scsi
/bin/devfsd /dev -np
i=$((i+1))
done

#umount /proc as it will be remounted by the normal init process
umount /proc

#now we simply exit, and the normal boot process should continue
exit 0


All we are doing is loading the appropriate modules to support the external drive: they should be uncommented as required. (I built all the required support into my kernel, hence no modules are required.) We then loop, rescanning the SCSI bus (by echoing a command to a special file in the /proc pseudo-filesystem and calling devfsd) until the root device (/dev/sda1 in my case) is present. In my case, the emulated FireWire SCSI bus in question is 1 0 0, but it doesn't hurt to try a few others -- if you know which one you will be using, you can tailor the script. Also, if you have other SCSI devices (or emulated SCSI devices), the drive may have a different letter (for example, /dev/sdb1). And if you are not using the first partition on the external drive, you will need to use a different number (for example, /dev/sda2).

All we now need to do is copy the relevant files into the initrd image (you can mount the uncompressed image using the mount -o loop command). In particular, we need to ensure that we have the linuxrc file, all of the commands used therein, and any libraries that those commands rely on. The (unmounted) image can then be optionally compressed.

The kernel (bzImage) and the initrd image (initrd.gz) are then copied to the (bootable, ext3) floppy.

The final step is to install a boot loader on the floppy, and boot the kernel with the following options: kernel bzImage root=/dev/sda1 initrd=initrd.gz.

You should now be able to boot using the floppy: it will load the kernel from the floppy, load the initrd image into RAM, wait for the root device to be recognized, and then continue to boot as normal from there. From this point onwards, the floppy can be removed.

If a floppy is not appropriate (for example, if the machine has no floppy drive), then any device that can be booted by your BIOS can be used. Personally, I use a little 32Mb USB key for this purpose. Alternatively, if you don't mind altering your internal hard drive, a small partition can be created there for more convenient booting.
 
中文版
這是來自 IBM developerworks 的一個非常好的文章可以清楚地解釋如何啓動火線和USB設備 Linux操作系統的原理。

希望它在對任何有興趣把Linux操作系統安裝到外部設備的人有所幫助。
原始的網址http://www-128.ibm.com/developerworks/library-combined/l-fireboot.html

從火線設備上啓動Linux

可移動驅動器上安裝Linux操作系統

難度等級:中等

Martyn Honeyford([email protected]) IBM英國實驗室 軟件工程師
宣可達([email protected])翻譯 webzi@linuxsir


2004年7月15日
2005年7月26日開始翻譯,7月28日完成。
翻譯經驗不多。如有不妥,歡迎EMAIL交流。

使用一個外部驅動器是一個極棒的方法,能給你的老硬件帶來新生和讓你在你不能(或者不想)改變內置硬盤的計算機上使用Linux系統。
假設你想使用Linux在一個雙啓動環境,但是你的計算機硬盤又沒有任何空餘的空間。一種解決方法是使用"live"的linux發行版本象是Knoppix,他們這能直接從CD啓動運行。 對於偶爾使用,這是一種可行的辦法,但是它有若干的劣勢:

*你將會仍然需要些固定文件存儲。如果你只是操作少量的文件,一張1.44MB的磁盤就可以勝任,或者一個USB閃存棒適合中等量的文件,但是這些都不夠理想。
*當使用一個LiveCD 的時候,最困難、最糟糕、最不可能的是安裝你自己的應用程序或者定製現存的程序。
*使用LiveCD在性能上的犧牲,在啓動初所有的設備檢測時尤爲明顯,同樣發生使用中(所有的文件從 CD 載入通常是比從硬盤中載入慢的多)。

的確,還有其他可選方案。舉例來說,你可以買額外的內置驅動器並且在此安裝Linux操作系統。但是常常,你可能沒有任何空閒的擴展阜。(這特別容易發生在筆記本上,通常他們只允許一個內置硬盤。)

或者,你可以使用一個較大的硬盤取代當前的硬盤, 獲得額外空間用於安裝 Linux操作系統。 然而,這的確是一個耗費大量時間的方案,需要你重新安裝現有的操作系統在新的硬盤, 重新安裝和配置你所有應用程序, 和恢復所有數據。

更好的解決方案是購買一個外置硬盤並安裝 Linux操作系統。 這樣使得你在想使用Linux系統時候不必涉及現有的硬件和軟件而只是簡單連接外部驅動器。

可選的可移動設備

可用於安裝Linux操作系統設備涵蓋磁盤驅動器、USB-閃存設備、通過USB/火線連接的硬盤等等。

在一個小容量的設備中安裝linux是可行的,例如在一個1.44MB的磁盤或者一個32MB的USB棒中,他們通常(必須)爲特定目的定製,採用裁減的發行版本,例如,拯救中斷的安裝過程。

外置的硬盤在爲使用一個常規目的的linux系統提供最大的靈活性同時,帶來一個合理的成本。

許多不同的廠商提供許多種不同容量的外置的驅動器。 (Maxtor, 西西部數據, 等等)。這些驅動器還會包含一個外置盒子,用於固定標準的 3-1/2 寸或 2-1/2 寸 IDE 硬盤。 然後這些驅動器經由 USB 或 IEEE1394(火線) 連接到計算機。

USB 受到在二個主要的版本中的影響,1.1 和 2.0. 1.1 版速度爲12 Mbit/ s(每秒1Mbit) ,而 2.0 版支持最高達到 480 Mbit/s 的傳輸速度。 大多數2.0相容的驅動器也向後兼容1.1,通常建議儘量不要採用1.1接口除非沒有其他的選擇(因爲其低速不適應此環境。)

火線標準也定義若干不同的速度, 實際上,大部分人所指的FireWire ,是“FireWire400”標準 ,支持最大傳輸速度爲 400 Mbit/s 。

USB2.0和火線在速度上不分伯仲: 雖然 USB 2.0 有比較高的傳輸速度, 但是實際中因爲協議的不同兩者速度十分的接近。 如果你的計算機擁有2種接口, 最好使用USB而非火線(理由我將後將解釋), 但是如果只有火線接口,當然你就選擇它了。爲了實現最大的靈活性, 可以選擇衆多同時支持 USB 2.0 和火線的驅動器中的一種。(像我稍後將在這一個文章中使用的那個一樣。)

對於那些沒有必須的接口, PCI(對於桌面) 和 PCMCIA(對於筆記本型電腦) ,現在火線和USB 2.0擴展卡也並非貴不可及: 舉例來說, 我稍後將在文章中提及PCMCIA火線擴展卡大約爲10英鎊.($20 美元以下)

爲了達到本文的目的,我已經購買 5-1/4寸外置驅動器的硬盤盒。 這是一件非常靈活的硬盤盒,並沒提供驅動器而且能固定任何的標準 IDE 設備,包括 3-1/2 寸驅動器和像CD-RW/DVD-RW等5-1/4 寸 IDE 設備 。硬盤盒兼有 USB 2.0 和火線接口。

爲了把硬盤盒連接到我的IBM Thinkpad T30筆記本電腦,我也購買了一個 PCMCIA 火線轉接卡。 ( 因爲內置的USB 接口只支持USB 1.1)

硬盤盒和火線轉接卡相對比較便宜。 (大約分別爲50英鎊和10英鎊。)

測試期間, 我把一個手邊閒置的13 GB 3-1/2 寸 IDE 硬盤放進硬盤盒——對於真實情況,我會買一個較大的容量驅動器, 現在另購一個硬盤相當低廉( 大約每個50英鎊!)

Linux 支持

正如你所期待,linux對這些硬盤盒支持的確非常好。任何遵從SBP(串行總線協議)標準的大儲存設備在linux中使用非常簡單。

大體上, 使對這些裝置的支持你能夠將會需要在你的核心中支持某些選項( 或直接地編譯或通過模塊。)

對於USB和火線,SBP 裝置通過SCSI模擬來實現-- 也就是說,Linux會把他們當作SCSI硬盤來處理 。 這是在 Linux 世界裏面使用存儲設備的一個常規方法。 ( 舉例來說, IDE CD/DVD設備通常通過SCSI模擬來連接)。基於以上原因,下列的選項需要被內核支持:

* SCSI support
* SCSI emulation
* SCSI disk support

除此之外,不同連接方式需要一下的內核支持:

* 針對火線:
* IEEE1394 support
* OHCI1394 support
* RAW1394 support
* SBP-2 support

* 針對USB:
*(host-side) USB support
* OHCI support
* UHCI support
* USB mass-storage support

明顯地,你還必須爲其他的硬件 ( 如顯卡等等)編譯常規的支持, 而且可能需要一些額外的模塊,取決你的具體硬件。

舉例來說,我正在使用 PCMCIA(cardbus) 火線轉接卡,因此,我還需要增加:

* PCMCIA support
* cardbus support

安裝

現在我們有很完美外置驅動器,我們將開始安裝在其安裝Linux美妙之旅。

目前最簡單的安裝linux方法(當然僅個人意見) , 是連接你的所有硬件;(我的情況是,插入火線轉接卡,連上轉接線盒驅動器,然後打開驅動器的電源) 然後從你選擇的發行版安裝CD上啓動你的計算機。

我採用的發行版是Gentoo(資源詳見鏈接),因此,我用了最新的 “通用”x86 LiveCD。 (2004.1) 其他的發行版本必須的步驟大致和此相似。

一旦你使用安裝CD啓動,要藉由一點運氣讓他要辨認出你的驅動器。 磁盤應該出現在/dev/sdX, X 是一個小寫字母從“a”開始。 在我的系統上,外置硬盤被當做 /dev/sda, 但是如果你有其他的 SCSI硬盤,這數字將會改變;( 或模擬了SCSI硬盤) 那種情況下,它可能是 /dev/sdb 或一些其他的字母。 如果你的驅動器不能被自動檢測到, 就需要進一步採取措施 -- 舉例來說,你可能必須打開啓動選項使用火線或者PCMCIA接口, 或你可能必須手動裝載一些內核模塊 , 或其他一類選項。 (資源詳見故障發現與維修指導鏈接)

一旦驅動器被辨認出來,安裝過程種其運作起來就像一個內置的硬盤。然後就你可以對其分區並且用常規方法安裝linux。

有一句話我着重要提醒,小心選擇何處安裝boot loader(通常是GRUB和LILO)--我建議不要裝到主引導記錄(MBR)中( 通常是默認值) 而是裝到外置驅動器的根分區 ( 或/boot分區,如果你單獨分了一個話)。

既然我們已經安裝 Linux 在這個設備上,我們想要從上面啓動它。 事情到此開始變的有點複雜了。

啓動

在我前討論在如何的新驅動器上面啓動,我要講解一些boot loader的小知識。

boot loader程序通常安裝在計算機的第一個硬盤的MBR上。 當boot loader被調用 (BIOS自動地運行MBR裏的代碼),它通常顯示一份可啓動操作系統的菜單。以便選擇一個給定的操作系統啓動計算機。

兩件細節在這個章節中應該注意:

* 操作系統選擇菜單(通常)從磁盤中載入。
*爲了啓動有關的操作系統, boot loader 需要從磁盤中讀取相關的內核。

在操作系統被裝載之前 ,以上各步驟已經執行,這意味着所有的磁盤讀取在BIOS調用時。 這是一個必須的前提,即,爲了直接啓動磁盤,你的 BIOS必須支持通過火線或USB被連接的磁盤。通常能在BIOS中看到關於從這些設備上啓動選項。 火線BIOS支持現在的確非常的少見,但是 USB 支持正在變得相當的平常。 因此,如果你正在使用一部相對比較新的計算機上的USB接口,應該可以直接啓動驅動器進Linux。

當經由 USB 連接,在安裝外部的驅動器的 MBR 的幼蟲之後,我能夠直接地啓動它。 當以被連接的磁盤片啓動的時候,只是進入 BIOS 裝備公用程序。 外部的磁盤片將會出現如一個一般的硬盤: 移動它,如此它在啓動次序中的內在驅動器之前。

我也能夠在一個內置的硬盤的MBR上安裝一個boot loader而且使用其啓動USB硬盤(在GRUB中被成爲hd1)。 如果你正在使用火線, 有可能你的 BIOS 將會無法直接地啓動硬盤,而且你需要更多一點步驟。

幸運的是,由於 Linux 的多樣性,這有非常容易解決,如果你不能直接地啓動,( 我就是這樣的情況,使用一個PCMCIA轉接卡!) 你能運行初始啓動步驟,來自一個支持的設備 , 像是一臺軟驅,CD-ROM,USB棒,或在主硬盤上的小分區, 然後使用外部的驅動器繼續其他步驟。

創建一個啓動鏡像

我們有2種方法啓動:

*單階啓動
內核啓動, 掛載根分區文件系統, 接着調用初始化教本繼續初始化( 通常是/sbin/init)
*雙階啓動 (initrd)的啓動
內核啓動,掛載一個初始ram disk (initrd),進一步運行定製的初始化, 然後掛載根分區文件系統繼續初始化(同樣, 通常調用/sbin/init)

每種方法都有它自己的優點和缺點。

單階啓動

爲了要使用一個單階的啓動,我們需要建立一個內核包含掛載一個根分區文件系統所需的所有驅動程序。( 任何其他的驅動能作爲模塊編譯並在根分區載入時候進行常規初始化。)

如果我們正在從像軟盤這樣的一個非常小的裝置嘗試啓動, 最好的方式是創建的一個正合適內核,編譯進所有掛載根外部分區系統所需驅動 -- 而且編譯其他的作爲模塊。 舉例來說,我的內核編譯進了SCSI支持, PCMCIA 支持, IEE1394 , SBP ,和其他相似的支持, 但是其他的(包括顯卡支持,網絡設備支持,等等)當做模塊編譯並儲存在根分區上(在外部的驅動器的),而不是存儲在軟盤上。

這種方法的主要問題是需要我們給內核源代碼打補丁-- 那是一種最糟糕的痛苦(當新的內核發佈),而真正的問題是如果補丁沒有在維護,這樣就不能跟上內核的變化。

你可能以爲如果計算機BIOS支持USB和火線直接啓動,我們能避免發生以上的2個問題。 不幸地,事情不是這樣:這一個方法使用BIOS在啓動時調用磁盤,一旦內核開始初始化, BIOS就失去了作用,而且使用內核驅動來調用磁盤-- 所以你仍然會碰到同樣的問題。

二階啓動

內核2.0.X版本 ,添加了一個有趣的功能到內核 -- 使用“initial RAM disk(初始內存磁盤)”(or initrd)來實現二階啓動。

簡言之,內核一概既往的啓動;掛載一個創建在內存裏迷你的根分區文件系統取代掛載“真正”的根分區系統。 無論任何命令都在這個初始環境中執行在“真正”的根分區系統被掛載之前直到我們切換到真正的根分區文件系統並銷燬初始內存磁盤(initial RAM disk)。

這點在各種環境中都非常有用,但是我們的目標僅僅是簡單的利用我們的迷你環境去重新掃描SCSI總線,等待外部驅動器識別,然後用它切換到我們真正的根而後繼續啓動。

使用這種方法,我們需要創建2個文件,一個內核和一個初始鏡像文件。

內核僅是一個內建啓動鏡像(initrd)支持常規內核。 initrd 鏡像是一個包涵我們迷你根分區系統的環路(loopback)文件系統鏡像。 (該鏡像可以有選折地使用gzip壓縮來減小它的大小)。

你能在資源章節裏找到關於如何創建和定製你自己的初始鏡像的更多信息。

在鏡像文件裏,有一個linuxrc的文件。當鏡像給載入時,該文件自動運行,所以請確定其有運行權限!爲了達到我們的目的,linuxrc文件非常的簡單:

列舉 1. initrd linuxrc

#!/bin/sh
REAL_ROOT=/dev/sda1
# mount the /proc filesystem
mount -t proc none /proc

#for scsi-emulation (SCSI模擬)
# modprobe sd_mod

#for pcmcia (PCNCIA卡)
# modprobe pcmcia_core

#for FireWire (火線)
# modprobe ieee1394
# modprobe ohci1394
# modprobe raw1394
# modprobe sbp2

#for USB (USB)
# modprobe usbcore
# modprobe ohci-hcd
# modprobe uhci-hcd
# modprobe usb-storage

# loop rescanning the scsi bus + rerunning devfsd
retries=5
i=1
until [ -e $REAL_ROOT ]
do
if [ $i -gt $retries ]
then
echo "Unable to mount real root ($REAL_ROOT) - Giving up!"
/bin/ash
exit
fi

echo "Real root ($REAL_ROOT) not found, retrying ($i)"
sleep 1
echo "scsi add-single-device 0 0 0" > /proc/scsi/scsi
echo "scsi add-single-device 1 0 0" > /proc/scsi/scsi
echo "scsi add-single-device 2 0 0" > /proc/scsi/scsi
/bin/devfsd /dev -np
i=$((i+1))
done

#umount /proc as it will be remounted by the normal init process
(解除掛載/proc分區當它會給常規的INIT進程重新掛載)
umount /proc

#now we simply exit, and the normal boot process should continue
(現在我們可以退出了,常規的啓動進程將會繼續)
exit 0

我們正在做的是載入適當的模塊去支持外部驅動器: 請按需注視相應行。 (我把所有需要的支持編譯進內核,因此不需要模塊)。 然後循環, 再掃描 SCSI 總線 (迴應一個命令到/proc虛擬文件系統下一個特別文件並調用devfsd程序) 直到根分區設備出現(我的情況是/dev/sda1 )。在我的情況,被訪問的火線模擬SCSI總線是1 0 0,但是並不影響嘗試部分其他的總線 -- 如果你知道將會使用哪一個,你可以裁減這個教本。 同時, 如果你有其他的 SCSI 設備 (或模擬 SCSI設備), 驅動器可能有一個不同的字母。(例如,/dev/sdb1) 而且如果你沒有使用外置驅動器上的第一個分區, 你將會需要使用一個不同的數字。( 例如,/dev/sda2)

現在我們需要做的全部是複製相關的文件進initrd鏡像.( 你能夠使用mount -o loop 命令掛載未壓縮的鏡像)尤其,我們需要確定我們有 linuxrc 文件、所有被用到的命令和其依賴的庫。 這個(未掛載的)鏡像可以有選折的壓縮。

複製內核 (bzImage) 和 initrd 鏡像 (initrd.gz)到磁盤。

最後的步是在磁盤上安裝一個boot loader, 而且用下列的選項啓動內核: kernel bzImage root=/dev/sda1 initrd=initrd.gz.

你現在應該可以使用磁盤來啓動了:它會從軟盤中載入內核,載入initrd鏡像入內存,然後從那裏繼續常規啓動。這點以後,磁盤就可以拿開了。

如果磁盤不合適 ( 例如,計算機沒有軟驅),任何設備在BIOS能用於啓動都可以使用。 個人而言,我就使用一個小小的32MB的USB棒來達到這個目的。 另外,如果你不介意改變你的內置硬盤,這裏創建一個小的硬盤分區更方便啓動。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章