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