将Gentoo安装在可启动的U盘上

From Gentoo Wiki
Jump to: navigation, search
This page is a translated version of the page Install Gentoo on a bootable USB stick and the translation is 28% complete.

Other languages:
Deutsch • ‎English • ‎español • ‎日本語 • ‎한국어 • ‎русский • ‎中文(中国大陆)‎

从U盘启动Gentoo真的很简单。主要的挑战是设置一个初始的ramdisk,并囊括所有要启动的机器的驱动程序。本文将介绍如何将Gentoo安装到可以在任何计算机上启动的U盘上。这种安装就像一般的桌面安装一样。changes will be persistent.

准备

Note
The guide assumes that the current platform is a Gentoo Linux one. The majority of steps however can be performed from any distribution, just translate the Gentoo-specific way of doing things (such as installing software) to the other distribution's practices.

Start by installing a couple of necessary packages. Most importantly these are a kernel and the sys-kernel/genkernel package:

root #emerge --ask sys-kernel/gentoo-sources sys-kernel/genkernel

To add package-specific useflags, create a new file in /etc/portage/package.use/:

FILE /etc/portage/package.use/sys-apps
sys-apps/busybox static

接下来安装 sys-apps/busybox 包:

root #emerge --ask sys-apps/busybox

编译内核

Make the required changes to the kernel and compile it. Please see 配置内核 and the 内核配置向导 for more detail on this step. Importantly remember that a general kernel is better here because the more general it is the more machines will boot with it.

During the configuration, it is of course possible to compile modules as these will be included in the initramfs and loaded at boot.

root #mkdir /tmp/boot
root #genkernel --firmware --busybox --disklabel --bootdir=/tmp/boot --no-symlink --all-ramdisk-modules --install all

The above command will take some time as it will build the default kernel, create a ramdisk and then copy all files into /tmp/boot.

root #ls /tmp/boot/
initramfs-genkernel-x86_64-3.5.2-gentoo
kernel-genkernel-x86_64-3.5.2-gentoo
System.map-genkernel-x86_64-3.5.2-gentoo

准备U盘

Create 2 partitions on the drive (assuming the installation will have a /boot and a / (root) partition). We assume that the USB stick is at /dev/sdb (run dmesg immediately after plugging in the USB stick to see which device is being used).

root #fdisk /dev/sdb
Command (m for help): d
Selected partition 1
 
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4001759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4001759, default 4001759): +100M
 
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2):  
Using default value 2
First sector (206848-4001759, default 206848): 
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-4001759, default 4001759): 
Using default value 4001759
 
Command (m for help): a
Partition number (1-4): 1
 
Command (m for help): p
 
Disk /dev/sdb: 2048 MB, 2048901120 bytes
255 heads, 63 sectors/track, 249 cylinders, total 4001760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x001663df
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048      206847      102400   83  Linux
/dev/sdb2          206848     4001759     1897456   83  Linux
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.

Make sure that the first partition is bootable (a command toggles the boot flag).

Now format the new partitions. In the example we use the ext2 file system but another file system for the / partition can be used if it is supported in the kernel. Notice that during the formatting operation, a label is assigned to the partitions. This is important because it will be how the root file system is detected later as the USB stick can be booted on systems where the drive letter allocation is completely different. This is related to the --disklabel option of the genkernel command line earlier.

root #mkfs.ext2 -L GENTOO_USB_BOOT /dev/sdb1
root #mkfs.ext2 -L GENTOO_USB_ROOT /dev/sdb2

Gentoo stage 3安装

With the kernel, ramdisk and file systems ready, we now need the minimal installation to be performed. This includes a fresh stage3 as well as a Portage tree snapshot.

For this example stage3-amd64-20120621.tar.bz2 and portage-latest.tar.xz are used. This next bit is the same as in the handbook for installing Gentoo.

root #mount /dev/sdb2 /mnt/gentoo
root #cd /mnt/gentoo
root #tar -xpf ~/Download/stage3-amd64-20120621.tar.bz2
root #cd usr
root #tar -xpf ~/Download/portage-latest.tar.xz
root #mount /dev/sdb1 /mnt/gentoo/boot
root #cp /tmp/boot/* /mnt/gentoo/boot

The sections below cover those aspects of a Gentoo Linux installation that are specific to installing on an USB stick. It is recommended to use the Gentoo handbook instructions while keeping an eye on this article.

Bootloader

In order to boot the new system, install a bootloader on the USB stick.

The below sections give example configurations for lilo and syslinux.

lilo

Emerge lilo (from within the chroot):

(chroot) #emerge sys-boot/lilo

Edit etc/lilo.conf so that it has the correct information in it.

Warning
These settings are very important and will be discussed line by line.
FILE /etc/lilo.confConfigure lilo.conf
boot=/dev/sdb              # The location of the USB Stick (currently)
lba32                      # use lba32 addressing (ignore)
compact                    # boot quickly by loading lots of blocks
                           # remove when there are problems with booting
prompt                     # Prompt for user input
timeout=20                 # Time to wait before default selection
default="Gentoo-352"       # Default selection after timeout
  
image=/boot/kernel-genkernel-x86_64-3.5.2-gentoo
	label="Gentoo-352"
	read-only
	root=/dev/ram0
	append="real_root=LABEL=GENTOO_USB_ROOT scandelay=5"
	initrd=/boot/initramfs-genkernel-x86_64-3.5.2-gentoo

The lines after image are all easy but the append line contains some interesting options.

  • real_root=LABEL=GENTOO_USB_ROOT will use the label of the disk instead of /dev/sdb2 which is important because these device numbers move around depending on the number of hard disks in the computer or number of USB disk drives.
  • Also important for USB booting is the scandelay option, as USB devices need a little time to be detected by the kernel; that is what this option is for. When there are lots of modules booting takes a long time so it probably does not matter, but if there are only a few modules loaded then it is important, because the system could have booted before the kernel detects the USB device. Also, on newer machines with USB3 controllers it is necessary to add the xhci_hcd driver (built into the kernel or as a module).

Now install LILO (with the configuration) on the USB stick:

(chroot) #lilo
Warning: /dev/sdb is not on the first disk
Warning: The initial RAM disk is too big to fit between the kernel and
   the 15M-16M memory hole.  It will be loaded in the highest memory as
   though the configuration file specified "large-memory" and it will
   be assumed that the BIOS supports memory moves above 16M.
Added Gentoo-352 ? *
2 warnings were issued.

The first warning is to be expected as we are not installing the boot loader onto the hard disk in the computer. The second warning will only cause a problem on machines that are old; if it is a problem try cutting down the kernel by removing modules.

syslinux

Warning
syslinux currently only works with ext{2,3,4}, btrfs, ntfs and fat filesystems.
(chroot) #emerge syslinux

Put the boot sector onto the USB stick.

(chroot) #dd bs=440 count=1 conv=notrunc if=/usr/share/syslinux/mbr.bin of=/dev/sdb

Now to configure the bootloader.

(chroot) #mkdir /boot/syslinux
(chroot) #nano -w /boot/syslinux/syslinux.cfg
FILE /boot/syslinux/syslinux.cfg
PROMPT 1
TIMEOUT 50
DEFAULT gentoo
 
LABEL gentoo
        LINUX ../kernel-genkernel-x86_64-3.5.2-gentoo
        APPEND root=LABEL=GENTOO_USB_ROOT scandelay=3 ro
        INITRD ../initramfs-genkernel-x86_64-3.5.2-gentoo
Note
The ro in the above APPEND line will cause the root partition to be mounted read-only (which is usually preferred on USB sticks). Replace ro with rw to allow the system to be modified from the boot onwards (instead of having the user mount the file system as read-write later).

Next install syslinux onto the USB stick. The --device /dev/sdb1 option is not absolutely necessary though.

(chroot) #extlinux --device /dev/sdb1 --install /boot/syslinux

fstab

FILE /etc/fstabConfigure the fstab file to work with labels
LABEL=GENTOO_USB_BOOT   /boot           ext2            noauto,noatime  1 2
LABEL=GENTOO_USB_ROOT   /               ext2            noatime         0 1
/dev/SWAP               none            swap            sw              0 0
/dev/cdrom              /mnt/cdrom      auto            noauto,ro       0 0
/dev/fd0                /mnt/floppy     auto            noauto          0 0

提示与技巧

Although it is possible to use the system just made as a standard Gentoo system, it might be worthwhile to compile binary packages on a host and then installing them on the USB stick. Or if the system has enough memory just mount /var/tmp to a tmpfs, as compilation will be much quicker that way anyway!