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 E2ACCESS_DIR=$(realpath "$2") 35 TARGET=$(realpath "$3") 36 37 shift 3 38 39 if [ ! -e "$PKGDIR" ] || [ ! -e "$E2ACCESS_DIR" ] || [ ! "$TARGET" ] ; then 40 cat 1>&2 <<EOF 41 Usage: $PROGNAME [ -q ] <package directory> <e2access directory> <target> [ <program> ... ] 42 43 Package directory: $PKGDIR 44 e2access directory: $E2ACCESS_DIR 45 Target filesystem: $TARGET 46 Programs: $* 47 EOF 48 exit 1 49 fi 50 51 # Initialise the program details and environment. 52 53 E2ACCESS="$E2ACCESS_DIR"/e2access 54 export LD_LIBRARY_PATH="$E2ACCESS_DIR" 55 56 OPTIONS='-u 1000 -g 1000' 57 58 # Define a convenience function for e2access invocation. 59 60 e2access() 61 { 62 "$E2ACCESS" $OPTIONS "$TARGET" $* 63 } 64 65 # Make a filesystem for the example. 66 67 mkdir tmp_e2test 68 cd tmp_e2test 69 70 # Set a sensible umask. 71 72 umask 0022 73 74 mkdir -p home/paulb/public 75 echo "Public!" > home/paulb/public/message 76 77 # Set a restrictive umask. 78 79 umask 0077 80 81 echo "Not public!" > home/paulb/public/message2 82 83 mkdir -p home/paulb/private 84 echo "Private!" > home/paulb/private/message 85 86 # Set a permissive umask. 87 88 umask 0000 89 90 mkdir -p home/paulb/shared 91 echo "Editable!" > home/paulb/shared/message 92 93 # Restore the umask for the remaining files. 94 95 umask 0022 96 97 mkdir -p home/paulb/many 98 cd home/paulb/many 99 100 # Populate the directory with plenty of files. 101 102 for N in `seq 1 400`; do 103 echo "Contents of #$N." > "file-$N".txt 104 done 105 106 cd .. 107 108 # Put file in the created home directory. 109 110 cp "$PKGDIR/../docs/LICENCE.txt" . 111 112 # Put some programs in the same place. 113 114 for PROGRAM in $* ; do 115 cp $(realpath "$PROGRAM") . 116 done 117 118 # Leave the filesystem root. 119 120 cd ../.. 121 122 # Create a filesystem image. 123 124 EXTRA=2000 125 SIZE=$(du -s -k home | cut -f 1) 126 TOTAL=$(($SIZE + $EXTRA)) 127 128 dd if=/dev/zero of="$TARGET" bs=1024 count=$TOTAL 129 130 if ! $(/sbin/mkfs.ext2 -q "$TARGET") ; then 131 exit 1 132 fi 133 134 # Add the directories and files to the image. 135 136 for DIR in home/paulb home/paulb/private home/paulb/public home/paulb/shared home/paulb/many ; do 137 e2access mkdir "$DIR" 138 e2access copy-in $(find "$DIR" -maxdepth 1 -type f | sort) "$DIR" 139 done 140 141 # Leave the root of the filesystem. 142 143 cd .. 144 145 if [ ! "$QUIET" ] ; then 146 e2access ls '' 147 e2access ls 'home' 148 e2access ls 'home/paulb' 149 e2access ls 'home/paulb/many' 150 e2access ls 'home/paulb/private' 151 e2access ls 'home/paulb/public' 152 e2access ls 'home/paulb/shared' 153 fi 154 155 rm -r tmp_e2test 156 157 # vim: tabstop=4 expandtab shiftwidth=4