我正在编写一个脚本,该脚本旨在从基于 Debian 的实时映像运行。其目标是从主硬盘驱动器上擦除所有数据,安装 Debian Buster(通过debootstrap
),然后配置 GRUB 以允许自动启动系统分区。这是一个大型项目的第一步。
脚本似乎运行正常。但是,当我启动新创建的系统时,屏幕上只显示该单词GRUB
,没有其他内容 - 甚至没有 GRUB 提示符。
我错过了什么?这是我目前的脚本:
#!/bin/sh
if [ "$(id -u)" -ne 0 ]; then
echo "Please run script as root user." >&2
exit 1
fi
# Install prerequisites
apt update
apt install debootstrap
# Wipe out drive and set up GPT table
umount /mnt/*
fuser -km /dev/sda*
sgdisk --zap-all /dev/sda
parted /dev/sda mklabel gpt
# hd0,1
parted /dev/sda mkpart primary ext4 0% 512MB
parted /dev/sda set 1 boot on
parted /dev/sda set 1 bios_grub on
yes | mkfs.ext4 /dev/sda1
# hd0,2
parted /dev/sda mkpart primary ext4 512MB 100%
yes | mkfs.ext4 /dev/sda2
mkdir -p /mnt/root
mkdir -p /mnt/boot
mount /dev/sda1 /mnt/boot
mount /dev/sda2 /mnt/root
# Set up Operating System
debootstrap --arch=amd64 buster /mnt/root http://deb.debian.org/debian/
mount --bind /dev /mnt/root/dev
mount --bind /proc /mnt/root/proc
mount --bind /sys /mnt/root/sys
# chroot to install programs
chroot /mnt/root /bin/bash << "EOT"
# Add the community repositories to the sources list
echo "deb http://deb.debian.org/debian/ stable main contrib non-free" >> /etc/apt/sources.list
echo "deb-src http://deb.debian.org/debian/ stable main contrib non-free" >> /etc/apt/sources.list
apt update -y
# Packages to install
apt install -y grub-pc grub-common
grub-install /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg
update-grub
EOT
答案1
正如一些人在上面的帖子评论中指出的那样,我做错了几件事。
- 我一直将 MBR 与 GPT 结合使用。我决定采用较旧的 MBR 方法进行分区,这
parted /dev/sda mklabel gpt
意味着parted /dev/sda mklabel msdos
…… - 我需要将我的
/boot
分区挂载到主文件系统中,即使它位于不同的分区上。挂载/dev/sda2/
到mnt/root/boot
(如上所述)有效。 - 我不需要这个
parted /dev/sda set 1 bios_grub on
命令。 debootstrap
不安装内核。我可以从shellapt
内部进行安装chroot
(如下所示)。
以下是最终实际起作用的脚本:
#!/bin/sh
if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
echo "Internet connection successful."
else
echo "Internet is not connected. Setup cannot continue."
exit 1
fi
if [ "$(id -u)" -ne 0 ]; then
echo "Please run script as root user." >&2
exit 1
fi
# Install prerequisites
apt update
apt install debootstrap
# Wipe out drive and set up GPT table
umount /mnt/*
fuser -km /dev/sda*
sgdisk --zap-all /dev/sda
parted /dev/sda mklabel msdos
# hd0,1
parted /dev/sda mkpart primary ext4 0% 512MB
parted /dev/sda set 1 boot on
parted /dev/sda set 1 bios_grub on
yes | mkfs.ext4 /dev/sda1
# hd0,2
parted /dev/sda mkpart primary ext4 512MB 100%
yes | mkfs.ext4 /dev/sda2
mkdir -p /mnt/root
mount /dev/sda2 /mnt/root
mkdir -p /mnt/root/boot
mount /dev/sda1 /mnt/root/boot
# Set up Operating System
debootstrap --arch=amd64 buster /mnt/root http://deb.debian.org/debian/
mount --bind /dev /mnt/root/dev
mount --bind /proc /mnt/root/proc
mount --bind /sys /mnt/root/sys
# chroot to install programs
chroot /mnt/root /bin/bash << "EOT"
# Add the community repositories to the sources list
echo "deb http://deb.debian.org/debian/ stable main contrib non-free" >> /etc/apt/sources.list
echo "deb-src http://deb.debian.org/debian/ stable main contrib non-free" >> /etc/apt/sources.list
apt update -y
apt upgrade -y
# Install Linux kernel
apt install -y linux-image-amd64 grub-pc grub-common
grub-install /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg
echo "GRUB_DEFAULT=0
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet"
GRUB_CMDLINE_LINUX=\"\"" > /etc/default/grub
update-grub
# Set up users
apt install -y sudo
useradd agent
echo "root:Shebang01#!" | chpasswd
echo "agent:Shebang01#!" | chpasswd
usermod -aG sudo agent
# Set up internet
echo graphene-hostname > /etc/hostname
echo "auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
source-directory /etc/network/interfaces.d" > /etc/network/interfaces
apt install -y network-manager
[5:48 PM]
# Desktop
apt install -y xorg xserver-xorg-video-all xserver-xorg-core xinit fonts-liberation fonts-dejavu fonts-droid-fallback fonts-noto fonts-roboto openbox qterminal xscreensaver pulseaudio
echo "/dev/sda2 / ext4 defaults 0 0" > /etc/fstab
# edit /etc/systemd/system/getty.target.wants/getty\@tty1.service so that ExecStart reads:
# ExecStart=-/sbin/agetty --noclear -p "" -a myuser %I $TERM
# Install laptop + portability utilities
# apt install -y wireless-tools acpi-support cpufrequtils acpi wpasupplicant powertop acpid apmd pcmciautils pm-utils anacron avahi-autoipd bluetooth
# Common apps
apt install -y chromium
apt autoremove -y
EOT