Skip to content

Instantly share code, notes, and snippets.

@andrwng
Created December 7, 2017 21:10
Show Gist options
  • Save andrwng/ca03983e38865644844309aa37132243 to your computer and use it in GitHub Desktop.
Save andrwng/ca03983e38865644844309aa37132243 to your computer and use it in GitHub Desktop.
script to create a faulty mount
#!/usr/bin/bash
#
# Script to create a faulty directory.
# Canonicalize the input directory.
faulty_device=`readlink -f $1`
echo Using file as the base of the loop device: $faulty_device
# Create a file large enough to hold the fs. The minimum size is 512B.
# Adjust `bs` based on the output of `state -f "%k" .` (tells you the block size of `.`)
# Adjust `count` based on the desired size of the filesystem.
dd if=/dev/zero of=$faulty_device bs=1024 count=1024
# Create a loop device out of the file.
losetup -f $faulty_device
loop_device=''
# Allow parsing of '\n'-terminated lines
IFS=$'\n'
# Get the name of the loop device we just created.
for dev in `losetup -a | grep $faulty_device`; do
echo "$dev";
loop_candidate=`echo $dev | sed -r 's/(\/dev\/loop[0-9]+).*/\1/'`;
if [ "$dev" != "$loop_device" ]; then
loop_device=$loop_candidate
break;
fi
done
if [ "$loop_device" == "" ]; then
echo Could not find a loop device for $faulty_device
rm $faulty_device
exit 0
fi
echo Using first found loop device for $faulty_device: $loop_device
# Get the name of the first available md device.
highest_md=""
for line in `cat /proc/mdstat`; do
echo $line
md_candidate=`echo $line | sed -r 's/md([0-9]+)\s*:.*/\1/'`;
if [ "$line" != "$md_candidate" ]; then
highest_md=$md_candidate
break;
fi
done;
md_int="0"
if [ "$highest_md" == "" ]; then
echo No md instances found
else
echo Last md instance found: md$highest_md
# Convert the md to an int and add 1.
md_int=$(expr $highest_md + 1)
fi
md=/dev/md$md_int
unset IFS
echo Creating device $md
mdadm --create $md --level=faulty --raid-devices=1 $loop_device
mkfs.ext4 $md
echo To mount the device, run: mount $md <your_mountpoint>
echo After mounting, <your_mountpoint> can be used normally as a directory.
echo To fail the directory, run: mdadm --grow $md -l faulty -p write-all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment