You are not logged in.

#1 2023-09-29 15:59:29

bedtime
Member
Registered: 2019-02-12
Posts: 106

Changing chroot prompt in script [Resolved]

I have have this command in a script:

arch-chroot /mnt

I'd like to be able to go into chroot and have the prompt (PS1) change to let me know I'm still in chroot. I could easily just paste the export code in there myself once in chroot, but I'd like it to pop up automatically.

Pseudo code:

export PS1="(chroot) $PS1" | arch-choot /mnt

Last edited by bedtime (2023-09-30 22:01:26)

Offline

#2 2023-09-29 19:07:05

seth
Member
Registered: 2012-09-03
Posts: 63,167

Re: Changing chroot prompt in script [Resolved]

You've your shell rc (try to) detect whether you're in a chroot and adjust the promt, see eg. https://unix.stackexchange.com/question … n-a-chroot for heuristics

Offline

#3 2023-09-29 21:59:35

bedtime
Member
Registered: 2019-02-12
Posts: 106

Re: Changing chroot prompt in script [Resolved]

seth wrote:

You've your shell rc (try to) detect whether you're in a chroot and adjust the promt, see eg. https://unix.stackexchange.com/question … n-a-chroot for heuristics

Thanks. But this isn't exactly what I'm looking for. I want to be able to change the PS1 prompt to say 'chroot' while in chroot automatically.

*** edit ***

Okay, I found something that seems to work for now:

[root@user-hpprodesk600g3sff user]# arch-chroot /mnt bash -c 'export PS1="(chroot) # "; sh'
(chroot) # 

Last edited by bedtime (2023-09-29 22:28:48)

Offline

#4 2023-09-30 06:09:22

seth
Member
Registered: 2012-09-03
Posts: 63,167

Re: Changing chroot prompt in script [Resolved]

arch-chroot /mnt bash -c 'exec env PS1="(chroot) # " bash --norc'

But how is that "automatically"???
You're chrooting, running bash, exporting a PS1 and run a subshell which is most likely (but not guaranteed) bash, but in posix mode (implicitily by argv[0] being /bin/sh) which as a side-effect skips parsing the bashrc
That's the opposite of automatic, that's very explicit and not very robust (see command above)

Maybe be more specific what the problem is you're trying to solve?

Offline

#5 2023-09-30 13:48:45

bedtime
Member
Registered: 2019-02-12
Posts: 106

Re: Changing chroot prompt in script [Resolved]

seth wrote:
arch-chroot /mnt bash -c 'exec env PS1="(chroot) # " bash --norc'

But how is that "automatically"???
You're chrooting, running bash, exporting a PS1 and run a subshell which is most likely (but not guaranteed) bash, but in posix mode (implicitily by argv[0] being /bin/sh) which as a side-effect skips parsing the bashrc
That's the opposite of automatic, that's very explicit and not very robust (see command above)

Maybe be more specific what the problem is you're trying to solve?

I didn't know it would introduce all these issues. I just wanted something that would bring me into chroot and automatically change the prompt to show I was in chroot. Thanks for telling me this. After hearing this, I think I'm better off with the plain code below, until something better comes up:

echo -e "\nYou are now going to be placed into chroot. Type 'exit' to leave.\n"
arch-chroot /mnt /bin/bash
echo -e "\nYou've left chroot.\n"

The issue I'm having is that after entering a bunch of commands in chroot, sometimes I get bumped out of chroot and don't know it. And often the exit text is buried in dozens of lines of text.

I know there's a way to do this as I saw it done in Debian's Live CD when using rescue mode, and I know that Debian wouldn't have done this if they thought it would be insecure or unreliable. Thing is, I wasn't able to find the code that did it. I'm gonna have to keep looking around.

Offline

#6 2023-09-30 14:11:20

seth
Member
Registered: 2012-09-03
Posts: 63,167

Re: Changing chroot prompt in script [Resolved]

Debian uses /etc/debian_chroot and scans for that in the default shell rc's (it's what I suggested in #2, I think the thread even hints at that)
If you can/want to rely on help from the chrooting shell, you can just "arch-chroot /mnt bash -c 'exec env IS_CHROOT=true bash'" and test for that variable in the shell rc when setting the PS1.

Alternatively, the command in #4 or rather

arch-chroot /mnt bash -ic 'exec env PS1="(chroot) # " bash --norc'

should™ be fine, bash doesn't operate in posix mode, you get an interactive parenting shell (hopefully sourcing all relevant scripts) and then a nested instance that doesn't re-run the rc's and therefore picks up the PS1 environment

This just isn't very automatic where a bashrc that understands that it operates in a chroot will hint you that no matter how the chroot was triggered.

Offline

#7 2023-09-30 20:42:00

bedtime
Member
Registered: 2019-02-12
Posts: 106

Re: Changing chroot prompt in script [Resolved]

seth wrote:

Alternatively, the command in #4 or rather

arch-chroot /mnt bash -ic 'exec env PS1="(chroot) # " bash --norc'

should™ be fine, bash doesn't operate in posix mode, you get an interactive parenting shell (hopefully sourcing all relevant scripts) and then a nested instance that doesn't re-run the rc's and therefore picks up the PS1 environment

This just isn't very automatic where a bashrc that understands that it operates in a chroot will hint you that no matter how the chroot was triggered.

Thanks so much. It works!

I had to look up what a few of those parameters did. Also had to reread what you wrote a few times; but, as of now, I think I'm able to wrap my brain around how this works. Feels nice to know instead of just blindly running stuff.

Learned something today! Thanks again seth! smile

Offline

#8 2023-09-30 21:02:23

seth
Member
Registered: 2012-09-03
Posts: 63,167

Re: Changing chroot prompt in script [Resolved]

Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.

Offline

Board footer

Powered by FluxBB