Add option to activate serial console

Resolves #6

Co-Authored-By: Jakub Jirutka <jakub@jirutka.cz>
I just refactored the code.
This commit is contained in:
Oliver Frommel 2018-09-30 08:03:32 +02:00 committed by Jakub Jirutka
parent b4e289fd4b
commit 6c4201fcd1

View file

@ -60,6 +60,8 @@
# -c --script-chroot (SCRIPT_CHROOT) Bind <script>'s directory at /mnt inside image and chroot
# into the image before executing <script>.
#
# -t --serial-console (SERIAL_CONSOLE) Add configuration for a serial console on ttyS0.
#
# -h --help Show this help message and exit.
#
# -v --version Print version and exit.
@ -247,7 +249,11 @@ setup_extlinux() {
local root_dev="$2" # root device
local modules="$3" # modules which should be loaded before pivot_root
local kernel_flavor="$4" # name of default kernel to boot
local serial_port="$5" # serial port number for serial console
local default_kernel="$kernel_flavor"
local kernel_opts=''
[ -z "$serial_port" ] || kernel_opts="console=$serial_port"
if [ "$kernel_flavor" = 'virt' ]; then
_apk search --root . --exact --quiet linux-lts | grep -q . \
@ -255,10 +261,12 @@ setup_extlinux() {
|| default_kernel='vanilla'
fi
sed -Ei -e "s|^[# ]*(root)=.*|\1=$root_dev|" \
-e "s|^[# ]*(default_kernel_opts)=.*|\1=\"\"|" \
sed -Ei \
-e "s|^[# ]*(root)=.*|\1=$root_dev|" \
-e "s|^[# ]*(default_kernel_opts)=.*|\1=\"$kernel_opts\"|" \
-e "s|^[# ]*(modules)=.*|\1=\"$modules\"|" \
-e "s|^[# ]*(default)=.*|\1=$default_kernel|" \
-e "s|^[# ]*(serial_port)=.*|\1=$serial_port|" \
"$mnt"/etc/update-extlinux.conf
chroot "$mnt" extlinux --install /boot
@ -303,8 +311,8 @@ wgets() (
#============================= M a i n ==============================#
opts=$(getopt -n $PROGNAME -o b:cCf:hi:k:p:r:s:v \
-l branch:,image-format:,image-size:,initfs-features:,kernel-flavor:,keys-dir:,mirror-uri:,no-cleanup,packages:,repositories-file:,rootfs:,script-chroot,help,version \
opts=$(getopt -n $PROGNAME -o b:cCf:hi:k:p:r:s:tv \
-l branch:,image-format:,image-size:,initfs-features:,kernel-flavor:,keys-dir:,mirror-uri:,no-cleanup,packages:,repositories-file:,rootfs:,script-chroot,serial-console,help,version \
-- "$@") || help 1 >&2
eval set -- "$opts"
@ -322,6 +330,7 @@ while [ $# -gt 0 ]; do
-p | --packages) PACKAGES="${PACKAGES:-} $2";;
-r | --repositories-file) REPOS_FILE="$(realpath "$2")";;
--rootfs) ROOTFS="$2";;
-t | --serial-console) SERIAL_CONSOLE='yes'; n=1;;
-c | --script-chroot) SCRIPT_CHROOT='yes'; n=1;;
-h | --help) help 0;;
-V | --version) echo "$PROGNAME $VERSION"; exit 0;;
@ -342,6 +351,7 @@ done
: ${REPOS_FILE:="/etc/apk/repositories"}
: ${ROOTFS:="ext4"}
: ${SCRIPT_CHROOT:="no"}
: ${SERIAL_CONSOLE:="no"}
if [ -f /etc/alpine-release ]; then
: ${INSTALL_HOST_PKGS:="yes"}
@ -349,6 +359,9 @@ else
: ${INSTALL_HOST_PKGS:="no"}
fi
SERIAL_PORT=
[ "$SERIAL_CONSOLE" = 'no' ] || SERIAL_PORT='ttyS0'
[ $# -ne 0 ] || help 1 >&2
IMAGE_FILE="$1"; shift
@ -447,13 +460,19 @@ _apk add --root . linux-$KERNEL_FLAVOR
einfo 'Setting up extlinux bootloader'
_apk add --root . --no-scripts syslinux
setup_extlinux . "UUID=$root_uuid" "$ROOTFS" "$KERNEL_FLAVOR"
setup_extlinux . "UUID=$root_uuid" "$ROOTFS" "$KERNEL_FLAVOR" "$SERIAL_PORT"
cat > etc/fstab <<-EOF
# <fs> <mountpoint> <type> <opts> <dump/pass>
UUID=$root_uuid / $ROOTFS noatime 0 1
EOF
#-----------------------------------------------------------------------
if [ "$SERIAL_PORT" ]; then
echo "$SERIAL_PORT" >> "$mount_dir"/etc/securetty
sed -Ei "s|^[# ]*($SERIAL_PORT:.*)|\1|" "$mount_dir"/etc/inittab
fi
#-----------------------------------------------------------------------
einfo 'Enabling base system services'