Flashrd

From lippmann wiki
Jump to: navigation, search

Flashrd is a collection of scripts that create a version of OpenBSD tailored to embedded devices. The filesystem is read only by default, and loads /var into a ram partition. Some tweaks are made to the kernel as well. See more on the maintainers page, http://www.nmedia.net/flashrd/. It's perfect for soekris hardware, which is what I use it for.

I created a basic script to prepare a system for flashrd image building. It gets the right sets and extracts them, makes sure the permissions on the extracted openbsd root are correct, it also gets the relevant source code. all these are tied to the architecture and OpenBSD version currently used to build from.

The script is below:

#!/bin/ksh

flashrdPath="/opt/flashrd/"
sets="base comp etc man xbase"
mirror="http://ftp.belnet.be/pub/OpenBSD"

version=`uname -r`

function get_source
{
  #A quick check for existing data. Things will get messy if this function is called with data already in /usr/src
  if [ "ls -A /usr/src/" ] ;then
    print "The src directory has other stuff in it. Please remove the existing data in /usr/src or install the source by hand."
  else
    version_="`echo $version | sed -e 's/\./_/g' `"
    cd /usr >/dev/null
    cvs -d anoncvs@anoncvs.openbsd.org:/cvs -q get -rOPENBSD_$version_ src
    cd - >/dev/null
  fi
}
#Creating the directory we are starting from.
mkdir -p $flashrdPath/`uname -r`/`uname -p`

#if the source is not in /usr/src there we kick off a download and extract function.
grep -i `uname -s`_`uname -r` /usr/src/CVS/Tag >/dev/null || get_source

versionShort="`echo $version | sed -e 's/\.//g' `"

# creating the sets directory and downloading the sets there.

if [ ! -d "$flashrdPath/`uname -r`/`uname -p`/sets" ];then
  mkdir "$flashrdPath/`uname -r`/`uname -p`/sets"
fi

pkg_info |grep -i wget >/dev/null || pkg_add wget >/dev/null

for i in $sets
do
  if [ ! -f "$flashrdPath/`uname -r`/`uname -p`/sets/${i}${versionShort}.tgz" ];then
    wget -O "$flashrdPath/`uname -r`/`uname -p`/sets/${i}${versionShort}.tgz" "$mirror/`uname -r`/`uname -p`/${i}${versionShort}.tgz" --quiet
  fi
done

#If the permissions are not set right on the passwd file, either the package has not yet been extracted or the
#permissions have not been updated (this happens when extracting to an export, the special permissions don't get applied in that case).

if [ ! -u "$flashrdPath/`uname -r`/`uname -p`/openbsd_base/usr/bin/passwd" ];then

  #If the base directory already exists, we are moving it here.
  if [ -d "$flashrdPath/`uname -r`/`uname -p`/openbsd_base" ]; then
    mv "$flashrdPath/`uname -r`/`uname -p`/openbsd_base" "/opt/flashrd/`uname -r`/`uname -p`/openbsd_base_deleteme"
  fi

  #Creating the base directory.
  if [ ! -d "$flashrdPath/`uname -r`/`uname -p`/openbsd_base" ];then
    mkdir "$flashrdPath/`uname -r`/`uname -p`/openbsd_base"
  fi

  #Extracting the sets. Finding all files in the sets directory and extracting them to the base directory.
  find "$flashrdPath/`uname -r`/`uname -p`/sets/" -type f -exec tar xfpz {} -C "$flashrdPath/`uname -r`/`uname -p`/openbsd_base/" \;

  #using the sets to list the files with special permissions and creating a list of commands to apply these.
  find "$flashrdPath/`uname -r`/`uname -p`/sets/" -type f -exec tar tfvz {} \; |egrep -i '(^...s)|(^......s)|(^.........t)' |awk '{print $1,$9}' |sed -e 's/^...s..s.../chmod gu+s/' |sed -e 's/^......s.../chmod g+s/' |sed -e 's/^...s....../chmod u+s/' | sed -e 's/^.........T/chmod +t/' | sed -e 's/^.........t/chmod +t/' > /opt/flashrd/`uname -r`/`uname -p`/.modifier

  #Iterating through the permission change command list and removing it afterwards.
  cd "$flashrdPath/`uname -r`/`uname -p`/openbsd_base" && sh "$flashrdPath/`uname -r`/`uname -p`/.modifier"  && cd -
  rm "$flashrdPath/`uname -r`/`uname -p`/.modifier"

fi

#Creating the devices if they don't already exist.
if [ ! -c "$flashrdPath/`uname -r`/`uname -p`/openbsd_base/dev/tty00" ];then
  cd "$flashrdPath/`uname -r`/`uname -p`/openbsd_base/dev/" &&  ./MAKEDEV all && cd -
fi