龙芯3A3000使用debootstrap安装Debian的完整过程,并解决开机进grub的问题

龙芯3A3000使用debootstrap安装Debian的完整过程,并解决开机进grub的问题

绪论

网上完整的3A3000安装系统的很少,而且经过测试不太适合昆仑固件的3A3000使用,于是自己折腾了三天,算是折腾出了个结果。

loongnix或者uos之类的国内定制系统虽然安装简单,但是这些系统不仅软件老旧,除了loongnix外还要收费。

这篇文章将通过debootstrap安装debian stable版本,理论上如果你想装aosc只需要把debootstrap换成aoscbootstrap就行。

以下所有shell操作均以root身份进行

安装步骤

安装Loongnix

这里只用来配置引导,实际上如果你经验丰富也可以不做这一步,手动分区然后直接把LiveCD的/boot魔改一下放进硬盘的/boot也是一样的。

从这里下载ISO:Loongnix20open in new window

下完之后烧录安装,只需要注意两点:

  • 单独分出一个/boot,其他随意
  • 下述所有操作均在U盘的LiveCD中完成,因此装好后不需要重启,不需要进入装好的Loongnix,这一步仅用于配置引导

装完以后修改一下 /etc/apt/sources.list,原来的源用不了了 把 ftp.loongnix.org 替换为 ftp.loongnix.cn,然后解决一下数字签名无效的问题:

apt update
gpg --recv-key A8C7C20CEDF1B817  # 上面弹出的签名
gpg --output loongnix20.gpg --armor --export A8C7C20CEDF1B817
apt-key add loongnix20.gpg
apt update

用Debootstrap安装Debian

chroot前的准备

  1. 挂载根分区并删除分区内全部文件
