ubuntu小技巧6--如何修复Ubuntu系统引导项

        当我们安装双系统的时候,若先安装windows后安装Ubuntu,安装后则会出现一个默认的系统选择界面,如图1,但是先装Ubuntu后装Windows或者双系统装好了又重装windows的时候,就会出现Ubuntu引导丢失的情况,此时可以如下恢复启动界面。

图1

ubuntu小技巧6--如何修复Ubuntu系统引导项_Ubuntn修复启动项

1、  进入Ubuntu启动盘,获取root权限,察看磁盘各个区的内容

sudo -i

get root privilege

如图2,3

图2

ubuntu小技巧6--如何修复Ubuntu系统引导项_Ubuntn修复启动项_02

图3

ubuntu小技巧6--如何修复Ubuntu系统引导项_双系统设置启动项_03

3 挂载相关区,重新生成引导文件

mount /dev/sda7  /mnt

mount /dev/sda5  /mnt/boot (no boot,skip it)

grub-install --root-directory=/mnt/  /dev/sda


if ok,it says "installationfinished.No error reported."

 

以上即将/(根目录,2中fdisk –l命令可以查看各个区安装什么内容)挂载在/mnt下面,将/boot目录挂载在/mnt/boot处,然后grub-install生成引导文件

如图4,5

图4将grub-install --root-directory=/mnt/ /dev/sda

打错了,图5重新输入正确后,修复引导成功

图4

ubuntu小技巧6--如何修复Ubuntu系统引导项_Ubuntn修复启动项_04

图5

 

ubuntu小技巧6--如何修复Ubuntu系统引导项_双系统设置启动项_05

4 重启电脑,进入ubuntu再次修复引导项,就会出现图1的界面

restart PC,it will show grub interface

enter ubuntu OS,then update-grub

as picture 6,it's finished!

restart PC,you can see familiar options

重启电脑,启动项完全恢复了!

图6

ubuntu小技巧6--如何修复Ubuntu系统引导项_linux_06

 

经过1、2、3就出现了一个ubuntu和一个windows引导项,但是windows选项还不太完美,enter进入windows后会提示按任意键进入系统,这是1、2、3之后不完美的地方,当然若多次恢复或还原windows后,可能会改变windows的引导位置此时生成的windows引导可能无法进入windows系统,因此需要使用update-grub来修复一下。

 

5 删除多余的启动项

if you want to delete some options,youcan edit /boot/grub/grub.cfg

and delete options you don't need 

 

具体删除的地方如配置文件所示,删除之前最好备份一下原诗grub.cfg文件

删除后就又剩下两个必须的启动项了,如下图所示:

ubuntu小技巧6--如何修复Ubuntu系统引导项_Ubuntu启动项_07

 

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"
fi

if [ x"${feature_menuentry_id}" = xy ]; then
menuentry_id_option="--id"
else
menuentry_id_option=""
fi

export menuentry_id_option

if [ "${prev_saved_entry}" ]; then
set saved_entry="${prev_saved_entry}"
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
fi

function savedefault {
if [ -z "${boot_once}" ]; then
saved_entry="${chosen}"
save_env saved_entry
fi
}
function recordfail {
set recordfail=1
if [ -n "${have_grubenv}" ]; then if [ -z "${boot_once}" ]; then save_env recordfail; fi; fi
}
function load_video {
if [ x$feature_all_video_module = xy ]; then
insmod all_video
else
insmod efi_gop
insmod efi_uga
insmod ieee1275_fb
insmod vbe
insmod vga
insmod video_bochs
insmod video_cirrus
fi
}

if [ x$feature_default_font_path = xy ] ; then
font=unicode
else
insmod part_msdos
insmod ext2
set root='hd0,msdos7'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos7 --hint-efi=hd0,msdos7 --hint-baremetal=ahci0,msdos7 fd063068-eb9a-4314-9b62-5caa732cc77a
else
search --no-floppy --fs-uuid --set=root fd063068-eb9a-4314-9b62-5caa732cc77a
fi
font="/usr/share/grub/unicode.pf2"
fi

if loadfont $font ; then
set gfxmode=auto
load_video
insmod gfxterm
set locale_dir=$prefix/locale
set lang=en_US
insmod gettext
fi
terminal_output gfxterm
if [ "${recordfail}" = 1 ] ; then
set timeout=30
else
if [ x$feature_timeout_style = xy ] ; then
set timeout_style=menu
set timeout=10
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
set timeout=10
fi
fi
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/05_debian_theme ###
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
if background_color 44,0,30,0; then
clear
fi
### END /etc/grub.d/05_debian_theme ###

