LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   chroot from bash script not working (https://www.linuxquestions.org/questions/linux-newbie-8/chroot-from-bash-script-not-working-4175637011/)

cdgoldin 08-24-2018 02:38 AM

chroot from bash script not working
 
I have a bash script as follows:

for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt/root$i; done
cat << EOF | sudo chroot /mnt/root
update-initramfs -u
grub-install --recheck /dev/sda
grub-install --recheck /dev/sdb
update-grub
EOF

The commands work if entered manually in a terminal, but when the bash script runs, I get the following error message: "unable to execute /usr/sbin/chroot: input output error"

Why?

l0f4r0 08-24-2018 02:58 AM

Quote:

Originally Posted by cdgoldin (Post 5895130)
cat << EOF | sudo chroot /mnt/root
update-initramfs -u
grub-install --recheck /dev/sda
grub-install --recheck /dev/sdb
update-grub
EOF

What are you trying to attempt with those commands? What's their purpose?
Are you aware that
Code:

update-initramfs -u
grub-install --recheck /dev/sda
grub-install --recheck /dev/sdb
update-grub

won't execute but will only appear as text on your terminal? So I don't understand the meaning of piping the text output to chroot...

MadeInGermany 08-24-2018 04:40 AM

Perhaps you want to pass the commands to a chroot'ed shell?
Code:

sudo chroot /mnt/root /bin/sh << EOF
update-initramfs -u
grub-install --recheck /dev/sda
grub-install --recheck /dev/sdb
update-grub
EOF


cdgoldin 08-24-2018 07:02 AM

Quote:

Originally Posted by l0f4r0 (Post 5895134)
What are you trying to attempt with those commands? What's their purpose?
Are you aware that
Code:

update-initramfs -u
grub-install --recheck /dev/sda
grub-install --recheck /dev/sdb
update-grub

won't execute but will only appear as text on your terminal? So I don't understand the meaning of piping the text output to chroot...

1) These commands (from "Sudo chroot ... update-grub") are the final part of a system restore-from-backup routine that is run from Live CD. They install grub (and the grub menu) on the two physical disks (sda and sdb). If I don't enter these commands, the restored system will be unbootable, and I would have to install them with "Boot Repair". If I enter the commands manually from the terminal after the restore, they work as desired. But I want to automate the routine.

2) No, I was NOT aware that the commands following the "chroot" command won't execute but will only appear as text. Apparently I misunderstand what the "cat << EOF |" command does. I'm not trying to pipe the text output to chroot. I'm trying to pipe the command input to chroot. (This is what happens when I try to learn bash from forum examples).

3) I tried what "MadeinGermany" has suggested, and it worked.

4) Thank both of you for your help.

l0f4r0 08-28-2018 08:36 AM

Quote:

Originally Posted by cdgoldin (Post 5895232)
3) I tried what "MadeinGermany" has suggested, and it worked.
4) Thank both of you for your help.

Great, you're welcome. Can you mark your thread as "SOLVED" then? ;)

cdgoldin 08-28-2018 08:45 AM

Quote:

Originally Posted by l0f4r0 (Post 5896997)
Great, you're welcome. Can you mark your thread as "SOLVED" then? ;)

Sorry. I didn't know how to mark it as solved yesterday. I finally found it under "thread tools" today.


All times are GMT -5. The time now is 05:15 PM.