# HG changeset patch # User Paul Boddie # Date 1560123466 -7200 # Node ID eeb7bcd53897dab399e54ce2a4a96c823514a068 # Parent 24240d76624ccea9290877361a7fa1c498058c2a Switched to a more structured system definition format. Eliminated partition summaries since the expanded definitions together with the filtering of definitions make such summaries largely redundant. diff -r 24240d76624c -r eeb7bcd53897 docs/COPYING.txt --- a/docs/COPYING.txt Tue May 28 00:30:18 2019 +0200 +++ b/docs/COPYING.txt Mon Jun 10 01:37:46 2019 +0200 @@ -19,9 +19,9 @@ Licence Details for makesd-defs ------------------------------- -The makesd-defs file is extracted from the original makesd tool, modified to -work with the makesd-expand-def script. The following applies to the original -content: +The makesd-defs file is based on content extracted from the original makesd +tool, modified to work with the makesd-expand-def script. The following +applies to the original content: (c) Golden Delicious Comp. GmbH&Co. KG, 2014-2018 Licenced under GPLv2 diff -r 24240d76624c -r eeb7bcd53897 makesd --- a/makesd Tue May 28 00:30:18 2019 +0200 +++ b/makesd Mon Jun 10 01:37:46 2019 +0200 @@ -23,8 +23,8 @@ COMMON="$THISDIR/makesd-common" DEFS="$THISDIR/makesd-defs" EXPAND="$THISDIR/makesd-expand-def" +FILTER="$THISDIR/makesd-filter-def" FORMAT="$THISDIR/makesd-format" -PARTSUMMARY="$THISDIR/makesd-partition-summary" PARTITION="$THISDIR/makesd-partition" . "$COMMON" @@ -35,16 +35,15 @@ if [ "$1" = '--help' ] ; then cat 1>&2 < +Usage: $PROGNAME [ --align ] $PROGNAME -a | --all | --defs Partition, format, populate and initialise a device for use with a particular -kind of hardware. The given definition name is used to search the database of +kind of hardware. The given system name is used to search the database of supported hardware devices and to configure the different preparation activities. -If the -a, --all or --defs options are specified, all hardware definitions are -emitted and no further actions are taken. +$(show_definitions_description) $(align_cylinder_description) EOF @@ -54,7 +53,7 @@ # Test for the definition output options. if [ "$1" = '-a' ] || [ "$1" = '--all' ] || [ "$1" = '--defs' ] ; then - grep '^[[:alpha:]]' "$DEFS" | cut -f1 | sort + "$EXPAND" "$1" exit 0 fi @@ -62,8 +61,6 @@ check_device -SUMMARY="`basename "$DEV"`.summary" - # Test for the cylinder alignment option. if test_align_cylinder $* ; then shift 1 ; fi @@ -79,28 +76,14 @@ exit 1 fi -shift 1 - -# Expand the definition to partitioning options. - -if ! ARGS=`"$EXPAND" "$DEF"` ; then - cat 1>&2 < "$SUMMARY" - # Partition the given device, passing only partition details to the script. - cut -f 1,2,3 "$SUMMARY" \ + "$EXPAND" "$DEF" \ +| "$FILTER" "partition" "position" "size" "type" \ | "$PARTITION" $ALIGN_CYLINDER # Format the given device, passing only partition types to the script. - cut -f 3 "$SUMMARY" \ + "$EXPAND" "$DEF" \ +| "$FILTER" "partition" "type" \ | "$FORMAT" diff -r 24240d76624c -r eeb7bcd53897 makesd-common --- a/makesd-common Tue May 28 00:30:18 2019 +0200 +++ b/makesd-common Mon Jun 10 01:37:46 2019 +0200 @@ -36,11 +36,101 @@ fi } -# Process partition field values in summary tables. +# filter [ ... ] +# +# Filter input, preserving only sections with the given type, preserving only +# the given properties if indicated. + +filter() +{ + local FIELD LINE PROPERTY SECTION TYPE + + TYPE=$1 + SECTION= + + shift 1 + + while read LINE ; do + + # Start a new section if appropriate. + + if [ "$LINE" ] && [ ! "$SECTION" ] ; then + SECTION=`echo "$LINE" | cut -d' ' -f1` + + # Produce suitable section headers. + + if [ "$SECTION" = "$TYPE" ] ; then + echo "$LINE" + fi + + continue + fi + + # Produce lines from any suitable section including any blank line. + + if [ "$SECTION" = "$TYPE" ] ; then + + # Select appropriate properties if indicated. + + if [ "$LINE" ] && [ "$1" ] ; then + FIELD=`echo "$LINE" | cut -d: -f1` + + for PROPERTY in $* ; do + if [ "$PROPERTY" = "$FIELD" ] ; then + echo "$LINE" + break + fi + done + + # Otherwise, select all properties. -read_fields() + else + echo "$LINE" + fi + fi + + # End sections on blank lines. + + if [ ! "$LINE" ] ; then + SECTION= + fi + done +} + +# get_field +# +# Emit the field name of a property definition. + +get_field() { - IFS=`echo -n '\t'` read START SIZE TYPE + echo "$1" | cut -d: -f1 +} + +# get_value +# +# Emit the value of a property definition. + +get_value() +{ + echo "$1" | cut -d: -f2 | sed 's/^\s*//' +} + +# search +# +# Search for the given string in the input, returning the line number. + +search() +{ + grep -h -n "$1" "$2" | cut -d: -f1 +} + +# set_value +# +# Set the parameter of the given name to value. + +set_value() +{ + eval "$1=\"\$2\"" } # test_align_cylinder @@ -58,6 +148,8 @@ fi } +# Common messages. + align_cylinder_description() { cat < # @@ -22,45 +22,17 @@ PROGNAME=`basename "$0"` THISDIR=`dirname "$0"` +COMMON="$THISDIR/makesd-common" DEFS="$THISDIR/makesd-defs" +. "$COMMON" + -# Test for options. - -if [ "$1" = '--original' ] ; then - FLAT=$1 - shift 1 -else - FLAT= -fi - -# Obtain the requested definition name. - -DEF=$1 - -if [ ! "$DEF" ] || [ "$DEF" = '--help' ] ; then - cat 1>&2 < - -Search for a definition of the given name in the definitions file: - -$DEFS - -If the definition can be found, the value of the definition is emitted and an -exit value of 0 returned. Otherwise, no output is produced and an exit value of -1 is returned. -EOF - exit 1 -fi - - - -# lookup +# lookup # -# Search for a definition of the given name in the definitions file. Emit the -# full definition, incorporating multiple lines if continuation characters are -# present. +# Search for the given definition in the definitions file. Emit the full +# definition. lookup() { @@ -68,105 +40,161 @@ # Obtain the line number of the matching definition. - LINENUM=`grep -h -n "^$1\s" "$DEFS" | cut -d: -f1` + LINENUM=`search "^$1$" "$DEFS"` if [ "$LINENUM" ] ; then - # Read from the definition line. Line continuations are observed. + # Read from the definition until a blank line or the end of file is + # encountered. Line continuations are observed. + + tail -n "+$(($LINENUM + 1))" "$DEFS" | while read LINE ; do + + # Detect end of definition. - tail -n "+$LINENUM" "$DEFS" | if read LINE ; then + if [ ! "$LINE" ] ; then + break + fi + + # Show definition lines. + echo "$LINE" - break - fi + done fi } -# match +# consolidate # -# Attempt to match pattern in string, emitting the string if successful. +# Eliminate duplicate properties, preserving only the final definition. + +consolidate() +{ + sort -k1,1 -t: -s | show_last +} + +show_last() +{ + local FIELD LAST LASTLINE + + LAST= + LASTLINE= + + while read LINE ; do + + # Inspect the property details. -match() -{ - if `echo "$1" | grep -q "$2"` ; then - echo "$1" + FIELD=`get_field "$LINE"` + + # With a differing property from any previous one, emit the previous + # one. + + if [ "$LAST" ] && [ "$FIELD" != "$LAST" ] ; then + echo "$LASTLINE" + fi + + # Retain this property definition for possible future emission. + + LAST=$FIELD + LASTLINE=$LINE + done + + # Emit any remaining line, also indicating whether any lines were processed. + + if [ "$LASTLINE" ] ; then + echo "$LASTLINE" return 0 else return 1 fi } -# defname -# -# Return the actual definition name from the given prefixed name. - -defname() -{ - echo "$1" | sed 's/^\$//' -} - -# expand +# expand # # Expand the given definition name to its value, recursively expanding any # definition names found in the value text. expand() { - local FOUND VALUE + local TYPE + + TYPE=`echo "$1" | cut -d' ' -f1` - FOUND= - VALUE= + # Look up the definition and read each line. + + lookup "$1" | expand_def "$TYPE" +} - for WORD in `lookup "$1"`; do - if [ ! "$FOUND" ] ; then - FOUND=$WORD - continue - fi +expand_def() +{ + local FIELD LINE RESULT TYPE VALUE - # Prevent recursive expansion if flat mode is selected. - - if [ ! "$FLAT" ] ; then + RESULT=1 - # Identify definition names by looking for a $ prefix. + while read LINE ; do + RESULT=0 - DEFNAME=`match "$WORD" '^\\$'` + # Inspect the line and find any references to other definitions. - # Attempt to expand definition names. + FIELD=`get_field "$LINE"` + VALUE=`get_value "$LINE"` + + # Merge definitions that are adapted. - if [ "$DEFNAME" ] ; then - DEFNAME=`defname "$DEFNAME"` - EXPANDED=`expand "$DEFNAME"` + if [ "$FIELD" = 'adapts' ] ; then + expand "$TYPE $VALUE" + + # Aggregate system components such as partitions and regions. - if [ "$EXPANDED" ] ; then - WORD=$EXPANDED - fi - fi - fi + elif [ "$TYPE" = 'system' ] && ( + [ "$FIELD" = 'partition' ] || [ "$FIELD" = 'region' ] ) ; then + + # Declare regions and partitions in the output. - # Incorporate expansions into the final value. + echo "$FIELD" + expand "$FIELD $VALUE" | consolidate + echo - if [ ! "$VALUE" ] ; then - VALUE=$WORD + # Otherwise, emit properties of the definition. + else - VALUE="$VALUE $WORD" + echo "$LINE" fi done - # Show the expanded definition value. - - echo "$VALUE" - - # Return a result value of 0 if successful, 1 otherwise. - - if [ "$VALUE" ] ; then - return 0 - else - return 1 - fi + return $RESULT } +# Obtain the requested system name. + +if [ ! "$1" ] || [ "$1" = '--help' ] ; then + cat 1>&2 < + $PROGNAME -a | --all | --defs + +Search for a definition of the given system in the definitions file: + +$DEFS + +If the definition can be found, the value of the definition is emitted and an +exit value of 0 returned. Otherwise, no output is produced and an exit value of +1 is returned. + +$(show_definitions_description) +EOF + exit 1 +fi + +# Show definitions if requested. + +if [ "$1" = '-a' ] || [ "$1" = '--all' ] || [ "$1" = '--defs' ] ; then + grep '^system' "$DEFS" | sed 's/^system //' | sort + exit 0 +fi + # Expand the definition and return the result code. -expand "$DEF" +DEF=$1 + +expand "system $DEF" exit $? diff -r 24240d76624c -r eeb7bcd53897 makesd-filter-def --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/makesd-filter-def Mon Jun 10 01:37:46 2019 +0200 @@ -0,0 +1,47 @@ +#!/bin/sh + +# Filter sections of a complete definition. +# +# Copyright (C) 2019 Paul Boddie +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see . + +PROGNAME=`basename "$0"` +THISDIR=`dirname "$0"` + +COMMON="$THISDIR/makesd-common" + +. "$COMMON" + + + +# Obtain the requested system name. + +TYPE=$1 + +if [ ! "$TYPE" ] || [ "$TYPE" = '--help' ] ; then + cat 1>&2 < [ ... ] + +Select sections of the given type from a complete description of a system +provided on standard input. If any properties are given, select only those +properties in the selected sections. +EOF + exit 1 +fi + +# Select sections of the given type, filtering out all other sections. + +filter $* +exit $? diff -r 24240d76624c -r eeb7bcd53897 makesd-format --- a/makesd-format Tue May 28 00:30:18 2019 +0200 +++ b/makesd-format Mon Jun 10 01:37:46 2019 +0200 @@ -62,9 +62,12 @@ cat 1>&2 < + +Types recognised include ext, ext2, ext3, ext4, fat and swap. EOF exit 0 fi @@ -75,10 +78,25 @@ # Read partition types, one per line. -PARTNUM=1 +PARTNUM=0 +SECTION= + +while read LINE ; do + + if [ "$LINE" ] && [ ! "$SECTION" ] ; then + SECTION=$LINE + PARTNUM=$(($PARTNUM + 1)) -while read TYPE ; do - DEVICE="${DEV}${PARTNUM}" - format "$DEVICE" "$TYPE" - PARTNUM=$(($PARTNUM + 1)) + elif [ ! "$LINE" ] ; then + SECTION= + + elif [ "$SECTION" ] ; then + FIELD=`get_field "$LINE"` + VALUE=`get_value "$LINE"` + + if [ "$FIELD" = 'type' ] ; then + DEVICE="${DEV}${PARTNUM}" + format "$DEVICE" "$TYPE" + fi + fi done diff -r 24240d76624c -r eeb7bcd53897 makesd-partition-summary --- a/makesd-partition-summary Tue May 28 00:30:18 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,187 +0,0 @@ -#!/bin/sh - -# Emit a partition summary for a device employing parameters such as the start, -# size, and type. -# -# Copyright (C) 2019 Paul Boddie -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation; either version 3 of the License, or (at your option) any later -# version. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -# details. -# -# You should have received a copy of the GNU General Public License along with -# this program. If not, see . - -PROGNAME=`basename "$0"` -THISDIR=`dirname "$0"` - - - -# Emit any partition details as a complete record. - -emit_partition() -{ - echo "${START:--}\t${SIZE:--}\t${TYPE:--}\t${BOOTLOADER:--}\t" \ - "${CONFIG:--}\t${DEVICETREE:--}\t${KERNEL:--}\t${MODULES:--}\t" \ - "${ROOTFS:--}" -} - -# Reset the current partition details. - -reset_partition() -{ - START= - SIZE= - TYPE= - - BOOTLOADER= - CONFIG= - DEVICETREE= - KERNEL= - MODULES= - ROOTFS= -} - -# Emit the current partition details and proceed to the next partition. - -next_partition() -{ - if [ "$START" ] || [ "$SIZE" ] || [ "$TYPE" ] ; then - emit_partition - reset_partition - fi -} - -# test_partition -# -# Test for the presence of the given value, starting a new partition if already -# defined. - -test_partition() -{ - if [ "$1" ] ; then - next_partition - fi -} - -# replace_in_partition -# -# Set in the partition the parameter of the given name to value. - -replace_in_partition() -{ - eval "$1=\"\$2\"" -} - -# set_in_partition -# -# Set the partition parameter of the given name to value, starting a new -# partition if necessary. - -set_in_partition() -{ - eval "test_partition \$$1" - replace_in_partition "$1" "$2" -} - - - -# Emit the help message if requested. - -if [ "$1" = '--help' ] ; then - cat 1>&2 <... - -Produce partition descriptions, indicating partition type, start position and -size for each partition. Each new occurrence of an active partitioning option -starts a new partition description. - -Partitioning options are as follows: - --f - partition type (ext2, ext3, ext4, fat, swap...) --p - start position/sector --s - partition size as a percentage of device capacity - -Alongside partitioning options, the following population options can be given: - --b - bootloader payload --c - configuration archive --d - device tree binary archive --k - kernel payload --m - kernel modules archive --r - root filesystem archive - -Each line of the produced description is tab-separated with '-' indicating an -empty field. -EOF - exit 0 -fi - -# Process the arguments, building a partition description. - -reset_partition - -while [ "$1" ] ; do - case "$1" in - - # Partition type, position, size. - - -f ) - set_in_partition "TYPE" "$2" - shift 2 - ;; - -p ) - set_in_partition "START" "$2" - shift 2 - ;; - -s ) - set_in_partition "SIZE" "$2" - shift 2 - ;; - - # Bootloader, configuration, device tree, kernel, modules, root - # filesystem. - - -b ) - replace_in_partition "BOOTLOADER" "$2" - shift 2 - ;; - -c ) - replace_in_partition "CONFIG" "$2" - shift 2 - ;; - -dd ) - # NOTE: To be supported. - shift 1 - ;; - -d ) - replace_in_partition "DEVICETREE" "$2" - shift 2 - ;; - -k ) - replace_in_partition "KERNEL" "$2" - shift 2 - ;; - -m ) - replace_in_partition "MODULES" "$2" - shift 2 - ;; - -r ) - replace_in_partition "ROOTFS" "$2" - shift 2 - ;; - * ) - shift 1 - ;; - esac -done - -# Terminate any unfinished partition. - -next_partition diff -r 24240d76624c -r eeb7bcd53897 makesd-partition-table --- a/makesd-partition-table Tue May 28 00:30:18 2019 +0200 +++ b/makesd-partition-table Mon Jun 10 01:37:46 2019 +0200 @@ -202,11 +202,12 @@ Produce partition descriptions, indicating partition type, start position and size for each partition. -The input for this program is supplied via standard input as a partitioning -summary with each line providing a collection of tab-separated values. These -values appear in the following order: +The input for this program is supplied via standard input as partition +definitions employing the following pertinent properties: - +position: +size: +type: Start positions are indicated as numbers of 512-byte sectors. @@ -235,9 +236,34 @@ # Read and emit partition details. -while read_fields ; do +SECTION= + +while read LINE ; do + + if [ "$LINE" ] && [ ! "$SECTION" ] ; then + SECTION=$LINE + + elif [ ! "$LINE" ] ; then + SECTION= + next_partition + + elif [ "$SECTION" ] ; then + FIELD=`get_field "$LINE"` + VALUE=`get_value "$LINE"` + + case "$FIELD" in + ( 'position' ) set_value "START" "$VALUE" ;; + ( 'size' ) set_value "SIZE" "$VALUE" ;; + ( 'type' ) set_value "TYPE" "$VALUE" ;; + esac + fi +done + +# Finish any open section. + +if [ "$SECTION" ] ; then next_partition -done +fi # Emit the last partition.