### BEGIN /etc/grub.d/10_linux ###
function gfxmode {
set gfxpayload="${1}"
if [ "${1}" = "keep" ]; then
set vt_handoff=vt.handoff=7
else
set vt_handoff=
fi
}
if [ "${recordfail}" != 1 ]; then
if [ -e ${prefix}/gfxblacklist.txt ]; then
if hwmatch ${prefix}/gfxblacklist.txt 3; then
if [ ${match} = 0 ]; then
set linux_gfx_mode=keep
else
set linux_gfx_mode=text
fi
else
set linux_gfx_mode=text
fi
else
set linux_gfx_mode=keep
fi
else
set linux_gfx_mode=text
fi
export linux_gfx_mode
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-fd063068-eb9a-4314-9b62-5caa732cc77a' {
recordfail
load_video
gfxmode $linux_gfx_mode
insmod gzio
insmod part_msdos
insmod ext2
set root='hd0,msdos5'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos5 --hint-efi=hd0,msdos5 --hint-baremetal=ahci0,msdos5 c865eff9-82bb-4ddc-8076-2cebdc5eec7b
else
search --no-floppy --fs-uuid --set=root c865eff9-82bb-4ddc-8076-2cebdc5eec7b
fi
linux /vmlinuz-4.4.0-31-generic root=UUID=fd063068-eb9a-4314-9b62-5caa732cc77a ro quiet splash $vt_handoff
initrd /initrd.img-4.4.0-31-generic
}
#删除1
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-fd063068-eb9a-4314-9b62-5caa732cc77a' {
menuentry 'Ubuntu, with Linux 4.4.0-31-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.4.0-31-generic-advanced-fd063068-eb9a-4314-9b62-5caa732cc77a' {
recordfail
load_video
gfxmode $linux_gfx_mode
insmod gzio
insmod part_msdos
insmod ext2
set root='hd0,msdos5'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos5 --hint-efi=hd0,msdos5 --hint-baremetal=ahci0,msdos5 c865eff9-82bb-4ddc-8076-2cebdc5eec7b
else
search --no-floppy --fs-uuid --set=root c865eff9-82bb-4ddc-8076-2cebdc5eec7b
fi
echo 'Loading Linux 4.4.0-31-generic ...'
linux /vmlinuz-4.4.0-31-generic root=UUID=fd063068-eb9a-4314-9b62-5caa732cc77a ro quiet splash $vt_handoff
echo 'Loading initial ramdisk ...'
initrd /initrd.img-4.4.0-31-generic
}
menuentry 'Ubuntu, with Linux 4.4.0-31-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.4.0-31-generic-recovery-fd063068-eb9a-4314-9b62-5caa732cc77a' {
recordfail
load_video
insmod gzio
insmod part_msdos
insmod ext2
set root='hd0,msdos5'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos5 --hint-efi=hd0,msdos5 --hint-baremetal=ahci0,msdos5 c865eff9-82bb-4ddc-8076-2cebdc5eec7b
else
search --no-floppy --fs-uuid --set=root c865eff9-82bb-4ddc-8076-2cebdc5eec7b
fi
echo 'Loading Linux 4.4.0-31-generic ...'
linux /vmlinuz-4.4.0-31-generic root=UUID=fd063068-eb9a-4314-9b62-5caa732cc77a ro recovery nomodeset
echo 'Loading initial ramdisk ...'
initrd /initrd.img-4.4.0-31-generic
}
}
#删除1 结束
### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/20_linux_xen ###

### END /etc/grub.d/20_linux_xen ###

### BEGIN /etc/grub.d/20_memtest86+ ###
#删除2
menuentry 'Memory test (memtest86+)' {
insmod part_msdos
insmod ext2
set root='hd0,msdos5'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos5 --hint-efi=hd0,msdos5 --hint-baremetal=ahci0,msdos5 c865eff9-82bb-4ddc-8076-2cebdc5eec7b
else
search --no-floppy --fs-uuid --set=root c865eff9-82bb-4ddc-8076-2cebdc5eec7b
fi
knetbsd /memtest86+.elf
}
menuentry 'Memory test (memtest86+, serial console 115200)' {
insmod part_msdos
insmod ext2
set root='hd0,msdos5'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos5 --hint-efi=hd0,msdos5 --hint-baremetal=ahci0,msdos5 c865eff9-82bb-4ddc-8076-2cebdc5eec7b
else
search --no-floppy --fs-uuid --set=root c865eff9-82bb-4ddc-8076-2cebdc5eec7b
fi
linux16 /memtest86+.bin console=ttyS0,115200n8
}
#删除2 结束
### END /etc/grub.d/20_memtest86+ ###

### BEGIN /etc/grub.d/30_os-prober ###
menuentry 'Windows 7 (loader) (on /dev/sda2)' --class windows --class os $menuentry_id_option 'osprober-chain-449E2C939E2C8012' {
insmod part_msdos
insmod ntfs
set root='hd0,msdos2'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos2 --hint-efi=hd0,msdos2 --hint-baremetal=ahci0,msdos2 449E2C939E2C8012
else
search --no-floppy --fs-uuid --set=root 449E2C939E2C8012
fi
parttool ${root} hidden-
chainloader +1
}
set timeout_style=menu
if [ "${timeout}" = 0 ]; then
set timeout=10
fi
### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/30_uefi-firmware ###
### END /etc/grub.d/30_uefi-firmware ###

### BEGIN /etc/grub.d/40_custom ###
# 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.
### END /etc/grub.d/40_custom ###

### BEGIN /etc/grub.d/41_custom ###
if [ -f ${config_directory}/custom.cfg ]; then
source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f $prefix/custom.cfg ]; then
source $prefix/custom.cfg;
fi
### END /etc/grub.d/41_custom ###


注意:

若安装双系统的时候无法进入linux只能进入windows系统,则可以用以上方法恢复系统引导,也可以使用easybcd工具来引导系统;

若只能进入linux无法显示windows,则可以使用相关启动盘(老毛桃、大白菜等)修复启动项,修复后就只有windows启动了,然后使用以上方法修复linux启动项即可


以上是修复Ubuntu系统启动项的一点小心得,写在此处以供学习!