1 #!/bin/sh 2 3 # Make a test filesystem. 4 # 5 # Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk> 6 # 7 # This program is free software; you can redistribute it and/or 8 # modify it under the terms of the GNU General Public License as 9 # published by the Free Software Foundation; either version 2 of 10 # the License, or (at your option) any later version. 11 # 12 # This program is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with this program; if not, write to the Free Software 19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 20 # Boston, MA 02110-1301, USA 21 22 PROGNAME=$(basename "$0") 23 24 # Handle program options. 25 26 if [ "$1" = '-q' ] ; then 27 QUIET=$1 28 shift 1 29 else 30 QUIET= 31 fi 32 33 PKGDIR=$(realpath "$1") 34 PROGRAMS_DIR=$(realpath "$2") 35 E2ACCESS_DIR=$(realpath "$3") 36 TARGET=$(realpath "$4") 37 38 if [ ! -e "$PKGDIR" ] || [ ! -e "$PROGRAMS_DIR" ] || [ ! -e "$E2ACCESS_DIR" ] || [ ! "$TARGET" ] ; then 39 cat 1>&2 <<EOF 40 Usage: $PROGNAME [ -q ] <package directory> <programs directory> <e2access directory> <target> 41 42 Package directory: $PKGDIR 43 Programs directory: $PROGRAMS_DIR 44 e2access directory: $E2ACCESS_DIR 45 Target filesystem: $TARGET 46 EOF 47 exit 1 48 fi 49 50 # Initialise the program details and environment. 51 52 E2ACCESS="$E2ACCESS_DIR"/e2access 53 export LD_LIBRARY_PATH="$E2ACCESS_DIR" 54 55 OPTIONS='-u 1000 -g 1000' 56 57 # Define a convenience function for e2access invocation. 58 59 e2access() 60 { 61 "$E2ACCESS" $OPTIONS "$TARGET" $* 62 } 63 64 # Make a filesystem for the example. 65 66 mkdir tmp_e2test 67 cd tmp_e2test 68 69 # Set a sensible umask. 70 71 umask 0022 72 73 mkdir -p home/paulb/public 74 echo "Public!" > home/paulb/public/message 75 76 # Set a restrictive umask. 77 78 umask 0077 79 80 echo "Not public!" > home/paulb/public/message2 81 82 mkdir -p home/paulb/private 83 echo "Private!" > home/paulb/private/message 84 85 # Set a permissive umask. 86 87 umask 0000 88 89 mkdir -p home/paulb/shared 90 echo "Editable!" > home/paulb/shared/message 91 92 # Restore the umask for the remaining files. 93 94 umask 0022 95 96 mkdir -p home/paulb/many 97 cd home/paulb/many 98 99 # Populate the directory with plenty of files. 100 101 for N in `seq 1 400`; do 102 echo "Contents of #$N." > "file-$N".txt 103 done 104 105 cd .. 106 107 # Put file in the created home directory. 108 109 cp "$PKGDIR/../docs/LICENCE.txt" . 110 111 # Put some programs in the same place. 112 113 cp "$PROGRAMS_DIR/dstest_"* . 114 115 # Leave the filesystem root. 116 117 cd ../.. 118 119 # Create a filesystem image. 120 121 EXTRA=2000 122 SIZE=$(du -s -k home | cut -f 1) 123 TOTAL=$(($SIZE + $EXTRA)) 124 125 dd if=/dev/zero of="$TARGET" bs=1024 count=$TOTAL 126 127 if ! $(/sbin/mkfs.ext2 -q "$TARGET") ; then 128 exit 1 129 fi 130 131 # Add the directories and files to the image. 132 133 for DIR in home/paulb home/paulb/private home/paulb/public home/paulb/shared home/paulb/many ; do 134 e2access mkdir "$DIR" 135 e2access copy-in $(find "$DIR" -maxdepth 1 -type f | sort) "$DIR" 136 done 137 138 # Leave the root of the filesystem. 139 140 cd .. 141 142 if [ ! "$QUIET" ] ; then 143 e2access ls '' 144 e2access ls 'home' 145 e2access ls 'home/paulb' 146 e2access ls 'home/paulb/many' 147 e2access ls 'home/paulb/private' 148 e2access ls 'home/paulb/public' 149 e2access ls 'home/paulb/shared' 150 fi 151 152 rm -r tmp_e2test 153 154 # vim: tabstop=4 expandtab shiftwidth=4