[SOLVED] Setting LFS environment variable at login
Linux From ScratchThis Forum is for the discussion of LFS.
LFS is a project that provides you with the steps necessary to build your own custom Linux system.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
As recommended in chapter 2.4. Setting The $LFS Variable I am trying to set the LFS environment variable at login. But it is a bit difficult for me to do.
What I have tried so far is to add the line
Code:
export LFS=/mnt/lfs
to the /root/.bash_profile and to the lfs user in /home/lfs/.bash_profile. But that did not work.
I have then studied the /etc/profile and found that it runs each script (.sh) file in a folder called /etc/profile.d/. So I decided to make my own lfsenv.sh script file in this folder with the following lines.
Code:
# LFS environment default værdi.
# Bruges til Linux From Scratch.
echo 'Kører LFS'
export LFS=/mnt/lfs
echo $LFS
Then I logged out out and logged ind again (with the console mode from Ctrl+Alt+F2).
What I can see is that the lines are executed since the echo lines does get printed when I log in with the lfs user. And the second echo line prints "/mnt/lfs" just fine. But when I try to print the LFS variable by entering
Code:
lfs@debian:~$ echo $LFS
in the command promt I only get a blank line in return. So the LFS environment variable has not survived the login. It must be out of scope somehow.
Could someone please help me find out why this variable does not stay in scope of the login session?
The whole idea of LFS for me is to learn the inner workings of a Linux machine. And the book suggest that I should add the LFS env variable at this point. The chapter 4 I have not gotten to yet but thanks for giving me the heads up on that.
Of course I could issue the command at the time spend next to nothing when logged in. And yes I have probably spend more time trying to get this right in the auto login process than doing this manually would cost for the next couple of years but that kind of defeat the purpose of learning the stuff.
This could also be a case others wanted a help with and that is why I have started this thread.
Is it the wrong spot to try to make an environment variable using my own script? I thought this would be the right way instead of altering the /evt/profile script.
On a second note. The book state that
Quote:
...In addition,
the shell specified in the /etc/passwd file for all users that need the LFS variable needs to be bash to
ensure that the /root/.bash_profile file is incorporated as a part of the login process.
As I understand it the /root/.bash_profile script file is executed for all users when they log in. Can that be right? Does a normal user have access rights for the /root/.bash_profile as part of the login?
It turns out that the lfsenv.sh script is running as it should for other users than the lfs user after a reboot. If logged in as root or another user the $LFS environment variable is still present when examined by the command prompt
Code:
echo $LFS
It is only the lfs user that it is not working for. Now the book has instructed me to make a special line in the /lfs/.bash_profile script
Now when the lfs user login the line "Running new env" is shown and then nothing more shows. The command "exec env..." is stopping the script right after it is executed and preventing the rest of the script to run. I think that is also why the lfsenv.sh script is not executed either.
Is the book wrong here in suggesting setting the environment variable up or am I still doing something wrong here?
As I understand it the /root/.bash_profile script file is executed for all users when they log in. Can that be right? Does a normal user have access rights for the /root/.bash_profile as part of the login?
The book is a trifle obscure there. What it is trying to say is that the LFS variable has to be set for root as well as for the lfs user. The simplest way to ensure this is to put it into root's .bash_profile file. But as this file is only read when the shell is bash, people with unusual shells must also edit their /etc/passwd file to ensure that root uses only bash for the duration of the build. As you have rightly guessed, normal users do not have access to any of root's files.
Setting up the LFS user correctly is absolutely core to getting the build off to a good start. If you get it wrong here, things can collapse further down the line. I appreciate your desire to learn by experiment but this is not the right place for it, especially in your first LFS build. I suggest that you follow the book slavishly and set up a separate project to learn about scripts and environmental variables.
I suggest that you follow the book slavishly and set up a separate project to learn about scripts and environmental variables.
Well I might be fighting a loosing battle here but it was note in the book that suggested that I should set the LFS environment variable during login. But that seems to be a bad advice since the lfs user cannot have it set on login due to the altered and simplified bash login.
Thanks for clearing up the part with the root .bash_profile.
On to the project without an automated LFS environment variable setting it is
I definitely jumped the gun on this one. What I have been doing is remembering the note in chapter 2 about the LFS environment variable and while I was going through chapter 4.4 I made a mistake. I jumped out of the follow-the-book and started adding the LFS environment variable on my own. But I should have paid special attention to what the book says about the new user:
Quote:
When logged on as user lfs, the initial shell is usually a login shell which reads the /etc/profile of the host (probably containing some settings and environment variables) and then .bash_profile. The exec env -i.../bin/bash command in the .bash_profile file replaces the running shell with a new one with a completely empty environment, except for the HOME, TERM, and PS1 variables. This ensures that no unwanted and potentially hazardous environment variables from the host system leak into the build environment. The technique used here achieves the goal of ensuring a clean environment.
And just as it was meant to stop the bash scripts from running I was driving strait into the wall by trying to execute the automatic LFS environment variable with /etc/profile... go figure. And if I had bothered to read just a few lines further I can see that they in the very next step are setting up the LFS environment variable for the lfs user in the .bashrc script. Just as stoat was telling me.
Very well. But just so you'll know, the issue will return at the end of Chapter 5 when you stop working as the lfs user and begin working as root in Chapter 6 and beyond. But it's again a very short-lived issue. The LFS variable is important only for the first few commands in Chapter 6 after which you enter the chroot environment and the LFS variable becomes irrelevant until the end of the book when you logout and unmount the filesystems.
I only mention this now so you will know it's coming later on. When you get to Chapter 6, I recommend that you simply become root in a terminal, run the export command again, double-check the LFS variable, and begin working. Within a very few steps in Chapter 6, you will enter chroot and all this LFS variable stuff is over until the final steps in the book.
That could be the case. You need to have the variable set in the LFS user's login scripts. The Book recommends using .bash_profile for this, as it's the file generally used for setting environmental variables. But graphical terminals may not run this script on starting so you might need to put the instructions in .bashrc. See the note at the end of https://www.linuxfromscratch.org/lfs.../aboutlfs.html for details.
That could be the case. You need to have the variable set in the LFS user's login scripts. The Book recommends using .bash_profile for this, as it's the file generally used for setting environmental variables. But graphical terminals may not run this script on starting so you might need to put the instructions in .bashrc. See the note at the end of https://www.linuxfromscratch.org/lfs.../aboutlfs.html for details.
Ok, I think I understand. The book says I should use a .bashrc file, but does that render the .bash_profile file superfluous?
In chapter 4.4 (Setting Up the Environment) you create a .bashrc file that seems to declare the $LFS variable, so I don't understand why it was empty when logged into the lfs user?
I used Debian 11 for the build process, but maybe there are distros that are better suited for LFS?
Slackware or an existing LFS are probably the best, but lots of people have done it using Debian. There are two things you need to look out for specially: Debian uses dash as its system shell (the one used by default in build scripts) and mawk as its awk. That will not work. You need to install gawk to replace mawk and make bash your system shell for the duration of the build. I used to do that by temporarily linking /bin/sh to /bin/bash but there's probably a more elegant way to do it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.