github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/ext/benchmark/make_deep_ext4.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2019 The gVisor Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # This script creates an ext4 image with $1 depth of directories and a file in
    18  # the inner most directory. The created file is at path /1/2/.../depth/file.txt.
    19  # The ext4 image is written to $2. The image is temporarily mounted at
    20  # /tmp/mountpoint. This script must be run with sudo privileges.
    21  
    22  # Usage:
    23  # sudo bash make_deep_ext4.sh {depth} {output path}
    24  
    25  # Check positional arguments.
    26  if [ "$#" -ne 2 ]; then
    27      echo "Usage: sudo bash make_deep_ext4.sh {depth} {output path}"
    28      exit 1
    29  fi
    30  
    31  # Make sure depth is a non-negative number.
    32  if ! [[ "$1" =~ ^[0-9]+$ ]]; then
    33          echo "Depth must be a non-negative number."
    34          exit 1
    35  fi
    36  
    37  # Create a 1 MB filesystem image at the requested output path.
    38  rm -f $2
    39  fallocate -l 1M $2
    40  if [ $? -ne 0 ]; then
    41      echo "fallocate failed"
    42      exit $?
    43  fi
    44  
    45  # Convert that blank into an ext4 image.
    46  mkfs.ext4 -j $2
    47  if [ $? -ne 0 ]; then
    48      echo "mkfs.ext4 failed"
    49      exit $?
    50  fi
    51  
    52  # Mount the image.
    53  MOUNTPOINT=/tmp/mountpoint
    54  mkdir -p $MOUNTPOINT
    55  mount -o loop $2 $MOUNTPOINT
    56  if [ $? -ne 0 ]; then
    57      echo "mount failed"
    58      exit $?
    59  fi
    60  
    61  # Create nested directories and the file.
    62  if [ "$1" -eq 0 ]; then
    63     FILEPATH=$MOUNTPOINT/file.txt
    64  else
    65     FILEPATH=$MOUNTPOINT/$(seq -s '/' 1 $1)/file.txt
    66  fi
    67  mkdir -p $(dirname $FILEPATH) || exit
    68  touch $FILEPATH
    69  
    70  # Clean up.
    71  umount $MOUNTPOINT
    72  rm -rf $MOUNTPOINT