1
0
mirror of https://github.com/krglaws/MyLFS.git synced 2025-08-13 14:38:10 +00:00

Merge pull request #4 from krglaws/bios_uefi_choice

BIOS/UEFI choice
This commit is contained in:
Kyle Glaws
2022-04-14 20:45:22 -04:00
committed by GitHub
12 changed files with 212 additions and 70 deletions

129
build.sh
View File

@ -19,6 +19,8 @@ function usage {
"\n" \ "\n" \
" -V|--verbose The script will output more information where applicable.\n" \ " -V|--verbose The script will output more information where applicable.\n" \
"\n" \ "\n" \
" -f|--uefi Build LFS with UEFI boot instead of the default BIOS boot.\n" \
"\n" \
" -e|--check Output LFS dependency version information, then exit.\n" \ " -e|--check Output LFS dependency version information, then exit.\n" \
" It is recommended that you run this before proceeding\n" \ " It is recommended that you run this before proceeding\n" \
" with the rest of the build.\n" \ " with the rest of the build.\n" \
@ -27,7 +29,7 @@ function usage {
" exit.\n" \ " exit.\n" \
"\n" \ "\n" \
" -i|--init Create the .img file, partition it, setup basic directory\n" \ " -i|--init Create the .img file, partition it, setup basic directory\n" \
" structure, then exit." \ " structure, then exit.\n" \
"\n" \ "\n" \
" -p|--start-phase\n" \ " -p|--start-phase\n" \
" -a|--start-package Select a phase and optionally a package\n" \ " -a|--start-package Select a phase and optionally a package\n" \
@ -133,15 +135,15 @@ function check_dependencies {
} }
function install_static { function install_static {
FILENAME=$1 local FILENAME=$1
FULLPATH="$LFS/$(basename $FILENAME | sed 's/__/\//g')" local FULLPATH="$LFS/$(basename $FILENAME | sed 's/__/\//g')"
mkdir -p $(dirname $FULLPATH) mkdir -p $(dirname $FULLPATH)
cp $FILENAME $FULLPATH cp $FILENAME $FULLPATH
} }
function install_template { function install_template {
FILENAME=$1 local FILENAME=$1
FULLPATH="$LFS/$(basename $FILENAME | sed 's/__/\//g')" local FULLPATH="$LFS/$(basename $FILENAME | sed 's/__/\//g')"
mkdir -p $(dirname $FULLPATH) mkdir -p $(dirname $FULLPATH)
cp $FILENAME $FULLPATH cp $FILENAME $FULLPATH
shift shift
@ -177,40 +179,49 @@ function init_image {
echo -n "Creating image file... " echo -n "Creating image file... "
trap "echo 'init failed.' && exit 1" ERR
if $VERBOSE
then
set -x
fi
# create image file # create image file
fallocate -l$LFS_IMG_SIZE $LFS_IMG fallocate -l$LFS_IMG_SIZE $LFS_IMG
# hopefully banish any ghost images
dd if=/dev/zero of=$LFS_IMG bs=1M count=1 conv=notrunc &> /dev/null
# attach loop device # attach loop device
LOOP=$(losetup -f) export LOOP=$(losetup -f)
losetup $LOOP $LFS_IMG losetup $LOOP $LFS_IMG
# partition the device # partition the device
FDISK_STR=" if $UEFI
g # create GPT then
n # new partition FDISK_INSTR=$FDISK_INSTR_UEFI
# default 1st partition else
# default start sector (2048) FDISK_INSTR=$FDISK_INSTR_BIOS
+512M # 512 MiB fi
t # modify parition type
uefi # EFI type # remove spaces and comments
n # new partition FDISK_INSTR=$(echo "$FDISK_INSTR" | sed 's/ *#.*//')
# default 2nd partition
# default start sector
# default end sector
w # write to device and quit
"
FDISK_STR=$(echo "$FDISK_STR" | sed 's/ *//g' | sed 's/#.*//')
# fdisk fails to get kernel to re-read the partition table # fdisk fails to get kernel to re-read the partition table
# so ignore non-zero exit code, and manually re-read # so ignore non-zero exit code, and manually re-read
trap - ERR
set +e set +e
echo "$FDISK_STR" | fdisk $LOOP &> /dev/null echo "$FDISK_INSTR" | fdisk $LOOP &> /dev/null
set -e set -e
trap "echo 'init failed.' && unmount_image && exit 1" ERR
# reattach loop device to re-read partition table # reattach loop device to re-read partition table
losetup -d $LOOP losetup -d $LOOP
sleep 1 # give the kernel a sec sleep 1 # give the kernel a sec
losetup -P $LOOP $LFS_IMG losetup -P $LOOP $LFS_IMG
if $UEFI
then
LOOP_P1=${LOOP}p1 LOOP_P1=${LOOP}p1
LOOP_P2=${LOOP}p2 LOOP_P2=${LOOP}p2
@ -227,6 +238,17 @@ function init_image {
# label the partitions # label the partitions
dosfslabel $LOOP_P1 $LFSEFILABEL &> /dev/null dosfslabel $LOOP_P1 $LFSEFILABEL &> /dev/null
e2label $LOOP_P2 $LFSROOTLABEL e2label $LOOP_P2 $LFSROOTLABEL
else
LOOP_P1=${LOOP}p1
# setup root partition
mkfs -t $LFS_FS $LOOP_P1 &> /dev/null
mkdir -p $LFS
mount $LOOP_P1 $LFS
e2label $LOOP_P1 $LFSROOTLABEL
fi
rm -rf $LFS/lost+found
echo "done." echo "done."
@ -273,9 +295,14 @@ function init_image {
fi fi
# install templates # install templates
install_template ./templates/boot__grub__grub.cfg LFSROOTLABEL
install_template ./templates/etc__fstab LFSROOTLABEL LFSEFILABEL LFSFSTYPE
install_template ./templates/etc__hosts LFSHOSTNAME install_template ./templates/etc__hosts LFSHOSTNAME
if $UEFI
then
install_template ./templates/etc__fstab LFSROOTLABEL LFSEFILABEL LFSFSTYPE
else
install_template ./templates/etc__fstab LFSROOTLABEL LFSFSTYPE
sed -i "s/.*LFSEFILABEL.*//" $LFS/etc/fstab
fi
# make special device files # make special device files
mknod -m 600 $LFS/dev/console c 5 1 mknod -m 600 $LFS/dev/console c 5 1
@ -298,13 +325,16 @@ function init_image {
mkdir -p $LFS/$(readlink $LFS/dev/shm) mkdir -p $LFS/$(readlink $LFS/dev/shm)
fi fi
set +x
trap - ERR
echo "done." echo "done."
} }
function cleanup_cancelled_download { function cleanup_cancelled_download {
local PKG=$PACKAGE_DIR/$(basename $1) local PKG=$PACKAGE_DIR/$(basename $1)
[ -f $PKG ] && rm $PKG [ -f $PKG ] && rm -f $PKG
exit
} }
function download_pkgs { function download_pkgs {
@ -315,7 +345,7 @@ function download_pkgs {
for url in $PACKAGE_URLS for url in $PACKAGE_URLS
do do
trap "{ cleanup_cancelled_download $url; }" ERR SIGINT trap "{ cleanup_cancelled_download $url; exit }" ERR SIGINT
$VERBOSE && echo -n "Downloading '$url'... " $VERBOSE && echo -n "Downloading '$url'... "
if ! echo $ALREADY_DOWNLOADED | grep $(basename $url) > /dev/null if ! echo $ALREADY_DOWNLOADED | grep $(basename $url) > /dev/null
@ -347,9 +377,12 @@ function mount_image {
unmount_image unmount_image
# attach loop device # attach loop device
LOOP=$(losetup -f) export LOOP=$(losetup -f)
LOOP_P1=${LOOP}p1 LOOP_P1=${LOOP}p1
LOOP_P2=${LOOP}p2 LOOP_P2=${LOOP}p2
if $UEFI
then
losetup -P $LOOP $LFS_IMG losetup -P $LOOP $LFS_IMG
sleep 1 # give the kernel a sec sleep 1 # give the kernel a sec
@ -358,6 +391,12 @@ function mount_image {
# mount boot partition # mount boot partition
mount -t vfat $LOOP_P1 $LFS/boot/efi mount -t vfat $LOOP_P1 $LFS/boot/efi
else
losetup -P $LOOP $LFS_IMG
sleep 1
mount $LOOP_P1 $LFS
fi
# mount stuff from the host onto the target disk # mount stuff from the host onto the target disk
mount --bind /dev $LFS/dev mount --bind /dev $LFS/dev
@ -415,15 +454,10 @@ function build_package {
pushd $LFS > /dev/null pushd $LFS > /dev/null
if $CHROOT if $CHROOT
then then
if ! chroot "$LFS" /usr/bin/env -i \ if ! chroot "$LFS" /usr/bin/env \
HOME=/root \ HOME=/root \
TERM=$TERM \ TERM=$TERM \
PATH=/usr/bin:/usr/sbin &> $LOG_FILE \ PATH=/usr/bin:/usr/sbin \
MAKEFLAGS=$MAKEFLAGS \
ROOT_PASSWD=$ROOT_PASSWD \
RUN_TESTS=$RUN_TESTS \
KERNELVERS=$KERNELVERS \
$(cat $PACKAGE_LIST) \
/usr/bin/bash +h -c "$BUILD_INSTR" &> $LOG_FILE /usr/bin/bash +h -c "$BUILD_INSTR" &> $LOG_FILE
then then
echo -e "\nERROR: $NAME Phase $PHASE failed:" echo -e "\nERROR: $NAME Phase $PHASE failed:"
@ -484,6 +518,11 @@ function build_phase {
CHROOT=true CHROOT=true
fi fi
if [ $PHASE -eq 5 ]
then
PHASE=5_$({ $UEFI && echo "uefi"; } || echo "bios")
fi
while read pkg while read pkg
do do
if $FOUNDSTARTPKG && $ONEOFF if $FOUNDSTARTPKG && $ONEOFF
@ -544,7 +583,7 @@ function clean_image {
# delete logs # delete logs
if [ -n "$(ls ./logs)" ] if [ -n "$(ls ./logs)" ]
then then
rm -rf ./logs/*log ./logs/*gz rm -rf ./logs/*
fi fi
} }
@ -559,6 +598,9 @@ source ./config.sh
ONEOFF=false ONEOFF=false
VERBOSE=false VERBOSE=false
# exported because it's used by linux.sh
export UEFI=false
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case $1 in case $1 in
-v|--version) -v|--version)
@ -569,6 +611,10 @@ while [ $# -gt 0 ]; do
VERBOSE=true VERBOSE=true
shift shift
;; ;;
-f|--uefi)
UEFI=true
shift
;;
-e|--check) -e|--check)
check_dependencies check_dependencies
exit exit
@ -626,7 +672,7 @@ while [ $# -gt 0 ]; do
done done
if [ -n "$STARTPHASE" ] && if [ -n "$STARTPHASE" ] &&
[ "$STARTPHASE" != 1 -a "$STARTPHASE" != 2 -a "$STARTPHASE" != 3 -a "$STARTPHASE" != 4 ] [ "$STARTPHASE" != 1 -a "$STARTPHASE" != 2 -a "$STARTPHASE" != 3 -a "$STARTPHASE" != 4 -a "$STARTPHASE" != 5 ]
then then
echo "ERROR: phase '$STARTPHASE' does not exist" echo "ERROR: phase '$STARTPHASE' does not exist"
exit 1 exit 1
@ -671,7 +717,8 @@ CONFIG_SITE=$LFS/usr/share/config.site
LC_ALL=POSIX LC_ALL=POSIX
export LC_ALL PATH CONFIG_SITE export LC_ALL PATH CONFIG_SITE
trap "{ unmount_image; exit 1; }" ERR SIGINT trap "echo 'build failed.' && cd $FULLPATH && unmount_image && exit 1" ERR
trap "echo 'build cancelled.' && cd $FULLPATH && unmount_image && exit" SIGINT
# ########### # ###########
# Start build # Start build
@ -696,7 +743,11 @@ $ONEOFF && $FOUNDSTARTPHASE && unmount_image && exit
build_phase 4 build_phase 4
# phase 4 cleanup $ONEOFF && $FOUNDSTARTPHASE && unmount_image && exit
build_phase 5
# final cleanup
rm -rf $LFS/tmp/* rm -rf $LFS/tmp/*
find $LFS/usr/lib $LFS/usr/libexec -name \*.la -delete find $LFS/usr/lib $LFS/usr/libexec -name \*.la -delete
find $LFS/usr -depth -name $LFS_TGT\* | xargs rm -rf find $LFS/usr -depth -name $LFS_TGT\* | xargs rm -rf

View File

@ -23,13 +23,38 @@ export LFSEFILABEL=LFSEFI
export LFSFSTYPE=ext4 export LFSFSTYPE=ext4
export KERNELVERS=5.16.9 export KERNELVERS=5.16.9
KEYS="MAKEFLAGS PACKAGE_LIST PACKAGE_DIR LOG_DIR KEEP_LOGS"\ export FDISK_INSTR_BIOS="
" LFS LFS_TGT LFS_FS LFS_IMG LFS_IMG_SIZE ROOT_PASSWD RUN_TESTS TESTLOG_DIR"\ o # create DOS partition table
" LFSHOSTNAME LFSROOTLABEL LFSEFILABEL LFSFSTYPE KERNELVERS" n # new partition
# default partition type (primary)
# default partition number (1)
# default partition start
# default partition end (max)
w # write to device and quit
"
export FDISK_INSTR_UEFI="
g # create GPT
n # new partition
# default 1st partition
# default start sector (2048)
+512M # 512 MiB
t # modify parition type
uefi # EFI type
n # new partition
# default 2nd partition
# default start sector
# default end sector
w # write to device and quit
"
KEYS="MAKEFLAGS PACKAGE_LIST PACKAGE_DIR LOG_DIR KEEP_LOGS LFS LFS_TGT"\
" LFS_FS LFS_IMG LFS_IMG_SIZE ROOT_PASSWD RUN_TESTS TESTLOG_DIR LFSHOSTNAME"\
" LFSROOTLABEL LFSEFILABEL LFSFSTYPE KERNELVERS FDISK_INSTR_BIOS FDISK_INSTR_UEFI"
for KEY in $KEYS for KEY in $KEYS
do do
if [ -z ${!KEY} ] if [ -z "${!KEY}" ]
then then
echo "ERROR: '$KEY' config is not set." echo "ERROR: '$KEY' config is not set."
exit -1 exit -1

View File

@ -54,7 +54,6 @@ diffutils
gawk gawk
findutils findutils
groff groff
# Skipping GRUB MBR build since we are using UEFI
gzip gzip
iproute2 iproute2
kbd kbd
@ -74,10 +73,3 @@ sysvinit
lfsbootscripts lfsbootscripts
linux linux
# UEFI Boot Dependencies
popt
mandoc
efivar
efibootmgr
grub

View File

@ -1,4 +1,15 @@
# LINUX Phase 4 # LINUX Phase 4
function config_on {
local UNCOMMENTED="^${1}=.*\$"
local COMMENTED="^# ${1} .*\$"
sed -E -i "s/${UCOMMENTED}|${COMMENTED}/${1}=y/" ./.config
}
function config_off {
sed -i "s/^${1}=.*$//" ./.config
}
CONFIGFILE=config-$KERNELVERS CONFIGFILE=config-$KERNELVERS
make mrproper make mrproper
@ -8,6 +19,12 @@ then
else else
# if kernel config not provided, use default architecture config # if kernel config not provided, use default architecture config
make defconfig make defconfig
config_off CONFIG_IKHEADERS
config_on CONFIG_FB
config_off CONFIG_UEVENT_HELPER
config_on CONFIG_DEVTMPFS
config_on CONFIG_MODULES
fi fi
make make

View File

@ -0,0 +1 @@
grub

25
phase5_bios/grub.sh Normal file
View File

@ -0,0 +1,25 @@
./configure --prefix=/usr \
--sysconfdir=/etc \
--disable-efiemu \
--disable-werror
make
make install
mv -v /etc/bash_completion.d/grub /usr/share/bash-completion/completions
grub-install $LOOP --target i386-pc
cat > /boot/grub/grub.cfg <<EOF
set default=0
set timeout=5
insmod $LFS_FS
set root=(hd0,1)
menuentry "GNU/Linux, Linux 5.16.9-lfs-11.1" {
search --no-floppy --label $LFSROOTLABEL --set=root
linux /boot/vmlinuz-5.16.9-lfs-11.1 root=LABEL=$LFSROOTLABEL ro
}
EOF

View File

@ -0,0 +1,7 @@
# UEFI Boot Dependencies
popt
mandoc
efivar
efibootmgr
grub

View File

@ -28,3 +28,27 @@ then
exit -1 exit -1
fi fi
cat > /boot/grub/grub.cfg <<EOF
set default=0
set timeout=5
insmod part_gpt
insmod $LFS_FS
set root=(hd0,2)
if loadfont /boot/grub/fonts/unicode.pf2; then
set gfxmode=auto
insmod all_video
terminal_output gfxterm
fi
menuentry "GNU/Linux, Linux 5.16.9-lfs-11.1" {
search --no-floppy --label $LFSROOTLABEL --set=root
linux /boot/vmlinuz-5.16.9-lfs-11.1 root=LABEL=$LFSROOTLABEL ro
}
menuentry "Firmware Setup" {
fwsetup
}
EOF