mount /dev/sda2 /mnt    # 假设sda2为根分区
rm -rf /mnt/*
  1. /usr/sbin加入PATH环境变量,否则大量程序找不到
export PATH=/usr/sbin/:$PATH
  1. 安装debootstrap
apt install debootstrap
  1. 安装基本系统(当前系统中没有对应的keyring,用--no-check-gpg跳过gpg校验)
debootstrap --arch mips64el --no-check-gpg stable /mnt https://mirrors.tuna.tsinghua.edu.cn/debian
  1. 复制Loongnix内核模块到Debian
cp -r /lib/modules /mnt/lib/
  1. 挂载其他目录、设备
mount /dev/sda1 /mnt/deb/boot       #假设sda1为boot分区
mount --bind /sys /mnt/deb/sys
mount --bind /proc /mnt/deb/proc
mount --bind /dev /mnt/deb/dev

chroot配置Debian

  1. 进入目标系统
chroot /mnt /bin/bash
  1. 修改root密码
passwd root
  1. 修改/etc/apt/sources.list,在第一行添加 non-freenon-free-firmware,添加完后如下:
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free non-free-firmware
  1. 安装grub、locales、内核、驱动、sudo、initramfs-tools,注意当前stable内核版本为6.1.0-32-loongson-3,高于6.1.0的内核无法启动
apt install locales grub-common linux-image-loongson-3 linux-headers-loongson-3 linux-libc-dev firmware-linux-free firmware-linux-nonfree sudo initramfs-tools
  1. 修改主机名/etc/hostname为你喜欢的名字,然后把它添加到 /etc/hosts127.0.0.1那一行

  2. 设置fstab

  • 先执行blkid查看UUID,得到类似如下结果:
/dev/sda1: UUID="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" BLOCK_SIZE="4096" TYPE="ext3" PARTUUID="00000000-0000-0000-0000-000000000000"
/dev/sda2: UUID="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="00000000-0000-0000-0000-000000000000"
/dev/sda3: UUID="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" TYPE="swap" PARTUUID="00000000-0000-0000-0000-000000000000"
/dev/sda5: UUID="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="00000000-0000-0000-0000-000000000000"
  • 然后按如下格式配置fstab
# UNCONFIGURED FSTAB FOR BASE SYSTEM
UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX       /       ext4    defaults,noatime        0       1
UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX       /boot   ext4    defaults,noatime        0       2
UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX       /home   ext4    defaults,noatime        0       2
UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX       none    swap    defaults                0       0
  1. 本地化配置

配置一下语言和时间 注意,语言要安装zh-CN.UTF-8和en-US.UTF-8,但是最后默认语言要设置为en-US.UTF-8,否则tty会乱码,后面不用tty了再改中文还来得及

dpkg-reconfigure locales
dpkg-reconfigure tzdata
  1. 配置用户
useradd -m myuser
usermod -s /bin/bash myuser
passwd myuser
usermod -a -G sudo myuser
  1. 修改/etc/initramfs-tools/initramfs.conf只加载需要的模块,否则会因为initrd过大无法启动:

找到一行 MODULES=most 配置,将其改成 MODULES=dep

然后重新创建initramfs

update-initramfs -k all -u

引导配置

主要有两种方法,可以先用快速方法进系统,再折腾grub模板,这样可以以后都可以直接用grub-mkconfig生成配置文件

快速方法
  1. 修改grub配置 /boot/grub.cfg 找到启动项的段落,参考下面的片段修改配置文件(倒数第四行起,注意:vmlinuz和initrd.img的文件名要和你系统中的一致,它们放在/boot下)
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-ieee1275='ieee1275//disk@0,msdos1' --hint-bios=hd0,msdos1 --hint-efi=hd0,msd$
    else
      search --no-floppy --fs-uuid --set=root b6a3a91c-82b0-4ac6-ba62-19a9d4cf63c7
    fi
    echo    'Loading Linux 6.1.0-32-loongson-3 ...'
    linux   /vmlinuz-6.1.0-32-loongson-3 root=UUID=XXXXXXXXX ro rhgb quiet loglevel=0 LANG=en_US.UTF-8
    initrd /initrd.img-6.1.0-32-loongson-3
    boot
  1. 因为grub默认$prefix为(hdX,xxx)/boot,所以要创建硬链接把grub.cfg链接过去,否则grub找不到启动配置,自然就卡在grub界面了

执行完重启就好

ln /boot/grub/grub.cfg /boot/boot/grub.cfg
一劳永逸,修改grub模板

这种方法无论内核怎么修改,都可以使用grub-mkconfig指令直接生成能用的grub.cfg

  1. 修改/etc/grub.d/00_header,在make_timeout ()中添加一行loadfont \$prefix/grub/fonts/unicode.pf2解决字体问题,参考如下代码片段:
...
make_timeout ()
{
    cat << EOF
loadfont \$prefix/grub/fonts/unicode.pf2
if [ "\${recordfail}" = 1 ] ; then
  set timeout=${GRUB_RECORDFAIL_TIMEOUT:-30}
else
EOF
...
  1. 修改/etc/grub.d/10_linux,给启动脚本添加boot指令

参考如下片段

...
  if test -n "${initrd}" ; then
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
    if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; then
      message="$(gettext_printf "Loading initial ramdisk ...")"
      sed "s/^/$submenu_indentation/" << EOF
        echo    '$(echo "$message" | grub_quote)'
EOF
    fi
    initrd_path=
    for i in ${initrd}; do
      initrd_path="${initrd_path} ${rel_dirname}/${i}"
    done
    sed "s/^/$submenu_indentation/" << EOF
        initrd  $(echo $initrd_path)
        boot
EOF
...
  1. 更新grub配置并创建硬链接(因为grub默认$prefix为(hdX,xxx)/boot,找不到启动配置,自然就卡在grub界面了,所以要创建硬链接把grub.cfg链接过去),接下来重启就好
grub-mkconfig -o /boot/grub.cfg
ln /boot/grub/grub.cfg /boot/boot/grub.cfg

开机之后

此时应该会直接进tty,我们把kde装上,重启之后就有GUI能用了

apt install task-kde-desktop

为了避免后续内核更新导致不开机,锁定一下内核版本

apt-mark hold linux-image-6.1.0-32-loongson-3 linux-headers-6.1.0-32-loongson-3

结束!

实验测试

按照惯例跑了下融合怪……好像不太合适,总之确实跑了下融合怪的硬件部分,结果如图,性能可以吊打大多数便宜的小鸡了,后面说不定可以装个qemu然后疯狂超开(

致谢

emm...总之先感谢一下Gemini 2.5 Pro在我折腾grub的时候不离不弃

参考文献

[1] 难得糊涂ha. 龙芯loongnix20源数字签名无效的处理方法[EB/OL]. CSDN博客, 2022-11-22 [2025-05-10]. https://blog.csdn.net/lizi5803/article/details/128051064.

[2] wells. 安装Debian 11到长城龙芯3A2000C平台[EB/OL]. GeekLogic, 2023-09-10 [2025-05-10]. https://geek-logic.com/debian-11-on-loongson/.

[3] 桜風の狐. 龙芯3A4000安装Debian stable[EB/OL]. 博客园, 2022-01-17 [2025-05-10]. https://www.cnblogs.com/weilinfox/p/15813976.html.

[4] LoongArch社区. 龙芯3A2000主板安装系统经验分享[EB/OL]. LoongArch论坛, 2023-07-15 [2025-05-10]. https://bbs.loongarch.org/d/305-3a2000.

[5] 共创社区. 龙芯平台安装AOSC操作系统指南[EB/OL]. 共创社区Wiki, 2023-08-01 [2025-05-10]. https://wiki.chuang.ac.cn/loongson:guide:aosc_installation.

附录

  1. 修改好的00_header
#! /bin/sh
set -e

# grub-mkconfig helper script.
# Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

prefix="/usr"
exec_prefix="/usr"
datarootdir="/usr/share"
grub_lang=`echo $LANG | cut -d . -f 1`
grubdir="`echo "/boot/grub" | sed 's,//*,/,g'`"
quick_boot="0"

export TEXTDOMAIN=grub
export TEXTDOMAINDIR="${datarootdir}/locale"

. "$pkgdatadir/grub-mkconfig_lib"

# Do this as early as possible, since other commands might depend on it.
# (e.g. the `loadfont' command might need lvm or raid modules)
for i in ${GRUB_PRELOAD_MODULES} ; do
  echo "insmod $i"
done

if [ "x${GRUB_DEFAULT}" = "x" ] ; then GRUB_DEFAULT=0 ; fi
if [ "x${GRUB_DEFAULT}" = "xsaved" ] ; then GRUB_DEFAULT='${saved_entry}' ; fi
if [ "x${GRUB_TIMEOUT}" = "x" ] ; then GRUB_TIMEOUT=5 ; fi
if [ "x${GRUB_GFXMODE}" = "x" ] ; then GRUB_GFXMODE=auto ; fi

if [ "x${GRUB_DEFAULT_BUTTON}" = "x" ] ; then GRUB_DEFAULT_BUTTON="$GRUB_DEFAULT" ; fi
if [ "x${GRUB_DEFAULT_BUTTON}" = "xsaved" ] ; then GRUB_DEFAULT_BUTTON='${saved_entry}' ; fi
if [ "x${GRUB_TIMEOUT_BUTTON}" = "x" ] ; then GRUB_TIMEOUT_BUTTON="$GRUB_TIMEOUT" ; fi

cat << EOF
if [ -s \$prefix/grubenv ]; then
  set have_grubenv=true
  load_env
fi
EOF
if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ]; then
    cat <<EOF
if cmostest $GRUB_BUTTON_CMOS_ADDRESS ; then
   set default="${GRUB_DEFAULT_BUTTON}"
elif [ "\${next_entry}" ] ; then
   set default="\${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="${GRUB_DEFAULT}"
fi
EOF
else
    cat <<EOF
if [ "\${next_entry}" ] ; then
   set default="\${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="${GRUB_DEFAULT}"
fi
EOF
fi
cat <<EOF

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
}
EOF

if [ "$quick_boot" = 1 ]; then
    cat <<EOF
function recordfail {
  set recordfail=1
EOF

  check_writable () {
    abstractions="$(grub-probe --target=abstraction "${grubdir}")"
    for abstraction in $abstractions; do
      case "$abstraction" in
        diskfilter | lvm)
          cat <<EOF
  # GRUB lacks write support for $abstraction, so recordfail support is disabled.
EOF
          return 1
          ;;
      esac
    done

    FS="$(grub-probe --target=fs "${grubdir}")"
    case "$FS" in
      btrfs | cpiofs | newc | odc | romfs | squash4 | tarfs | zfs)
        cat <<EOF
  # GRUB lacks write support for $FS, so recordfail support is disabled.
EOF
        return 1
        ;;
    esac

    cat <<EOF
  if [ -n "\${have_grubenv}" ]; then if [ -z "\${boot_once}" ]; then save_env recordfail; fi; fi
EOF
  }

  if ! check_writable; then
    recordfail_broken=1
  fi

  cat <<EOF
}
EOF
fi

cat <<EOF
function load_video {
EOF
if [ -n "${GRUB_VIDEO_BACKEND}" ]; then
    cat <<EOF
  insmod ${GRUB_VIDEO_BACKEND}
EOF
else
# If all_video.mod isn't available load all modules available
# with versions prior to introduction of all_video.mod
cat <<EOF
  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
EOF
fi
cat <<EOF
}

EOF

serial=0;
gfxterm=0;
for x in ${GRUB_TERMINAL_INPUT} ${GRUB_TERMINAL_OUTPUT}; do
    if [ xserial = "x$x" ]; then
        serial=1;
    fi
    if [ xgfxterm = "x$x" ]; then
        gfxterm=1;
    fi
done

if [ "x$serial" = x1 ]; then
    if [ "x${GRUB_SERIAL_COMMAND}" = "x" ] ; then
        grub_warn "$(gettext "Requested serial terminal but GRUB_SERIAL_COMMAND is unspecified. Default parameters will be used.")"
        GRUB_SERIAL_COMMAND=serial
    fi
    echo "${GRUB_SERIAL_COMMAND}"
fi

if [ "x$gfxterm" = x1 ]; then
    if [ -n "$GRUB_FONT" ] ; then
       # Make the font accessible
       prepare_grub_to_access_device `${grub_probe} --target=device "${GRUB_FONT}"`
    cat << EOF
if loadfont `make_system_path_relative_to_its_root "${GRUB_FONT}"` ; then
EOF
    else
        for dir in "${pkgdatadir}" "`echo '/boot/grub' | sed "s,//*,/,g"`" /usr/share/grub ; do
            for basename in unicode unifont ascii; do
                path="${dir}/${basename}.pf2"
                if is_path_readable_by_grub "${path}" > /dev/null ; then
                    font_path="${path}"
                else
                    continue
                fi
                break 2
            done
        done
        if [ -n "${font_path}" ] ; then
    cat << EOF
if [ x\$feature_default_font_path = xy ] ; then
   font=unicode
else
EOF
                # Make the font accessible
                prepare_grub_to_access_device `${grub_probe} --target=device "${font_path}"`
    cat << EOF
    font="`make_system_path_relative_to_its_root "${font_path}"`"
fi

if loadfont \$font ; then
EOF
            else
    cat << EOF
if loadfont unicode ; then
EOF
            fi
        fi

    cat << EOF
  set gfxmode=${GRUB_GFXMODE}
  load_video
  insmod gfxterm
EOF

# Gettext variables and module
if [ "x${LANG}" != "xC" ] &&  [ "x${LANG}" != "x" ]; then
  cat << EOF
  set locale_dir=\$prefix/locale
  set lang=${grub_lang}
  insmod gettext
EOF
fi

cat <<EOF
fi
EOF
fi

case x${GRUB_TERMINAL_INPUT} in
  x)
    # Just use the native terminal
  ;;
  x*)
    cat << EOF
terminal_input ${GRUB_TERMINAL_INPUT}
EOF
  ;;
esac

case x${GRUB_TERMINAL_OUTPUT} in
  x)
    # Just use the native terminal
  ;;
  x*)
    cat << EOF
terminal_output ${GRUB_TERMINAL_OUTPUT}
EOF
  ;;
esac

if [ "x$gfxterm" = x1 ]; then
    if [ "x$GRUB_THEME" != x ] && [ -f "$GRUB_THEME" ] \
        && is_path_readable_by_grub "$GRUB_THEME"; then
        gettext_printf "Found theme: %s\n" "$GRUB_THEME" >&2

        prepare_grub_to_access_device `${grub_probe} --target=device "$GRUB_THEME"`
        cat << EOF
insmod gfxmenu
EOF
        themedir="`dirname "$GRUB_THEME"`"
        for x in "$themedir"/*.pf2 "$themedir"/f/*.pf2; do
            if [ -f "$x" ]; then
                cat << EOF
loadfont (\$root)`make_system_path_relative_to_its_root $x`
EOF
            fi
        done
        if [ x"`echo "$themedir"/*.jpg`" != x"$themedir/*.jpg" ] || [ x"`echo "$themedir"/*.jpeg`" != x"$themedir/*.jpeg" ]; then
            cat << EOF
insmod jpeg
EOF
        fi
        if [ x"`echo "$themedir"/*.png`" != x"$themedir/*.png" ]; then
            cat << EOF
insmod png
EOF
        fi
        if [ x"`echo "$themedir"/*.tga`" != x"$themedir/*.tga" ]; then
            cat << EOF
insmod tga
EOF
        fi
            
        cat << EOF
set theme=(\$root)`make_system_path_relative_to_its_root $GRUB_THEME`
export theme
EOF
    elif [ "x$GRUB_BACKGROUND" != x ] && [ -f "$GRUB_BACKGROUND" ] \
            && is_path_readable_by_grub "$GRUB_BACKGROUND"; then
        gettext_printf "Found background: %s\n" "$GRUB_BACKGROUND" >&2
        case "$GRUB_BACKGROUND" in 
            *.png)         reader=png ;;
            *.tga)         reader=tga ;;
            *.jpg|*.jpeg)  reader=jpeg ;;
            *)             gettext "Unsupported image format" >&2; echo >&2; exit 1 ;;
        esac
        prepare_grub_to_access_device `${grub_probe} --target=device "$GRUB_BACKGROUND"`
        cat << EOF
insmod $reader
background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`
EOF
    fi
fi

make_timeout ()
{
    cat << EOF
loadfont \$prefix/grub/fonts/unicode.pf2
if [ "\${recordfail}" = 1 ] ; then
  set timeout=${GRUB_RECORDFAIL_TIMEOUT:-30}
else
EOF
    if [ "x${3}" != "x" ] ; then
        timeout="${2}"
        style="${3}"
    elif [ "x${1}" != "x" ] && \
         ([ "$quick_boot" = 1 ] || [ "x${1}" != "x0" ]) ; then
        # Handle the deprecated GRUB_HIDDEN_TIMEOUT scheme.
        timeout="${1}"
        if [ "x${2}" != "x0" ] ; then
            grub_warn "$(gettext "Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.")"
        fi
        if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then
            style="hidden"
            verbose=
        else
            style="countdown"
            verbose=" --verbose"
        fi
    else
        # No hidden timeout, so treat as GRUB_TIMEOUT_STYLE=menu
        timeout="${2}"
        style="menu"
    fi
    cat << EOF
  if [ x\$feature_timeout_style = xy ] ; then
    set timeout_style=${style}
    set timeout=${timeout}
EOF
    if [ "x${style}" = "xmenu" ] ; then
        cat << EOF
  # Fallback normal timeout code in case the timeout_style feature is
  # unavailable.
  else
    set timeout=${timeout}
EOF
    else
        cat << EOF
  # Fallback hidden-timeout code in case the timeout_style feature is
  # unavailable.
  elif sleep${verbose} --interruptible ${timeout} ; then
    set timeout=0
EOF
    fi
    cat << EOF
  fi
fi
EOF
if [ "$recordfail_broken" = 1 ]; then
  cat << EOF
if [ \$grub_platform = efi ]; then
  set timeout=${GRUB_RECORDFAIL_TIMEOUT:-30}
  if [ x\$feature_timeout_style = xy ] ; then
    set timeout_style=menu
  fi
fi
EOF
fi
}

if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ]; then
    cat <<EOF
if cmostest $GRUB_BUTTON_CMOS_ADDRESS ; then
EOF
make_timeout "${GRUB_HIDDEN_TIMEOUT_BUTTON}" "${GRUB_TIMEOUT_BUTTON}" "${GRUB_TIMEOUT_STYLE_BUTTON}"
echo else
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}" "${GRUB_TIMEOUT_STYLE}"
echo fi
else
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}" "${GRUB_TIMEOUT_STYLE}"
fi

if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ] && [ "x$GRUB_BUTTON_CMOS_CLEAN" = "xyes" ]; then
    cat <<EOF
cmosclean $GRUB_BUTTON_CMOS_ADDRESS
EOF
fi

# Play an initial tune
if [ "x${GRUB_INIT_TUNE}" != "x" ] ; then
  echo "play ${GRUB_INIT_TUNE}"
fi

if [ "x${GRUB_BADRAM}" != "x" ] ; then
  echo "badram ${GRUB_BADRAM}"
fi
  1. 修改好的10_linux
#! /bin/sh
set -e

# grub-mkconfig helper script.
# Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

prefix="/usr"
exec_prefix="/usr"
datarootdir="/usr/share"
ubuntu_recovery="0"
quiet_boot="0"
quick_boot="0"
gfxpayload_dynamic="0"
vt_handoff="0"

. "$pkgdatadir/grub-mkconfig_lib"

export TEXTDOMAIN=grub
export TEXTDOMAINDIR="${datarootdir}/locale"

CLASS="--class gnu-linux --class gnu --class os"
SUPPORTED_INITS="sysvinit:/lib/sysvinit/init systemd:/lib/systemd/systemd upstart:/sbin/upstart"

if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
  OS=GNU/Linux
else
  case ${GRUB_DISTRIBUTOR} in
    Ubuntu|Kubuntu)
      OS="${GRUB_DISTRIBUTOR}"
      ;;
    *)
      OS="${GRUB_DISTRIBUTOR} GNU/Linux"
      ;;
  esac
  CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1|LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}"
fi

# loop-AES arranges things so that /dev/loop/X can be our root device, but
# the initrds that Linux uses don't like that.
case ${GRUB_DEVICE} in
  /dev/loop/*|/dev/loop[0-9])
    GRUB_DEVICE=`losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
    # We can't cope with devices loop-mounted from files here.
    case ${GRUB_DEVICE} in
      /dev/*) ;;
      *) exit 0 ;;
    esac
  ;;
esac

# Default to disabling partition uuid support to maintian compatibility with
# older kernels.
GRUB_DISABLE_LINUX_PARTUUID=${GRUB_DISABLE_LINUX_PARTUUID-true}

# btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
# and mounting btrfs requires user space scanning, so force UUID in this case.
if ( [ "x${GRUB_DEVICE_UUID}" = "x" ] && [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] ) \
    || ( [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
        && [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ] ) \
    || ( ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
        && ! test -e "/dev/disk/by-partuuid/${GRUB_DEVICE_PARTUUID}" ) \
    || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
  LINUX_ROOT_DEVICE=${GRUB_DEVICE}
elif [ "x${GRUB_DEVICE_UUID}" = "x" ] \
    || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ]; then
  LINUX_ROOT_DEVICE=PARTUUID=${GRUB_DEVICE_PARTUUID}
else
  LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
fi

case x"$GRUB_FS" in
    xbtrfs)
        rootsubvol="`make_system_path_relative_to_its_root /`"
        rootsubvol="${rootsubvol#/}"
        if [ "x${rootsubvol}" != x ]; then
            GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
        fi;;
    xzfs)
        rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
        bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
        LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs%/}"
        ;;
esac

title_correction_code=

if [ -x /lib/recovery-mode/recovery-menu ]; then
    GRUB_CMDLINE_LINUX_RECOVERY=recovery
else
    GRUB_CMDLINE_LINUX_RECOVERY=single
fi
if [ "$ubuntu_recovery" = 1 ]; then
    GRUB_CMDLINE_LINUX_RECOVERY="$GRUB_CMDLINE_LINUX_RECOVERY nomodeset"
fi

if [ "$vt_handoff" = 1 ]; then
  for word in $GRUB_CMDLINE_LINUX_DEFAULT; do
    if [ "$word" = splash ]; then
      GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT \$vt_handoff"
    fi
  done
fi

linux_entry ()
{
  os="$1"
  version="$2"
  type="$3"
  args="$4"

  if [ -z "$boot_device_id" ]; then
      boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  fi
  if [ x$type != xsimple ] ; then
      case $type in
          recovery)
              title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "$(gettext "${GRUB_RECOVERY_TITLE}")")" ;;
          init-*)
              title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "${type#init-}")" ;;
          *)
              title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
      esac
      if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
          replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
          quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
          title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
          grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
      fi
      echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  else
      echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  fi      
  if [ "$quick_boot" = 1 ]; then
      echo "    recordfail" | sed "s/^/$submenu_indentation/"
  fi
  if [ x$type != xrecovery ] ; then
      save_default_entry | grub_add_tab
  fi

  # Use ELILO's generic "efifb" when it's known to be available.
  # FIXME: We need an interface to select vesafb in case efifb can't be used.
  if [ "x$GRUB_GFXPAYLOAD_LINUX" = x ]; then
      echo "    load_video" | sed "s/^/$submenu_indentation/"
  else
      if [ "x$GRUB_GFXPAYLOAD_LINUX" != xtext ]; then
          echo "        load_video" | sed "s/^/$submenu_indentation/"
      fi
  fi
  if ([ "$ubuntu_recovery" = 0 ] || [ x$type != xrecovery ]) && \
     ([ "x$GRUB_GFXPAYLOAD_LINUX" != x ] || [ "$gfxpayload_dynamic" = 1 ]); then
      echo "    gfxmode \$linux_gfx_mode" | sed "s/^/$submenu_indentation/"
  fi

  echo "        insmod gzio" | sed "s/^/$submenu_indentation/"
  echo "        if [ x\$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi" | sed "s/^/$submenu_indentation/"

  if [ x$dirname = x/ ]; then
    if [ -z "${prepare_root_cache}" ]; then
      prepare_root_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE} | grub_add_tab)"
    fi
    printf '%s\n' "${prepare_root_cache}" | sed "s/^/$submenu_indentation/"
  else
    if [ -z "${prepare_boot_cache}" ]; then
      prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
    fi
    printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
  fi
  if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; then
    message="$(gettext_printf "Loading Linux %s ..." ${version})"
    sed "s/^/$submenu_indentation/" << EOF
        echo    '$(echo "$message" | grub_quote)'
EOF
  fi
  if test -d /sys/firmware/efi && test -e "${linux}.efi.signed"; then
    sed "s/^/$submenu_indentation/" << EOF
        linux   ${rel_dirname}/${basename}.efi.signed root=${linux_root_device_thisversion} ro ${args}
EOF
  else
    sed "s/^/$submenu_indentation/" << EOF
        linux   ${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args}
EOF
  fi
  if test -n "${initrd}" ; then
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
    if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; then
      message="$(gettext_printf "Loading initial ramdisk ...")"
      sed "s/^/$submenu_indentation/" << EOF
        echo    '$(echo "$message" | grub_quote)'
EOF
    fi
    initrd_path=
    for i in ${initrd}; do
      initrd_path="${initrd_path} ${rel_dirname}/${i}"
    done
    sed "s/^/$submenu_indentation/" << EOF
        initrd  $(echo $initrd_path)
        boot
EOF
  fi
  sed "s/^/$submenu_indentation/" << EOF
}
EOF
}

machine=`uname -m`
case "x$machine" in
    xi?86 | xx86_64)
        list=
        for i in /boot/vmlinuz-* /vmlinuz-* /boot/kernel-* ; do
            if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
        done ;;
    *) 
        list=
        for i in /boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* /boot/kernel-* ; do
                  if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
        done ;;
esac

case "$machine" in
    i?86) GENKERNEL_ARCH="x86" ;;
    mips|mips64) GENKERNEL_ARCH="mips" ;;
    mipsel|mips64el) GENKERNEL_ARCH="mipsel" ;;
    arm*) GENKERNEL_ARCH="arm" ;;
    *) GENKERNEL_ARCH="$machine" ;;
esac

prepare_boot_cache=
prepare_root_cache=
boot_device_id=
title_correction_code=

cat << 'EOF'
function gfxmode {
        set gfxpayload="${1}"
EOF
if [ "$vt_handoff" = 1 ]; then
  cat << 'EOF'
        if [ "${1}" = "keep" ]; then
                set vt_handoff=vt.handoff=7
        else
                set vt_handoff=
        fi
EOF
fi
cat << EOF
}
EOF

# Use ELILO's generic "efifb" when it's known to be available.
# FIXME: We need an interface to select vesafb in case efifb can't be used.
if [ "x$GRUB_GFXPAYLOAD_LINUX" != x ] || [ "$gfxpayload_dynamic" = 0 ]; then
  echo "set linux_gfx_mode=$GRUB_GFXPAYLOAD_LINUX"
else
  cat << EOF
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
EOF
fi
cat << EOF
export linux_gfx_mode
EOF

# Extra indentation to add to menu entries in a submenu. We're not in a submenu
# yet, so it's empty. In a submenu it will be equal to '\t' (one tab).
submenu_indentation=""

is_top_level=true
while [ "x$list" != "x" ] ; do
  linux=`version_find_latest $list`
  case $linux in
    *.efi.signed)
      # We handle these in linux_entry.
      list=`echo $list | tr ' ' '\n' | grep -vx $linux | tr '\n' ' '`
      continue
      ;;
  esac
  gettext_printf "Found linux image: %s\n" "$linux" >&2
  basename=`basename $linux`
  dirname=`dirname $linux`
  rel_dirname=`make_system_path_relative_to_its_root $dirname`
  version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
  alt_version=`echo $version | sed -e "s,\.old$,,g"`
  linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"

  initrd_early=
  for i in ${GRUB_EARLY_INITRD_LINUX_STOCK} \
           ${GRUB_EARLY_INITRD_LINUX_CUSTOM}; do
    if test -e "${dirname}/${i}" ; then
      initrd_early="${initrd_early} ${i}"
    fi
  done

  initrd_real=
  for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
           "initrd-${version}" "initramfs-${version}.img" \
           "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
           "initrd-${alt_version}" "initramfs-${alt_version}.img" \
           "initramfs-genkernel-${version}" \
           "initramfs-genkernel-${alt_version}" \
           "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
           "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
    if test -e "${dirname}/${i}" ; then
      initrd_real="${i}"
      break
    fi
  done

  initrd=
  if test -n "${initrd_early}" || test -n "${initrd_real}"; then
    initrd="${initrd_early} ${initrd_real}"

    initrd_display=
    for i in ${initrd}; do
      initrd_display="${initrd_display} ${dirname}/${i}"
    done
    gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
  fi

  config=
  for i in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do
    if test -e "${i}" ; then
      config="${i}"
      break
    fi
  done

  initramfs=
  if test -n "${config}" ; then
      initramfs=`grep CONFIG_INITRAMFS_SOURCE= "${config}" | cut -f2 -d= | tr -d \"`
  fi

  if test -z "${initramfs}" && test -z "${initrd_real}" ; then
    # "UUID=" and "ZFS=" magic is parsed by initrd or initramfs.  Since there's
    # no initrd or builtin initramfs, it can't work here.
    if [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] \
        || [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ]; then

        linux_root_device_thisversion=${GRUB_DEVICE}
    else
        linux_root_device_thisversion=PARTUUID=${GRUB_DEVICE_PARTUUID}
    fi
  fi

  # The GRUB_DISABLE_SUBMENU option used to be different than others since it was
  # mentioned in the documentation that has to be set to 'y' instead of 'true' to
  # enable it. This caused a lot of confusion to users that set the option to 'y',
  # 'yes' or 'true'. This was fixed but all of these values must be supported now.
  if [ "x${GRUB_DISABLE_SUBMENU}" = xyes ] || [ "x${GRUB_DISABLE_SUBMENU}" = xy ]; then
    GRUB_DISABLE_SUBMENU="true"
  fi

  if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xtrue ]; then
    linux_entry "${OS}" "${version}" simple \
    "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"

    submenu_indentation="$grub_tab"
    
    if [ -z "$boot_device_id" ]; then
        boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
    fi
    # TRANSLATORS: %s is replaced with an OS name
    echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
    is_top_level=false
  fi

  linux_entry "${OS}" "${version}" advanced \
              "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
  for supported_init in ${SUPPORTED_INITS}; do
    init_path="${supported_init#*:}"
    if [ -x "${init_path}" ] && [ "$(readlink -f /sbin/init)" != "$(readlink -f "${init_path}")" ]; then
      linux_entry "${OS}" "${version}" "init-${supported_init%%:*}" \
                  "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} init=${init_path}"
    fi
  done
  if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
    linux_entry "${OS}" "${version}" recovery \
                "${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}"
  fi

  list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
done

# If at least one kernel was found, then we need to
# add a closing '}' for the submenu command.
if [ x"$is_top_level" != xtrue ]; then
  echo '}'
fi

echo "$title_correction_code"