在 chroot 环境中隐藏设备

在 chroot 环境中隐藏设备

在 Linux 上,我正在寻找一种在 chroot 环境中运行的方法grub-install <device>update-grub而不需要 GRUB 考虑某些设备(我不希望它们在启动时出现)。

我想从 A 盘上的系统 chroot 到具有自己系统的 B 盘,以便在那里安装 grub。因此,grub 将自身安装在 B 上,并包含条目 A 和 B。它可以工作,但是当我启动磁盘 B(没有 A)时,我会看到所有条目(包括 A),这是我不想要的。因此我的问题。

我测试过的...

当我在磁盘 /dev/sda 上启动新的 Debian Buster 安装以及第二个磁盘 /dev/sdb 和新的 Debian Buster 安装时,我得到了:

root@buster:~# blkid -s UUID
/dev/sda1: UUID="0505963d-a522-415c-ba85-57bac4b7e0ae"
/dev/sda5: UUID="40e956ea-05c7-4099-b9c8-3b0c97780db0"
/dev/sdb1: UUID="afc267fa-5f9b-464a-b9c0-02437f83b28f"
/dev/sdb5: UUID="ae03e23b-cdbf-4b46-96d1-0f1b0b5ac13b"

=> 列出了 2 个磁盘

GRUB 菜单启动是(我也写了一个命令来显示 UUID,结果是法语):

Debian GNU/Linux [0505963d-a522-415c-ba85-57bac4b7e0ae]
Options avancées pour Debian GNU/Linux [0505963d-a522-415c-ba85-57bac4b7e0ae]

=> 仅涉及 /dev/sda1

所以,我准备了chroot环境:

root@buster:~# DEST=/dev/sdb
root@buster:~# mkdir -p /mnt${DEST}1
root@buster:~# mount -t ext4 ${DEST}1 /mnt${DEST}1
root@buster:~# for i in /dev /proc /sys /run /sys ; \
               do mount -B $i /mnt${DEST}1$i ; done
root@buster:~# DEST=$DEST chroot /mnt${DEST}1

从 chroot 环境:

root@buster:/# cat /etc/fstab 
...
UUID=afc267fa-5f9b-464a-b9c0-02437f83b28f /               ext4    ...
UUID=ae03e23b-cdbf-4b46-96d1-0f1b0b5ac13b none            swap    ...

root@buster:/# blkid -s UUID
/dev/sda1: UUID="0505963d-a522-415c-ba85-57bac4b7e0ae"
/dev/sda5: UUID="40e956ea-05c7-4099-b9c8-3b0c97780db0"
/dev/sdb1: UUID="afc267fa-5f9b-464a-b9c0-02437f83b28f"
/dev/sdb5: UUID="ae03e23b-cdbf-4b46-96d1-0f1b0b5ac13b"

=> chroot 环境看到的东西与非 chroot 环境相同

我在上面安装了 grub :

root@buster:/# grub-install ${DEST}
root@buster:/# update-grub

=> 成功,没有错误!

但是当显示 GRUB 菜单时(与上面使用的命令相同):

Debian GNU/Linux [afc267fa-5f9b-464a-b9c0-02437f83b28f]
Options avancées pour Debian GNU/Linux [afc267fa-5f9b-464a-b9c0-02437f83b28f]
Debian GNU/Linux 10 (buster) (sur /dev/sda1) [0505963d-a522-415c-ba85-57bac4b7e0ae]
Options avancées pour Debian GNU/Linux 10 (buster) (sur /dev/sda1) [0505963d-a522-415c-ba85-57bac4b7e0ae]

=> 列出了分区 /dev/sdb1 和 /dev/sda1,但我不想要 /dev/sda1

因此,我正在寻找一种在 chroot 环境中安装 GRUB 的方法,而 GRUB 不会考虑某些设备。

我在网上搜索,但没有找到实现此目的的方法以及是否可能。我该怎么做?


好的,我找到了一个或多或少令我满意的解决方案,它与系统无关。

这个想法是在安装 GRUB 之前使 os-prober 不可执行并在 (来源的想法):

os_prober_path=$( which os-prober ) && perms=$( getfacl -e $os_prober_path ) \ 
  && chmod a-x $os_prober_path
grub-install ${DEST}
update-grub
[[ $os_prober_path ]] && echo "$perms" |setfacl -M- $os_prober_path

最后我们有:

# needs : gawk acl
DEST=/dev/sdb
mkdir -p /mnt${DEST}1
mount -t ext4 ${DEST}1 /mnt${DEST}1
for i in /dev /proc /sys /run /sys ; do mount -B $i /mnt${DEST}1$i; done
DEST=$DEST chroot /mnt${DEST}1
os_prober_path=$( which os-prober ) && perms=$( getfacl -e $os_prober_path ) \
  && chmod a-x $os_prober_path
grub-install ${DEST}
update-grub
[[ $os_prober_path ]] && echo "$perms" |setfacl -M- $os_prober_path
exit
for i in /dev /proc /sys /run /sys ; do umount -l /mnt${DEST}1$i; done
umount -l /mnt${DEST}1
rmdir /mnt${DEST}1

但我总是寻找一种方法来禁用某些设备。所以如果你有其他想法...

答案1

看起来您正在尝试控制 grub 菜单的生成方式。这些通常是通过/etc/grub.d.我相信您特别感兴趣的是“os-prober”,它可以查找其他已安装的操作系统。

当然,如果您想删除所有其他操作系统,您只需禁用 os-prober 即可chmod ugo-x /etc/grub.d/30_os-prober停止脚本的可执行。

正常情况是未安装其他操作系统,因此安装点不太可能影响此脚本的行为方式。它会根据内存尝试安装驱动器以调查驱动器上安装的内容。

我从你的脚本中看到你正在绑定安装/dev到你的 chroot 环境中。如果可以控制 os-prober,那么可以通过从 /dev 中删除驱动器来控制。
你可以尝试复制设备文件而不是绑定安装 /dev。然后您可以在运行之前随意删除任何您想要的内容update-grub

如果做不到这一点,我担心您可能会被迫完全禁用 os-prober,然后创建自己的脚本来模仿它,减去不需要的驱动器。

相关内容