以前的 GRUB 很簡單,透過修改 /boot/grub/grub.conf 或是 /boot/grub/menu.lst ( https://benjr.tw/134 )就可以搞定,但是 GRUB2 (版本為 1.98 或更新版) 變得很不一樣.
與 GRUB2 相關的幾個目錄為
- /boot/grub/
- /etc/default/
- /etc/grub.d/
下面針對 GRUB2 幾個重要的檔案做說明.
/boot/grub/grub.cfg
如果你去手動編輯 /boot/grub/grub.cfg 會發現怎麼變成 shell script ,不知道要怎麼修改, GRUB2 也不建議修改這個檔案.建議修改 /etc/default/grub 檔案
root@ben-VirtualBox:~# cat /boot/grub/grub.cfg # # DO NOT EDIT THIS FILE # # It is automatically generated by grub-mkconfig using templates # from /etc/grub.d and settings from /etc/default/grub # ### BEGIN /etc/grub.d/00_header ### if [ -s $prefix/grubenv ]; then set have_grubenv=true load_env fi if [ "${next_entry}" ] ; then set default="${next_entry}" set next_entry= save_env next_entry set boot_once=true else set default="0" ....略
/etc/default/grub
先來看看 /etc/default/grub 檔案,下面是 Ubuntu 16.04 的 GRUB2 ,裡面有可以做調整的設定.
root@ben-VirtualBox:~# cat /etc/default/grub # If you change this file, run 'update-grub' afterwards to update # /boot/grub/grub.cfg. # For full documentation of the options in this file, see: # info -f grub -n 'Simple configuration' GRUB_DEFAULT=0 GRUB_HIDDEN_TIMEOUT=0 GRUB_HIDDEN_TIMEOUT_QUIET=true GRUB_TIMEOUT=10 GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian` GRUB_CMDLINE_LINUX_DEFAULT="quiet" GRUB_CMDLINE_LINUX="find_preseed=/preseed.cfg auto noprompt priority=critical locale=en_US" # Uncomment to enable BadRAM filtering, modify to suit your needs # This works with Linux (no patch required) and with any kernel that obtains # the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...) #GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef" # Uncomment to disable graphical terminal (grub-pc only) #GRUB_TERMINAL=console # The resolution used on graphical terminal # note that you can use only modes which your graphic card supports via VBE # you can see them in real GRUB with the command `vbeinfo' #GRUB_GFXMODE=640x480 # Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux #GRUB_DISABLE_LINUX_UUID=true # Uncomment to disable generation of recovery mode menu entries #GRUB_DISABLE_RECOVERY="true" # Uncomment to get a beep at grub start #GRUB_INIT_TUNE="480 440 1"
- GRUB_DEFAULT
設定預設的開機選項,選項可以是數字 (依據 /boot/grub/grub.cfg 中的 menuentry 第一筆選單為 0 ,第二筆則為 1 依此類推下去),完整的選單選項文字 (依據 /boot/grub/grub.cfg 中的 menuentry )或是 saved (可利用 grub-reboot(只有下次開機有效) 和 grub-set-default 指令來設定預設開機的作業系統)root@ben-VirtualBox:~# grub-set-default 1
後面參數代表選單的位置,第一筆是由 0 開始算起.手動修改 /etc/default/grub 的設定也是可以的.
root@ben-VirtualBox:~# vi /etc/default/grub ....略 GRUB_DEFAULT=saved
saved 代表會以上次所選的開機選項為預設選項.
- GRUB_HIDDEN_TIMEOUT
把 GRUB 2 的選單隱藏起來的時間. - GRUB_HIDDEN_TIMEOUT_QUIET
true 螢幕空白不顯示倒數, false 空白螢幕上並顯示一個倒數計時器. - GRUB_TIMEOUT
在 GRUB2 停留的時間,待時間結束後以預設的作業系統選項開機.(在開機期間需 SHIFT 才會顯示選單) - GRUB_DISTRIBUTOR
取得選單選項中的描述名稱 - GRUB_CMDLINE_LINUX_DEFAULT
- GRUB_CMDLINE_LINUX
這邊可以增加額外開機參數.
檔案內任何設定修改後都需要執行 grub-mkconfig 將 /etc/default/grub 修改的內容回寫至 /boot/grub/grub.cfg
root@ben-VirtualBox:~# grub-mkconfig -o /boot/grub/grub.cfg
其他關於詳細設定內容可以直接參考 GRUB2中文指南第二版(上)
/etc/grub.d/40_custom
/etc/grub.d/ 這個目錄下,可以做修改的檔案是 40_custom ,這個檔案是當你要新增一個開機選項時,可以在這裡編寫.
設定檔可以參考 /boot/grub/grub.cfg 裡的 menuentry 定義,我這邊是直接參考鳥哥的網站.
root@ben-VirtualBox:~# vi /etc/grub.d/40_custom #!/bin/sh exec tail -n +3 $0 # This file provides an easy way to add custom menu entries. Simply type the # menu entries you want to add after this comment. Be careful not to change # the 'exec tail' line above. menuentry 'My graphical CentOS, with Linux 3.10.0-229.el7.x86_64' --class rhel fedora --class gnu-linux --class gnu --class os --unrestricted --id 'mygraphical' { load_video set gfxpayload=keep insmod gzio insmod part_gpt insmod xfs set root='hd0,gpt2' if [ x$feature_platform_search_hint = xy ]; then search --no-floppy --fs-uuid --set=root --hint='hd0,gpt2' 94ac5f77-cb8a-495e-a65b-... else search --no-floppy --fs-uuid --set=root 94ac5f77-cb8a-495e-a65b-2ef7442b837c fi linux16 /vmlinuz-3.10.0-229.el7.x86_64 root=/dev/mapper/centos-root ro rd.lvm.lv= centos/root rd.lvm.lv=centos/swap crashkernel=auto rhgb quiet elevator=deadline systemd.unit=graphical.target initrd16 /initramfs-3.10.0-229.el7.x86_64.img }
一開始 GRUB2 需要載入讀取核心檔案所需要的驅動程式 load_video (顯示卡), set gfxpayload=keep (keep the same mode as used in GRUB) , insmod gzio (解壓縮), insmod part_gpt (分割槽), insmod xfs (檔案系統) .
一樣透過指令 grub-mkconfig 將新增的內容回寫至 /boot/grub/grub.cfg
root@ben-VirtualBox:~# grub-mkconfig -o /boot/grub/grub.cfg Generating grub configuration file ... Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported. Found linux image: /boot/vmlinuz-4.4.0-78-generic Found initrd image: /boot/initrd.img-4.4.0-78-generic Found linux image: /boot/vmlinuz-4.4.0-75-generic Found initrd image: /boot/initrd.img-4.4.0-75-generic Found linux image: /boot/vmlinuz-4.4.0-71-generic Found initrd image: /boot/initrd.img-4.4.0-71-generic Found linux image: /boot/vmlinuz-4.4.0-64-generic Found initrd image: /boot/initrd.img-4.4.0-64-generic Found linux image: /boot/vmlinuz-4.4.0-62-generic Found initrd image: /boot/initrd.img-4.4.0-62-generic Found linux image: /boot/vmlinuz-4.4.0-59-generic Found initrd image: /boot/initrd.img-4.4.0-59-generic Found memtest86+ image: /boot/memtest86+.elf Found memtest86+ image: /boot/memtest86+.bin done
檢查一下,確認已新增了剛剛的選項.
root@ben-VirtualBox:~# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /boot/grub/grub.cfg 0 : Ubuntu 1 : Memory test (memtest86+) 2 : Memory test (memtest86+, serial console 115200) 3 : My graphical CentOS, with Linux 3.10.0-229.el7.x86_64
如果不習慣還可以去下載圖形介面的 #grub-customizer ( https://launchpad.net/grub-customizer ) 來使用.
root@ben-VirtualBox:~# add-apt-repository ppa:danielrichter2007/grub-customizer root@ben-VirtualBox:~# apt-get update root@ben-VirtualBox:~# apt-get install grub-customizer root@ben-VirtualBox:~# gksudo grub-customizer
2 thoughts on “Linux – GRUB2”