2

I am writing a script to automate my Arch Linux installation process and I've bumped into an environmental problem. I have a file with the installation sequence and a file that holds all the functions, sourced in the beginning of the script.

Running arch-chroot /mnt /bin/bash -c someFunction does not recognize the function nor the variables set before this line of code.

If I export -f someFunction it will recognize the function, but someFunction in itself is just a decomposition function, which only calls other functions. What is the most elegant way to source all the functions inside the chroot environment?

I also need a way of exporting all variables, set by the user at the beginning of the script, into the chrooted environment.(I'm guessing solving the above will also solve this problem)

2 Answers 2

1

I managed to pass my variables without copying them over ( would present a security risk to copy sensitive variables outside the script ). Below is a snippet.

cp archsin-functions /mnt/root/;

arch-chroot /mnt /bin/bash -- << EOCHROOT

    source \$HOME/archsin-functions;

    createSwap;
    setLocale "${CITY}";
    setHost "${HSTNAME}";
    setInitramfs "${REPOAPITOKEN}" "${REPOID}";
    setUser "${USRGROUPS}" "${USRNAME}" "${USRPASSWD}" "${REPOAPITOKEN}" "${REPOID}";

    systemctl enable NetworkManager.service

EOCHROOT

shred --verbose -u --zero --iterations=3 /mnt/root/archsin-functions;

<< EOCHRROOT - heredoc pases the lines up to EOCHROOT marker to the /bin/bash execution shell created in the chroot env.

Since variable expansion is done when the file is read the functions will be served the data held inside the variables correctly. The escaped $ prevents expansion of $HOME until the line is actually executed. When it is executed it will be done inside the chroot environment making $HOME evaluate to the /root folder of the chroot env where the functions file has been placed. After execution the file will be removed without a trace.

0

Put the variables and functions in a file in the /etc/profile.d directory in the chroot hierarchy, and bash should source that upon startup (if you've set up the other /etc files bash uses).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .