Advertisement
Uno-Dan

lfs prep stage 1

Jun 14th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.30 KB | None | 0 0
  1. #!/bin/bash
  2. # File: lfs-prep-stage1.sh
  3.  
  4. start=`date +%s` &&
  5. source ~/lfs-functions.sh
  6.  
  7. SCRIPT=lfs-prep-stage1.sh
  8. USR=$1
  9. HOST=$2
  10. DEVICE=$3
  11. LFS=/mnt/$USR
  12. USER_PASSWORD=Backin3???
  13. USER_HOME=/home/$USR
  14. LOGS=/mnt/$USR/logs # The path to the log files.
  15. TIME_LOGS=$LOGS/stage1-build-times.log # The time logs file.
  16. TOOLS=$LFS/tools
  17. SOURCES=$LFS/sources
  18.  
  19. # Remove the auto start script command from roots startup .bash_profile file.
  20. sed -i '/source lfs-prep-stage1.sh/d' .bash_profile &&
  21.  
  22. # Create a mount point for the build partition.
  23. # Use the stage 1 build user as the mounting directory name.
  24. [ ! -d /mnt/$USR ] &&
  25. mkdir -p /mnt/$USR &&
  26.  
  27. # Mount build partition then add entry to /etc/fstab
  28. mount /dev/${DEVICE}2 /mnt/$USR &&
  29. echo "/dev/$DEVICE $LFS ext4 defaults 1 1" >> /etc/fstab &&
  30. sleep 1
  31.  
  32. # Create some necessary directories.
  33. mkdir -pv $LFS/{dev,proc,sys,run,root,logs} &&
  34.  
  35. # Creating initial device nodes
  36. mknod -m 600 $LFS/dev/console c 5 1 &&
  37. mknod -m 666 $LFS/dev/null c 1 3 &&
  38.  
  39. # Mounting/Populating /dev and Virtual Kernel File Systems
  40. mount --bind /dev $LFS/dev &&
  41. mount -t devpts devpts $LFS/dev/pts -o gid=5,mode=620 &&
  42. mount -t proc proc $LFS/proc &&
  43. mount -t sysfs sysfs $LFS/sys &&
  44. mount -t tmpfs tmpfs $LFS/run &&
  45. if [ -h $LFS/dev/shm ]; then
  46.   mkdir -pv $LFS/$(readlink $LFS/dev/shm)
  47. fi &&
  48.  
  49. # Create sources directory then download and verify md5 on all source packages.
  50. if [ ! -d $SOURCES ]; then
  51.     mkdir -p $SOURCES
  52.     wget http://www.linuxfromscratch.org/lfs/downloads/stable/wget-list
  53.     wget --input-file=wget-list --continue --directory-prefix=$SOURCES
  54.     wget http://www.linuxfromscratch.org/lfs/downloads/stable/md5sums --directory-prefix=$SOURCES
  55.    
  56.     err=false
  57.     pushd $LFS/sources
  58.     while IFS= read -r line; do
  59.         package=`echo $line | cut -f1 -d' '`
  60.         result=`echo $line | cut -f2 -d' '`
  61.         echo "md5sum: $package $result"
  62.         [ $result != "OK" ] && err=true
  63.     done <<< $(md5sum -c $SOURCES/md5sums)  > $LOGS/md5sum.log
  64.     [ $err == "true" ] && { echo Error: md5checksum failed.; exit 1; }
  65.     popd
  66. fi &&
  67.  
  68. # Create the tools directory and soft link.
  69. if [ ! -d $TOOLS ]; then
  70.     mkdir -p $TOOLS
  71.     ln -s $TOOLS /
  72. fi &&
  73.  
  74. # Create user/group and set users password.
  75. groupadd $USR &&
  76. useradd -s /bin/bash -g $USR -m -k /dev/null $USR &&
  77. echo $USER_PASSWORD | passwd $USR --stdin &&
  78.  
  79. # Create the time logs file
  80. touch $TIME_LOGS &&
  81.  
  82. # Copy needed scripts to the build users home directory.
  83. cp lfs-build-stage1.sh lfs-functions.sh -t $USER_HOME &&
  84.  
  85. # Create files .bashrc and .bash_profile for build user.
  86. cat > "$USER_HOME/.bash_profile" << EOL &&
  87. exec env -i HOME=$USER_HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
  88. EOL
  89.  
  90. cat > $USER_HOME/.bashrc << EOL &&
  91. set +h
  92. umask 022
  93. LFS=/mnt/$USR
  94. LC_ALL=POSIX
  95. LFS_TGT=$(uname -m)-lfs-linux-gnu
  96. PATH=/tools/bin:/bin:/usr/bin
  97. export LFS LC_ALL LFS_TGT PATH
  98. alias vi=vim
  99. source lfs-build-stage1.sh $USR
  100. EOL
  101.  
  102. # Set ownership of files so build user can access them.
  103. chown -R $USR:$USR $USER_HOME $SOURCES $TOOLS $LOGS &&
  104. # chown $USR:$USR $TIME_LOGS &&
  105.    
  106. # Logging as user lfs will execute the script for build stage 1.
  107. su - lfs  &&  
  108.  
  109. # Build stage 1 is done so clean up sources directory.
  110. # Remove all package build directories from stage 1 build.
  111. cd $SOURCES &&
  112. for i in `ls -A`; do if [ -d $i ]; then rm -rf $i; fi; done &&
  113.  
  114. # Have root take ownership of the tool chain.
  115. chown -R root:root $TOOLS &&
  116.  
  117. # Make stage 2 scripts available in the chroot environment.
  118. cp ~/lfs-prep-stage2.sh ~/lfs-build-stage2.sh ~/lfs-functions.sh -t $LFS/root/ &&
  119.  
  120. # Add a command to start the stage 2 prep script,
  121. # to the root users .bash_profile file.
  122. # This command will be executed upon entering the chroot environment.
  123. echo "source ~/lfs-prep-stage2.sh $USR" > $LFS/root/.bash_profile
  124.  
  125. # Enter the chroot environment, this will trigger the stage 2 prep script to execute.
  126. chroot "$LFS" /tools/bin/env -i \
  127.     HOME=/root                  \
  128.     TERM="$TERM"                \
  129.     PS1='chroot: \u:\w\$ ' \
  130.     PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
  131.     /tools/bin/bash --login +h
  132.    
  133.  
  134. # Update the build time logs with the run time stats.
  135. end=`date +%s` &&
  136. echo "S:$start E:$end T:$(format_time $[end-start]) S:$SCRIPT" >> $TIME_LOGS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement