通过grub-install命令把grub安装到u盘-总结

通过grub-install命令把grub安装到u盘


①准备一个u盘。容量不限。能有1MB都足够了。 
②把u盘格式化(我把u盘格式化成FAT、fat32格式了,最后证明也是成功的)。
③开启linux系统,打开命令行终端。进入root模式,然后输入命令行:
mount   /dev/sdb3   /tmp/boot
grub-install --root-directory=/tmp/boot --no-floppy /dev/sdb


注意:上面/dev/sdb是我的u盘,在linux系统里的盘符吧,那个/dev/sdb3为什么是“3”。这个因不同的实际情况而不同吧。
总的来说,/dev/sdb就是我的u盘的名字,/dev/sdb3就是我的u盘的一个分区。




④这个时候。你会发现,你的u盘。已经多了一个boot文件夹,里面有一些内容,这个时候,boot文件夹的路径是/dev/sdb3/boot/。
⑤把“/boot/grub/grub.conf”和“/boot/grub/splash.xpm.gz”。拷贝到“/dev/sdb3/boot/grub/”以下(也就是“u盘/boot/grub/”)。
⑥然后把/dev/sdb3/boot/grub/grub.conf改动成以下内容:
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You do not have a /boot partition.  This means that
         all kernel and initrd paths are relative to /, eg.
         root (hd0,0)
         kernel /boot/vmlinuz-version ro root=/dev/sda1
         initrd /boot/initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz


title Red Hat Enterprise Linux Server 1280*1024(3.4.0)
root (hd0,0)
kernel /boot/vmlinuz-3.4.0  root=/dev/sda1 selinux=0 init=/sbin/init vga=795 fb:on
initrd /boot/initrd.img-3.4.0


title Red Hat Enterprise Linux Server 1600*1200(3.4.0)
root (hd0,0)
kernel /boot/vmlinuz-3.4.0  root=/dev/sda1 selinux=0 init=/sbin/init vga=858 fb:on
initrd /boot/initrd.img-3.4.0



⑦重新启动计算机。在BIOS里。设置为从u盘启动,就可以。

 


说明:grub2.0以后支持的是grub.cfg
set default=0
set timeout=5
set gfxmode=1280x1024
menuentry 'Red Hat Enterprise Linux Server 1280*1024(3.4.0)' {
       set gfxpayload=1280x1024x32,1024x768x32,800x600x32,800x600x16,800x600,640x480
	linux /boot/vmlinuz-3.4.0  root=/dev/sda1 selinux=0 init=/sbin/init vga=795 
	initrd /boot/initrd.img-3.4.0
}


制作步骤:


先用cfdisk 在U盘中,创建个两个分区,第二个一会儿作为boot分区。


0:mkfs.ext3 /dev/sdc5
1:mount /dev/sdc5 /tmp/boot
2:grub-install --root-directory=/tmp/boot --no-floppy  /dev/sdc(*注意*)


自己改动一下menu.lst文件吧。这个简单。

****************************************************************************
以上所有推翻重写。

须要懂得理论上的知识,才干做出正确的结果。

****************************************************************************

首先理解引导中U盘引导部分的过程。
1:当BISO通电引导到磁盘时。会检查MBR区域(MBR:446+DPT:64+ENDFLAG:2=512)
MBR里面必须存在引导程序,一般会是我们熟悉grub,dos,95dos引导等。否则无法引导。
这里做个简要说明:寻常我们将系统安装硬盘上,通常安装都是安装windows,再安装linux,而通常当安装linux时。会提示你是否安装到MBR里面,或者安装到你的某个磁盘分区里面,一般是你的/boot的所在分区。


安装在MBR,或者某个磁盘分区。这2者之间存在区别。
a:当安装到MBR时。会将已经存在的Win自己在MBR的引导程序替代,变成GRUB引导。
b:当安装到某个分区时。会由存在的Win的引导程序,引导到那个分区的Grub,再有Grub引导到各个操作系统。
这就是2者的区别。

win没有为其他系统考虑过。到了自己的磁盘分区之后。对于引导其他系统,没有提供能够直接引导的命令。仅仅能进入后改动它的boot.ini文件。而grub提供命令命令菜单,能够进行手工引导。



2:grub的制作
笔者发现win仅仅能自己主动识别出U盘的第一个分区,而对于第二个分区无动于衷。

所以做了这种分区方案。

第一个分区用来存储数据。第二个分区用来放置引导信息,这样不至于在使用过程中。自己创建的grub的boot分区,被别人勿删除,或者格式化。

我的1G U盘分区例如以下:


/dev/sdc1  950M   ntfs                                                      用于平时的数据存储
/dev/sdc5  60M     ext3   (boot标识,能够引导启动。用cfdisk时。非常easy改动。

) 用于存储引导文件。


运行命令
mount /dev/sdc5 /tmp/boot
grub-install --root-directory=/tmp/boot --no-floppy  /dev/sdc
此处脚下留神。必须讲grub安装到/dev/sdc,否则眼下对于U盘的MBR来讲。没有能够用的引导程序。
此命令运行后,会在/tmp/boot/文件夹。也就是/dev/sdc5,以下产生文件夹/boot/grub.里面有*stage*等文件。

最后在/boot/grub 文件夹中建立menu.lst文件。

文件内容例如以下:

timeout         20
default         0

title windows xp
map (hd0) (hd1)
map (hd1) (hd0)
rootnoverify (hd1,0)
makeactive
chainloader +1


讲一下。假设你用U盘引导后,他会把自己标记为hd0。

而已经安装在硬盘上的win操作系统觉得自己的宿主硬盘是hd0。此时引导win操作系统时就会造成盘符错位。

因此我们须要用map进行一下映射转换。

map 的解释及使用例如以下:
map TO_DRIVE   FROM_DRIVE

映射 驱动器FROM_DRIVE 到TO_DRIVE.

当你链式引导向dos一样的操作系统。而且该系统没有在第一个驱动器上时,必须进行该映射。

 

 

that's Ok!



****************************************************************************
随着我的使用的愈加频繁。menu.lst上面的内容多了起来。

****************************************************************************


以下分享一下。里面的内容。首先是文件夹结构。


root@kook:~# fdisk -l /dev/sdb

Disk /dev/sdb: 1010 MB, 1010826752 bytes
32 heads, 61 sectors/track, 1011 cylinders
Units = cylinders of 1952 * 512 = 999424 bytes

  Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         951      928145+   6  FAT16
/dev/sdb2   *         952        1011       58560   83  Linux
root@kook:~# mount /dev/sdb2 /tmp/boot/
root@kook:~# tree -d /tmp/boot
/tmp/boot
|-- boot
|   `-- grub
|-- centos4.4
|-- centos4.4-64
|-- lost+found
`-- ubuntu704

6 directories



以下是menu.lst的内容。


root@kook:~# cat /tmp/boot/boot/grub/menu.lst
color light-gray/blue
timeout         20
default         0

title           Windows 95/98/NT/2000
map             (hd0) (hd1)
map             (hd1) (hd0)
rootnoverify    (hd1,0)
makeactive
chainloader     +1

title           CentOS 4.4 x86_64 Net Install
root            (hd0,1)
kernel          /centos4.4-64/vmlinuz root=/dev/hda2 ro single
initrd          /centos4.4-64/initrd.img
boot

title           CentOS 4.4 i386 Net Install
root            (hd0,1)
kernel          /centos4.4/vmlinuz root=/dev/hda2 ro single
initrd          /centos4.4/initrd.img
boot

title           Ubuntu 7.04 AMD 64 Net Install
root            (hd0,1)
kernel          /ubuntu704/linux root=/dev/hda2 ro single
initrd          /ubuntu704/initrd.gz
boot

 

 

 

注意:
1:成功的关键是MBR的内容。能够用dd if=/dev/sdc of=/tmp/mbr.bin bs=446 count=1 提取 出。

hexdump -C/tmp/mbr.bin 查看
2:有时候MBR会有问题。

用这个命令清零吧。

dd if=/dev/zero of=/dev/sdc bs=446 count=1 

由于一个是0.97版(grub-legacy),一个是2.02版(grub2)
grub-legacy 没有 /boot/grub/grub.cfg 这个文件,而是使用 menu.lst 文件。


root (hd0,0)
kernel /boot/vmlinuz-3.10xxx
initrd /boot/initrd-3.10xxx
boot
这个格式是grub格式。或者grub一代


root (hd0,msdos0)
linux /boot/vmlinuz-3.10xxx
initrd /boot/initrd-3.10xxx
boot
这是grub2的写法

rhel6曾经都採用grub一代,到rhel7则採用grub2模块化引导管理器

grub和grub2的异同还请參考官方文档或自行百度!
我的笔记:

 

系    统:虚拟机RHEL6.4(2.6.32)

裁剪内核:linux-2.6.34

 

在虚拟机+虚拟硬盘、945主板+USB,均引导内核成功,系统未作复杂裁剪,未作压缩镜像

 

1.      linux内核编译:

详细步骤:
# tar zxvf linux-2.6.tar.gz -C /usr/src
# cd /usr/src/linux2.6
# make menuconfig


# make
# make modules_install
# cp arch/x86/boot/bzImage /boot/vmlinuz-2.6

# make install

对照/boot/grub/grub.cfg文件的改动

2.      安装启动盘(U盘、硬盘)

# Fdisk /dev/sdb 

#mkfs.ext2 /dev/sdb1

# mkdir /tmp/boot ; mount /dev/sdb1/tmp/boot

# grub-install--root-directory=/tmp/boot --no-floppy /dev/sdb

# cp /boot/grub/grub.conf /tmp/boot/boot/grub/

# cp /boot/grub//boot/grub/splash.xpm.gz /tmp/boot/boot/grub/

# cp /boot/vmlinuz-2.6.34/mnt/boot/vmlinuz

# cp /boot/initramfs-2.6.34.img/mnt/boot/initramfs.img

3.  构建系统文件夹

# mkdir dev proc etc sbin bin lib mntusr

# cp /bin /tmp/boot/

# cp –dpr /dev/{console,fd0.hda,hda8,hda9,hda10,initctl,initrd,kmem,mem,null,ram,ram0,ramdisj,sda,tty1,tty} /tmp/boot/

# cp/etc/{default,ld.so.cache,ld.so.conf,login.defs,fstab,groub,init.d,inittab,issue,modules.conf,mtab,nsswitch.conf,pam.d,profile,rc.d} /tmp/boot/

4.  编辑grub.conf

Default为默认启动项

Grub2 grub.cfg

set default=0

set timeout=5

set gfxmode=1280x1024

menuentry 'Red Hat EnterpriseLinux Server 1280*1024(3.4.0)' {

       setgfxpayload=1280x1024x32,1024x768x32,800x600x32,800x600x16,800x600,640x480

    linux/boot/vmlinuz-3.4.0  root=/dev/sda1selinux=0 init=/sbin/init vga=795

    initrd/boot/initrd.img-3.4.0

}

 

 

5. 加入必要的命令

 比如:ls

 Ldd ls,加入相应的依赖库文件;ldd ls > 1.txt

 

6. 改动initrd

又一次编译内核后,可能加入了自己定义的模块。就有可能须要改动init文件。而init文件就在initrd中,这里记录下操作步骤,以防遗忘。

 

1.  cp  /boot/initrd-3.2.img /tmp/mylinux/initrd-3.2.img.gz

 

    这里之所以进行改名。是由于initrd-3.2.img是经过gzip压缩过的,所以须要对其解压,可是gzip对解压的文件的文件后缀名又有要求。所以就先进行改名。

 

2.   gunzip  initrd-3.2.9.img.gz

 

3.   cpio  -id  < initrd-3.2.9.img

 

经过以上三步,就在当前文件夹下解压了initrd文件。从而得到了init文件。

依据自己的需求改动init文件后。通过以下命令又一次生成initrd文件。

 

4.   find  .  | cpio  -H  newc  -o  |  gzip  -9  > /boot/initrd-3.2.9.img

find . | cpio -H newc -o | gzip -9>../initrd.img-3.4.0

 

 

7. 内核加入fb0

mknod /dev/fb0 c 29 0

Device Drivers --->

Graphics support --->

<*> Support for frame buffer devices--->

       

318     31b    35a  

1024    1280   1600

8. 加入网络驱动

# initrd,img/init

echo "0: linuxrunning on initrd.img"

echo "1: linuxrunning on usb ext2 filesystem"

 

read -p "select:" data

if [ $data ="0" ]; then

    echo "0: linux running oninitrd.img  selected"

    uname -r

 insmod lib/modules/3.4.0/kernel/drivers/net/mii.ko

 insmod lib/modules/3.4.0/kernel/drivers/net/ethernet/realtek/r8169.ko#加入网络

 insmod lib//modules/3.4.0/kernel/drivers/usb/storage/usb-storage.ko#加入USB

sleep 1

 mkdir usb

 mount /dev/sda1usb

 ifconfig -a

 ifconfig eth0192.168.1.88

 

  sleep 1

sh

   

else

    echo "1: linux running on usb ext2filesystem  selected"

    sleep 1

fi


posted @ 2017-08-20 15:41  yfceshi  阅读(18002)  评论(1编辑  收藏  举报