On Linux, I'm looking for a way to run grub-install <device>
and update-grub
in a chrooted environment without some devices being taken into account by GRUB (I don't want them to appear at boot).
From a system on a disk A, I want to chroot to a disk B with its own system, to install grub there. So grub installs itself on B with entries A and B. It works but when I boot disk B (without A) I see all the entries (including A), which I don't want. Hence my question.
What I tested...
When I booted a fresh Debian Buster install on a disk /dev/sda with a second disk /dev/sdb with too a fresh Debian Buster install, I had :
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 disks are listed
The GRUB menu boot was (I wrote a command to display UUID too and the result is in french) :
Debian GNU/Linux [0505963d-a522-415c-ba85-57bac4b7e0ae]
Options avancées pour Debian GNU/Linux [0505963d-a522-415c-ba85-57bac4b7e0ae]
=> only /dev/sda1 is concerned
So, I prepared the chroot environment :
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
From the chrooted environment :
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"
=> the chrooted environment saw the same things as the NON-chrooted one
I installed grub on it :
root@buster:/# grub-install ${DEST}
root@buster:/# update-grub
=> success, no errors !
But when the GRUB menu is displayed (the same command as these used above) :
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]
=> the partitions /dev/sdb1 and /dev/sda1 are listed, but I do NOT want /dev/sda1
So I am looking for a way to install GRUB in a chrooted environment without some devices being taken into account by GRUB.
I searched in web but I did not find a way to achieve this and if it is possible. How can I do it?
OK so I found a solution that more or less satisfies me which is pretty system agnostic.
The idea is to make os-prober NOT executable before installing GRUB and reverting back after (source of idea) :
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
Finally we have :
# 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
But I always search a way to disable some devices. So if you have another idea...
/dev/
directory is populated by the kernel. What do you mean by "I don't want them to appear at boot"? What does boot have to do with chroot? I think you should introduce the question with some motivation about what you are really trying to achieve, because this seems like an XY Problem meta.stackexchange.com/questions/66377/what-is-the-xy-problem