Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # File: lfs-prep-stage1.sh
- start=`date +%s` &&
- source ~/lfs-functions.sh
- SCRIPT=lfs-prep-stage1.sh
- USR=$1
- HOST=$2
- DEVICE=$3
- LFS=/mnt/$USR
- USER_PASSWORD=Backin3???
- USER_HOME=/home/$USR
- LOGS=/mnt/$USR/logs # The path to the log files.
- TIME_LOGS=$LOGS/stage1-build-times.log # The time logs file.
- TOOLS=$LFS/tools
- SOURCES=$LFS/sources
- # Remove the auto start script command from roots startup .bash_profile file.
- sed -i '/source lfs-prep-stage1.sh/d' .bash_profile &&
- # Create a mount point for the build partition.
- # Use the stage 1 build user as the mounting directory name.
- [ ! -d /mnt/$USR ] &&
- mkdir -p /mnt/$USR &&
- # Mount build partition then add entry to /etc/fstab
- mount /dev/${DEVICE}2 /mnt/$USR &&
- echo "/dev/$DEVICE $LFS ext4 defaults 1 1" >> /etc/fstab &&
- sleep 1
- # Create some necessary directories.
- mkdir -pv $LFS/{dev,proc,sys,run,root,logs} &&
- # Creating initial device nodes
- mknod -m 600 $LFS/dev/console c 5 1 &&
- mknod -m 666 $LFS/dev/null c 1 3 &&
- # Mounting/Populating /dev and Virtual Kernel File Systems
- mount --bind /dev $LFS/dev &&
- mount -t devpts devpts $LFS/dev/pts -o gid=5,mode=620 &&
- mount -t proc proc $LFS/proc &&
- mount -t sysfs sysfs $LFS/sys &&
- mount -t tmpfs tmpfs $LFS/run &&
- if [ -h $LFS/dev/shm ]; then
- mkdir -pv $LFS/$(readlink $LFS/dev/shm)
- fi &&
- # Create sources directory then download and verify md5 on all source packages.
- if [ ! -d $SOURCES ]; then
- mkdir -p $SOURCES
- wget http://www.linuxfromscratch.org/lfs/downloads/stable/wget-list
- wget --input-file=wget-list --continue --directory-prefix=$SOURCES
- wget http://www.linuxfromscratch.org/lfs/downloads/stable/md5sums --directory-prefix=$SOURCES
- err=false
- pushd $LFS/sources
- while IFS= read -r line; do
- package=`echo $line | cut -f1 -d' '`
- result=`echo $line | cut -f2 -d' '`
- echo "md5sum: $package $result"
- [ $result != "OK" ] && err=true
- done <<< $(md5sum -c $SOURCES/md5sums) > $LOGS/md5sum.log
- [ $err == "true" ] && { echo Error: md5checksum failed.; exit 1; }
- popd
- fi &&
- # Create the tools directory and soft link.
- if [ ! -d $TOOLS ]; then
- mkdir -p $TOOLS
- ln -s $TOOLS /
- fi &&
- # Create user/group and set users password.
- groupadd $USR &&
- useradd -s /bin/bash -g $USR -m -k /dev/null $USR &&
- echo $USER_PASSWORD | passwd $USR --stdin &&
- # Create the time logs file
- touch $TIME_LOGS &&
- # Copy needed scripts to the build users home directory.
- cp lfs-build-stage1.sh lfs-functions.sh -t $USER_HOME &&
- # Create files .bashrc and .bash_profile for build user.
- cat > "$USER_HOME/.bash_profile" << EOL &&
- exec env -i HOME=$USER_HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
- EOL
- cat > $USER_HOME/.bashrc << EOL &&
- set +h
- umask 022
- LFS=/mnt/$USR
- LC_ALL=POSIX
- LFS_TGT=$(uname -m)-lfs-linux-gnu
- PATH=/tools/bin:/bin:/usr/bin
- export LFS LC_ALL LFS_TGT PATH
- alias vi=vim
- source lfs-build-stage1.sh $USR
- EOL
- # Set ownership of files so build user can access them.
- chown -R $USR:$USR $USER_HOME $SOURCES $TOOLS $LOGS &&
- # chown $USR:$USR $TIME_LOGS &&
- # Logging as user lfs will execute the script for build stage 1.
- su - lfs &&
- # Build stage 1 is done so clean up sources directory.
- # Remove all package build directories from stage 1 build.
- cd $SOURCES &&
- for i in `ls -A`; do if [ -d $i ]; then rm -rf $i; fi; done &&
- # Have root take ownership of the tool chain.
- chown -R root:root $TOOLS &&
- # Make stage 2 scripts available in the chroot environment.
- cp ~/lfs-prep-stage2.sh ~/lfs-build-stage2.sh ~/lfs-functions.sh -t $LFS/root/ &&
- # Add a command to start the stage 2 prep script,
- # to the root users .bash_profile file.
- # This command will be executed upon entering the chroot environment.
- echo "source ~/lfs-prep-stage2.sh $USR" > $LFS/root/.bash_profile
- # Enter the chroot environment, this will trigger the stage 2 prep script to execute.
- chroot "$LFS" /tools/bin/env -i \
- HOME=/root \
- TERM="$TERM" \
- PS1='chroot: \u:\w\$ ' \
- PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
- /tools/bin/bash --login +h
- # Update the build time logs with the run time stats.
- end=`date +%s` &&
- echo "S:$start E:$end T:$(format_time $[end-start]) S:$SCRIPT" >> $TIME_LOGS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement