#!/bin/bash #©keithhedger Mon 2 Feb 12:38:29 GMT 2015 kdhedger68713@gmail.com RED='\e[1;31m' GREEN='\e[1;32m' NORMAL='\e[0;0m' trap 'echo -e "$0 ${RED}FAILED${NORMAL} at line ${GREEN}${LINENO}${NORMAL}"' ERR readWithDefault () { local varname="$1" local varcontents="${!varname}" local message="$2" read -e -p "$message [ ${varcontents} ] " if [ "X$REPLY" != "X" ];then eval "${varname}=$REPLY" fi } if [ $UID -ne 0 ];then echo "You MUST run this script as root ..." exit 100 fi if [ -e ./SystemData ];then . ./SystemData else echo "No SystemData file found ..." exit 100 fi if [[ -z $LFS ]];then echo 'LFS NOT SET!!! Exiting ...' exit 100 else readWithDefault "LFS" "LFS set to \"$LFS\"" if ! mountpoint $LFS;then echo "LFS NOT Mounted, exiting ..." exit 100 fi fi pushd $LFS mkdir -vp dev proc sys lib${LIBDIRSUFFIX} opt media usr var bin boot etc tmp home run sbin mnt root srv mknod -m 600 dev/console c 5 1 mknod -m 666 dev/null c 1 3 mkdir -vp etc/opt etc/sysconfig mkdir -pv lib${LIBDIRSUFFIX}/firmware mkdir -pv media/floppy media/cdrom mkdir -pv usr/{,local/}{bin,include,lib${LIBDIRSUFFIX},sbin,src} mkdir -pv usr/{,local/}share/{color,dict,doc,info,locale,man} mkdir -pv usr/{,local/}share/{misc,terminfo,zoneinfo} mkdir -pv usr/{,local/}share/man/man{1..8} mkdir -pv var/{cache,local,log,mail,opt,spool} mkdir -pv var/lib/{color,misc,locate} ln -sfnv /run var/run ln -sfnv /run/lock var/lock install -dv -m 0750 root install -dv -m 1777 tmp var/tmp for dir in usr usr/local do ln -sfnv share/{man,doc,info} $dir done ln -sfnv /proc/self/mounts etc/mtab if [ "$USESYSTEMD" -eq 1 ];then SYSTEMDUSERS="systemd-bus-proxy:x:72:72:systemd Bus Proxy:/:/bin/false systemd-journal-gateway:x:73:73:systemd Journal Gateway:/:/bin/false systemd-journal-remote:x:74:74:systemd Journal Remote:/:/bin/false systemd-journal-upload:x:75:75:systemd Journal Upload:/:/bin/false systemd-network:x:76:76:systemd Network Management:/:/bin/false systemd-resolve:x:77:77:systemd Resolver:/:/bin/false systemd-timesync:x:78:78:systemd Time Synchronization:/:/bin/false systemd-coredump:x:79:79:systemd Core Dumper:/:/bin/false " SYSTEMDGROUPS="systemd-bus-proxy:x:72: systemd-journal-gateway:x:73: systemd-journal-remote:x:74: systemd-journal-upload:x:75: systemd-network:x:76: systemd-resolve:x:77: systemd-timesync:x:78: systemd-coredump:x:79:" fi cat > etc/passwd << EOF root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/dev/null:/bin/false daemon:x:6:6:Daemon User:/dev/null:/bin/false messagebus:x:18:18:D-Bus Message Daemon User:/var/run/dbus:/bin/false $SYSTEMDUSERS uuidd:x:80:80:UUID Generation Daemon User:/dev/null:/bin/false nobody:x:99:99:Unprivileged User:/dev/null:/bin/false EOF cat > etc/group << EOF root:x:0: bin:x:1:daemon sys:x:2: kmem:x:3: tape:x:4: tty:x:5: daemon:x:6: floppy:x:7: disk:x:8: lp:x:9: dialout:x:10: audio:x:11: video:x:12: utmp:x:13: usb:x:14: cdrom:x:15: adm:x:16: console:x:17 messagebus:x:18: input:x:24: mail:x:34: cdrw:x:40 news:x:41 scanner:x:42 network:x:43 plugdev:x:44 power:x:45 kvm:x:61: $SYSTEMDGROUPS wheel:x:97 nogroup:x:99: users:x:999 EOF cat > etc/resolv.conf << EOF # Begin /etc/resolv.conf domain local $(for i in $NAMESERVERS;do echo nameserver $i;done) # End /etc/resolv.conf EOF cat > etc/hosts << EOF # Begin /etc/hosts (network card version) 127.0.0.1 localhost $IP $LFSNAME $LFSNAME.local # End /etc/hosts (network card version) EOF cat > etc/profile << EOF # Begin /etc/profile export LANG=$LANGUAGE pathremove () { local IFS=':' local NEWPATH local DIR local PATHVARIABLE=\${2:-PATH} for DIR in \${!PATHVARIABLE} do if [ "\$DIR" != "\$1" ];then NEWPATH=\${NEWPATH:+\$NEWPATH:}\$DIR fi done export \$PATHVARIABLE="\$NEWPATH" } pathprepend () { pathremove \$1 \$2 local PATHVARIABLE=\${2:-PATH} export \$PATHVARIABLE="\$1\${!PATHVARIABLE:+:\${!PATHVARIABLE}}" } pathappend () { pathremove \$1 \$2 local PATHVARIABLE=\${2:-PATH} export \$PATHVARIABLE="\${!PATHVARIABLE:+\${!PATHVARIABLE}:}\$1" } export -f pathremove pathprepend pathappend # Set the initial path export PATH=/bin:/usr/bin export HISTSIZE=1000 export HISTIGNORE="&:[bf]g:exit" if [ \$EUID -eq 0 ];then pathappend /sbin:/usr/sbin #unset HISTFILE fi export XDG_DATA_DIRS=\${XDG_DATA_DIRS:-/usr/share/} export XDG_CONFIG_DIRS=\${XDG_CONFIG_DIRS:-/etc/xdg/} export XDG_RUNTIME_DIR=\${XDG_RUNTIME_DIR:-/tmp/xdg-\$USER} # Setup a red prompt for root and a green one for users. NORMAL="\[\e[0m\]" RED="\[\e[1;31m\]" GREEN="\[\e[1;32m\]" if [[ \$EUID == 0 ]] ; then PS1=''\${RED}'\u@\h [ '\${NORMAL}'\w '\${RED}']$'\${NORMAL}' ' else PS1=''\${GREEN}'\u@\h [ '\${NORMAL}'\w '\${GREEN}']$'\${NORMAL}' ' fi for script in /etc/profile.d/*.sh do if [ -r \$script ];then . \$script fi done unset script RED GREEN NORMAL # End /etc/profile EOF cat > etc/inputrc << "EOF" # Begin /etc/inputrc # Modified by Chris Lynn # Allow the command prompt to wrap to the next line set horizontal-scroll-mode Off # Enable 8bit input set meta-flag On set input-meta On # Turns off 8th bit stripping set convert-meta Off # Keep the 8th bit for display set output-meta On # none, visible or audible set bell-style none # All of the following map the escape sequence of the value # contained in the 1st argument to the readline specific functions "\eOd": backward-word "\eOc": forward-word # for linux console "\e[1~": beginning-of-line "\e[4~": end-of-line "\e[5~": beginning-of-history "\e[6~": end-of-history "\e[3~": delete-char "\e[2~": quoted-insert # for xterm "\eOH": beginning-of-line "\eOF": end-of-line # for Konsole "\e[H": beginning-of-line "\e[F": end-of-line # End /etc/inputrc EOF swapline="$(lsblk -no UUID $SWAP 2>/dev/null)" && swapline="UID=$(lsblk -no UUID $SWAP 2>/dev/null)" || [ -n $SWAP ] && [ -e "$SWAP" ] && swapline="$SWAP" || swapline="#" cat > etc/fstab << EOF # Begin /etc/fstab #file system mount-point type options dump fsck order UUID=$(findmnt -no UUID $LFS) / $ROOTFSTYPE defaults 1 1 $swapline swap swap pri=1 0 0 proc /proc proc nosuid,noexec,nodev 0 0 sysfs /sys sysfs nosuid,noexec,nodev 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 tmpfs /run tmpfs defaults 0 0 devtmpfs /dev devtmpfs mode=0755,nosuid 0 0 tmpfs /dev/shm tmpfs nosuid,nodev 0 0 # End /etc/fstab EOF if [ "$USESYSTEMD" -eq 0 ];then mkdir -vp etc/sysconfig cat > etc/sysconfig/ifconfig.eth0 << EOF ONBOOT=yes IFACE=eth0 SERVICE=ipv4-static IP=$IP GATEWAY=$GATEWAY PREFIX=24 BROADCAST=${SUBNET} EOF cat > etc/inittab << "EOF" # Begin /etc/inittab id:3:initdefault: si::sysinit:/etc/rc.d/init.d/rc S l0:0:wait:/etc/rc.d/init.d/rc 0 l1:S1:wait:/etc/rc.d/init.d/rc 1 l2:2:wait:/etc/rc.d/init.d/rc 2 l3:3:wait:/etc/rc.d/init.d/rc 3 l4:4:wait:/etc/rc.d/init.d/rc 4 l5:5:wait:/etc/rc.d/init.d/rc 5 l6:6:wait:/etc/rc.d/init.d/rc 6 ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now su:S06:once:/sbin/sulogin s1:1:respawn:/sbin/sulogin 1:2345:respawn:/sbin/agetty --noclear tty1 9600 2:2345:respawn:/sbin/agetty tty2 9600 3:2345:respawn:/sbin/agetty tty3 9600 4:2345:respawn:/sbin/agetty tty4 9600 5:2345:respawn:/sbin/agetty tty5 9600 6:2345:respawn:/sbin/agetty tty6 9600 # End /etc/inittab EOF echo $LFSVERSION > etc/lfs-release cat > etc/lsb-release << EOF DISTRIB_ID="Linux From Scratch" DISTRIB_RELEASE="$LFSVERSION" DISTRIB_CODENAME="$LFSNAME" DISTRIB_DESCRIPTION="Linux From Scratch" EOF echo "HOSTNAME=$LFSNAME" > etc/sysconfig/network cat > etc/sysconfig/clock << "EOF" # Begin /etc/sysconfig/clock UTC=1 # Set this to any options you might need to give to hwclock, # such as machine hardware clock type for Alphas. CLOCKPARAMS= # End /etc/sysconfig/clock EOF cat > etc/sysconfig/console << EOF # Begin /etc/sysconfig/console KEYMAP=$KEYBOARD #FONT="lat2a-16 -m 8859-2" # End /etc/sysconfig/console EOF else . LFSScripts/SystemDFiles fi cat > etc/shells << EOF # Begin /etc/shells /bin/sh /bin/bash # End /etc/shells EOF if [ ! -e etc/lfspkg.rc ];then cat > etc/lfspkg.rc << EOF LFSVERSION=$LFSVERSION KERNELVERSION=$KERNELVERSION LIBDIRSUFFIX=$LIBDIRSUFFIX SCRIPTLOG=1 OUTPUT=$PKGARCHIVES SOURCEARCHIVES=$SOURCEARCHIVES BUILDSCRIPTS=$BUILDSCRIPTS COMPILEAT=$COMPILEAT CONFIGOPTIONS="--disable-option-checking --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/libexec --libdir=/usr/lib\$LIBDIRSUFFIX --disable-debug --disable-nls --disable-static" MESONCONFIGOPTIONS="--prefix=/usr --sysconfdir=/etc --libexecdir=/usr/libexec --libdir=/usr/lib\$LIBDIRSUFFIX --buildtype=release --default-library=shared" MAKEFLAGS=" -j4 " CHECKETC=0 EOF fi mkdir -vp etc/ld.so.conf.d ||true cat > etc/ld.so.conf << EOF # Begin /etc/ld.so.conf /lib${LIBDIRSUFFIX} /usr/lib${LIBDIRSUFFIX} /usr/local/lib /opt/lib include /etc/ld.so.conf.d/*.conf EOF #Set the perms for $LFS will fail for any mounted network tha can't have their perms changed ( eg a samba mount ). #chown -R root:root $LFS/* 2>/dev/null||true touch var/log/{btmp,lastlog,wtmp} chmod -v 664 var/log/lastlog chmod -v 600 var/log/btmp chgrp -v utmp var/log/lastlog chmod 777 tmp popd