溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

學(xué)習(xí)筆記之grub應(yīng)用

發(fā)布時(shí)間:2020-08-11 18:09:52 來源:網(wǎng)絡(luò) 閱讀:268 作者:KIWI譚龍 欄目:安全技術(shù)

GRUB(Boot Loader):基本應(yīng)用

grub: GRand Unified Bootloader

   版本:grub 0.x: grub legacy傳統(tǒng)grub版本

 grub 1.x: grub2

以centos6為例;

grub是安裝在MBR中的

 grub legacy:

    stage1: mbr

    stage1_5: mbr之后的扇區(qū)中,讓stage1中的bootloader能識別stage2所在的分區(qū)上的文件系統(tǒng);

    stage2:存放于磁盤分區(qū)(/boot/grub/)


grub的配置文件:/boot/grub/grub.conf <-- /etc/grub.conf

當(dāng)系統(tǒng)啟動(dòng)時(shí),如果要加載grub所在的磁盤時(shí),會(huì)讀取磁盤上的MBR,從此能加載到stage1,stage1加載以后,它會(huì)嘗試去讀取隨后扇區(qū)的stage1_5,從而之后會(huì)讀到驅(qū)動(dòng)stage2的磁盤分區(qū)了。

stage2及內(nèi)核等通常放置于一個(gè)基本磁盤分區(qū);

grub(或stage2)功用:

(1) 提供菜單、并提供交互式接口

e: 編輯模式,用于編輯菜單;

c: 命令模式,交互式接口;

(2) 加載用戶選擇的內(nèi)核或操作系統(tǒng)

允許傳遞參數(shù)給內(nèi)核

可隱藏此菜單

(3) 為菜單提供了保護(hù)機(jī)制

為編輯菜單進(jìn)行認(rèn)證

為啟用內(nèi)核或操作系統(tǒng)進(jìn)行認(rèn)證


grub如何識別設(shè)備:

   (hd#,#)

hd#: 磁盤編號,用數(shù)字表示;從0開始編號

#: 分區(qū)編號,用數(shù)字表示; 從0開始編號


(hd0,0)


開機(jī)時(shí)的一個(gè)交互界面,敲c可以進(jìn)入grub命令行模式

grub的命令行接口中的常用命令介紹

help: 獲取幫助列表

help KEYWORD: 詳細(xì)幫助信息

find (hd#,#)/PATH/TO/SOMEFILE:(在磁盤)查找某文件

root (hd#,#):把哪一個(gè)磁盤分區(qū)設(shè)置為grub的根設(shè)備

kernel /PATH/TO/KERNEL_FILE: 設(shè)定本次啟動(dòng)時(shí)用到的內(nèi)核文件;額外還可以添加許多內(nèi)核支持使用的cmdline參數(shù);

例如:init=/path/to/init, selinux=0

initrd /PATH/TO/INITRAMFS_FILE: 設(shè)定為選定的內(nèi)核提供額外文件的ramdisk;

boot: 引導(dǎo)啟動(dòng)選定的內(nèi)核;


手動(dòng)在grub命令行接口啟動(dòng)系統(tǒng):

grub> root (hd#,#)

grub> kernel /vmlinuz-VERSION-RELEASE ro root=/dev/DEVICE (指定內(nèi)核版本和指定根所在的目錄,這里必須注意root必須是你根所在的設(shè)備的分區(qū)的文件,可以通過df命令查看)

grub> initrd /initramfs-VERSION-RELEASE.img

grub> boot


配置文件:/boot/grub/grub.conf

    配置項(xiàng):

default=#: 設(shè)定默認(rèn)啟動(dòng)的菜單項(xiàng);菜單項(xiàng)(title)編號從0開始;

timeout=#:指定菜單項(xiàng)等待選項(xiàng)選擇的時(shí)長;(如果用戶選擇時(shí)間超過5秒就自己動(dòng)選擇了)

splashp_w_picpath=(hd#,#)/PATH/TO/XPM_PIC_FILE:指明菜單背景圖片文件路徑;

hiddenmenu:隱藏菜單;

password [--md5] STRING: 菜單編輯認(rèn)證;

title TITLE:定義菜單項(xiàng)“標(biāo)題”, 可出現(xiàn)多次;

root (hd#,#):grub查找stage2及kernel文件所在設(shè)備分區(qū);為grub的“根”; 

kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]:啟動(dòng)的內(nèi)核

initrd /PATH/TO/INITRAMFS_FILE: 內(nèi)核匹配的ramfs文件;

password [--md5] STRING: 啟動(dòng)選定的內(nèi)核或操作系統(tǒng)時(shí)進(jìn)行認(rèn)證;


若在開機(jī)時(shí)需要認(rèn)證使用,可用下面的命令幫助我們生成密碼串

  grub-md5-crypt命令:幫我們生成對應(yīng)要保護(hù)的密碼串


  進(jìn)入單用戶模式步驟:

(1) 編輯grub菜單(選定要編輯的title,而后使用e命令); 

(2) 移動(dòng)菜單到kernel項(xiàng),在選定的kernel后附加

1, s, S或single都可以;之后回車回到菜單項(xiàng)

(3) 在kernel所在行,鍵入(點(diǎn)擊)“b命令”;

   系統(tǒng)損壞之后,在沒有重啟的時(shí)候可以按照下面的方法來修復(fù)

安裝grub:

(1) grub-install:安裝grub命令grub-install - install GRUB on your drive

grub-install [OPTION] install_device

grub-install --root-directory=ROOT /dev/DISK

系統(tǒng)沒重啟修復(fù)使用下面的:

(2) grub

grub> root (hd#,#)###此方法必須實(shí)現(xiàn)存在有一個(gè)grub目錄

        grub> setup (hd#)

創(chuàng)建一個(gè)磁盤,提供一個(gè)可以單獨(dú)運(yùn)行bash的系統(tǒng)

1、首先添加一塊磁盤,并分區(qū)創(chuàng)建文件系統(tǒng),這里設(shè)置為/dev/sdb1(做boot和安裝grub用),/dev/sdb2(作為swap分區(qū)),/dev/sdb3(放根)。

2、安裝grub

[root@localhost ~]# mkdir /mnt/boot

[root@localhost ~]# mount /dev/sdb1 /mnt/boot

[root@localhost ~]# ls /mnt/boot

lost+found

[root@localhost ~]# grub-install --root-directory=/mnt /dev/sdb

##安裝grub并指明根所在目錄

Probing devices to guess BIOS drives. This may take a long time.

Installation finished. No error reported.

This is the contents of the device map /mnt/boot/grub/device.map.

Check if this is correct or not. If any of the lines is incorrect,

fix it and re-run the script `grub-install'.


(fd0) /dev/fd0

(hd0) /dev/sda

(hd1) /dev/sdb

[root@localhost ~]# ls /mnt

boot  sysroot

[root@localhost ~]# ls /mnt/boot

grub  lost+found

###顯示已經(jīng)成功安裝grub

[root@localhost ~]# ls /mnt/boot/grub/

device.map     iso9660_stage1_5   stage1           xfs_stage1_5

e2fs_stage1_5  jfs_stage1_5       stage2

fat_stage1_5   minix_stage1_5     ufs2_stage1_5

ffs_stage1_5   reiserfs_stage1_5  vstafs_stage1_5

[root@localhost ~]# cp /boot/vmlinuz-2.6.32-573.el6.x86_64 /mnt/boot/vmlinuz

###復(fù)制內(nèi)核

[root@localhost ~]# cp /boot/initramfs-2.6.32-573.el6.x86_64.img /mnt/boot/initramfs.img

[root@localhost ~]# vim /mnt/boot/grub/grub.conf

     default=0

     timeout=5

     title Centos (express)

            root (hd0,0)##此處在新建的系統(tǒng)中識別為第一塊磁盤的第一分區(qū)

            kernel /vmlinuz ro root=/dev/sda3

       ###此處也一樣用的是sda3而非sdb3,對新系統(tǒng)來說它就是磁盤的第三分區(qū)

           initrd /initramfs.img

#####編輯grub配置文件

##以上為grub配置階段,下面為創(chuàng)建根文件


[root@localhost ~]# mkdir /mnt/sysroot

[root@localhost ~]# ls /mnt

boot  sysroot

[root@localhost ~]# mount /dev/sdb3 /mnt/sysroot

#####掛載分區(qū)sdb3,將它作為根文件放的位置

[root@localhost ~]# ls /mnt/sysroot

lost+found

[root@localhost ~]# cd /mnt/sysroot

[root@localhost sysroot]# ls

lost+found

[root@localhost sysroot]# mkdir -pv etc bin sbin lib lib64 dev proc sys tmp var usr home root mnt media 

##給根下創(chuàng)建必要的目錄

mkdir: created directory `etc'

mkdir: created directory `bin'

mkdir: created directory `sbin'

mkdir: created directory `lib'

mkdir: created directory `lib64'

mkdir: created directory `dev'

mkdir: created directory `proc'

mkdir: created directory `sys'

mkdir: created directory `tmp'

mkdir: created directory `var'

mkdir: created directory `usr'

mkdir: created directory `home'

mkdir: created directory `root'

mkdir: created directory `mnt'

mkdir: created directory `media'

[root@localhost sysroot]# ls

bin  etc   lib    lost+found  mnt   root  sys  usr

dev  home  lib64  media       proc  sbin  tmp  var

###此時(shí)文件中沒有程序,下面我們將bash復(fù)制進(jìn)來作為init來啟動(dòng)

[root@localhost sysroot]# cp /bin/bash /mnt/sysroot/bin/

[root@localhost sysroot]# ldd /bin/bash

linux-vdso.so.1 =>  (0x00007ffcb40d4000)

libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003d61800000)

libdl.so.2 => /lib64/libdl.so.2 (0x0000003d5ac00000)

libc.so.6 => /lib64/libc.so.6 (0x0000003d5b000000)

[root@localhost sysroot]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64

[root@localhost sysroot]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64

[root@localhost sysroot]# cp /lib64/libc.so.6 /mnt/sysroot/lib64

[root@localhost sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64

####要將bash所依賴的都復(fù)制到相應(yīng)目錄

[root@localhost sysroot]# chroot /mnt/sysroot

bash-4.1# 

###檢測根系統(tǒng)

##配置根已經(jīng)成功

一個(gè)簡單的系統(tǒng)就配置完成了,在之后創(chuàng)建一個(gè)虛擬機(jī),將該磁盤裝載上面就可以了,但在同時(shí)使用手動(dòng)啟動(dòng)的時(shí)候,要在grub菜單項(xiàng)中編輯kernel選項(xiàng)在init前面添加selinux=0之后回車退回菜單項(xiàng)敲擊“b命令”就可以啟動(dòng)了。

注:這里面沒有任何命令,要使用命令則需另外復(fù)制相關(guān)文件到系統(tǒng)中



 


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI