You are not logged in.

#1 2025-02-12 12:00:02

Succulent of your garden
Member
Registered: 2024-02-29
Posts: 201

[SOLVED] How can I Pass a variable to chroot ?

Hi

    Lets think i have  this:

arch-chroot /mnt /myScript.sh

I had send myScript.sh to that path in /mnt . How can I send a varaible to my new chroot environment ? my arch-chroot command is part of a script and have the variable $myDisk . myScript.sh must have the variable $myDisk with the content available in the new chroot. Can I do that ?

Last edited by Succulent of your garden (2025-02-12 21:39:57)

Offline

#2 2025-02-12 12:42:29

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 336

Re: [SOLVED] How can I Pass a variable to chroot ?

Succulent of your garden wrote:

How can I send a varaible to my new chroot environment ?

# myDisk=value arch-chroot /mnt/ /myScript.sh

?

Offline

#3 2025-02-12 12:50:26

Lone_Wolf
Administrator
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 13,458

Re: [SOLVED] How can I Pass a variable to chroot ?

If it doesn't work , a few other possibilties are :
putting the envvar in  a file that is included in the chroot mount and sourcing that file in the script

SO thread with several (untested by me) options .


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.

clean chroot building not flexible enough ?
Try clean chroot manager by graysky

Offline

#4 2025-02-12 13:16:17

Succulent of your garden
Member
Registered: 2024-02-29
Posts: 201

Re: [SOLVED] How can I Pass a variable to chroot ?

I forget to comment that the the content of the variable $myDisk is given by the user by using read command. So I can hardcode it. It must be something like

arch-chroot /mnt /myscript.sh $myDisk

but that obviously does not works.

Offline

#5 2025-02-12 15:27:25

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 125

Re: [SOLVED] How can I Pass a variable to chroot ?

Seems like arch-chroot itself is a bash script  as per https://wiki.archlinux.org/title/Chroot … rch-chroot

You can always make a copy and modify it to accept whatever arguments.

Last edited by ReDress (2025-02-12 15:27:53)

Offline

#6 2025-02-12 16:04:41

Succulent of your garden
Member
Registered: 2024-02-29
Posts: 201

Re: [SOLVED] How can I Pass a variable to chroot ?

ReDress wrote:

Seems like arch-chroot itself is a bash script  as per https://wiki.archlinux.org/title/Chroot … rch-chroot

You can always make a copy and modify it to accept whatever arguments.

but that would work if I need launch the arch-chroot from a script ? I can paste the arch-chroot script directly but that would made my script a lot harder to mantain. I split the main scripts in two, its for my custom arch installation. The first part is before the chroot, and I really want to be able to just pass the variable from the first part to the second, so I can automate my custom installation without asking more than the necessary inputs by user. My $myDisk is the path of the drive which arch is being installed (for example /dev/sda), I need that  variable to automate the grub installation for bios systems without the need to ask the user again.

Last edited by Succulent of your garden (2025-02-12 16:05:06)

Offline

#7 2025-02-12 17:41:33

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 336

Re: [SOLVED] How can I Pass a variable to chroot ?

Succulent of your garden wrote:

I forget to comment that the the content of the variable $myDisk is given by the user by using read command.

read -p "Enter disk name: " myDisk
export myDisk
arch-chroot /mnt /myscript.sh

or

read -p "Enter disk name: "
myDisk="$REPLY" arch-chroot /mnt /myscript.sh

Offline

#8 2025-02-12 19:01:00

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 336

Re: [SOLVED] How can I Pass a variable to chroot ?

But I would do

read -p "Enter disk name: " myDisk
arch-chroot /mnt /myscript.sh "$myDisk"

and in myscript.sh:

myDisk="$1"  # Or whatever parameter index it is

Offline

#9 2025-02-12 21:39:36

Succulent of your garden
Member
Registered: 2024-02-29
Posts: 201

Re: [SOLVED] How can I Pass a variable to chroot ?

Thanks dimich you made my day undestranding that the chroot variables can be get using the $1 and so on.  My script now works smile

Offline

Board footer

Powered by FluxBB