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