mount-loop.sh is a versatile shell script designed to simplify the creation and management of loop devices, filesystems, ramdisks, and devices with simulated faulty blocks on Linux systems. It automates the process of setting up loopback devices, creating files of specified or random sizes, and mounting them with or without filesystems. This tool is particularly useful for developers, system administrators, and testers who need to simulate block devices, perform filesystem operations, test storage-related functionalities, or simulate faulty storage conditions without the need for physical hardware.
- Features
- Requirements
- Installation
- Usage
- Use-Case Scenarios
- How It Works
- Cleanup and Safety
- Contributing
- License
- Automated Loop Device Setup: Quickly create and manage loop devices without manual configuration.
- File Creation: Generate files of specific or random sizes to act as virtual block devices.
- Filesystem Support: Optionally create filesystems (ext4) on loop devices and mount them for immediate use.
- Multiple Devices: Create and manage multiple loop devices simultaneously with ease.
- Ramdisk Creation: Set up ramdisks as loop devices for high-speed I/O operations.
- Faulty Block Simulation: Create loop devices with specified faulty blocks to test error handling.
- User-Friendly Interface: Simple command-line options with detailed help and examples.
- Automatic Cleanup: Ensures temporary files and mounts are cleaned up after use.
- Operating System: Linux
- Permissions: Root privileges (run as
root
or usesudo
) - Utilities:
bash
losetup
dd
mkfs.ext4
mount
umount
awk
uuidgen
mktemp
dmsetup
-
Clone the Repository:
git clone https://github.com/yourusername/mount-loop.sh.git
-
Navigate to the Directory:
cd mount-loop.sh
-
Make the Script Executable:
chmod +x mount-loop.sh
./mount-loop.sh [OPTION]... [FILE]
Set up loop devices for a given file, create new files of specified or random sizes, create ramdisks, or simulate devices with faulty blocks.
--help
: Display help and exit.automount <Size>
: Create a file of the specified size and set it up as a loop device.automountfs <Size>
: Same asautomount
but also create a filesystem and mount it.faultymount <Size> <BlockNumbers>
: Create a loop device with specified faulty blocks.faultymountfs <Size> <BlockNumbers>
: Same asfaultymount
but also create a filesystem and mount it.polymount <N> <Size>
: CreateN
files of the specified size and set them up as loop devices.polymountfs <N> <Size>
: Same aspolymount
but also create filesystems and mount them.polymount rand <N> <MinSize> <MaxSize>
: CreateN
files with random sizes betweenMinSize
andMaxSize
, set them up as loop devices.polymountfs rand <N> <MinSize> <MaxSize>
: Same as above but also create filesystems and mount them.tmpfsmount <Size>
: Create a ramdisk of the specified size as a loop device.tmpfsmountfs <Size>
: Same astmpfsmount
but also create a filesystem and mount it.<FilePath>
: Path to an existing file to set up as a loop device.
-
Set up an existing file as a loop device:
sudo ./mount-loop.sh /path/to/your/file.img
-
Create a 1G file and set it up as a loop device:
sudo ./mount-loop.sh automount 1G
-
Create a 1G file, set it up as a loop device, create a filesystem, and mount it:
sudo ./mount-loop.sh automountfs 1G
-
Create a 1G loop device with blocks 500 and 1000 marked as faulty:
sudo ./mount-loop.sh faultymount 1G 500,1000
-
Create a 1G loop device, create a filesystem, mount it, and mark blocks 500 to 510 as faulty:
sudo ./mount-loop.sh faultymountfs 1G 500-510
-
Using a combination of single blocks and ranges:
sudo ./mount-loop.sh faultymount 1G 500,1000-1010,1500
-
Create 5 files of 1G each and set them up as loop devices:
sudo ./mount-loop.sh polymount 5 1G
-
Same as above but also create filesystems and mount them:
sudo ./mount-loop.sh polymountfs 5 1G
-
Create 5 files with random sizes between 500M and 2G:
sudo ./mount-loop.sh polymount rand 5 500M 2G
-
Same as above but also create filesystems and mount them:
sudo ./mount-loop.sh polymountfs rand 5 500M 2G
-
Create a 1G ramdisk and set it up as a loop device:
sudo ./mount-loop.sh tmpfsmount 1G
-
Create a 1G ramdisk, set it up as a loop device, create a filesystem, and mount it:
sudo ./mount-loop.sh tmpfsmountfs 1G
Developers working on disk utilities or applications that interact with block devices can use mount-loop.sh
to simulate disk environments without the need for physical disks.
-
Scenario: Testing a new disk cloning tool.
-
Solution: Create multiple loop devices with filesystems to act as source and target disks.
sudo ./mount-loop.sh automountfs 5G sudo ./mount-loop.sh automountfs 5G
Test how applications handle read/write errors due to bad sectors or faulty blocks.
-
Scenario: Testing the robustness of backup software when encountering disk errors.
-
Solution: Create a loop device with faulty blocks to simulate a failing disk.
sudo ./mount-loop.sh faultymountfs 1G 500-510
-
Testing read error handling:
sudo dd if=/dev/mapper/faulty-loop-loop0 of=/dev/null bs=512 skip=500 count=1
This should produce an input/output error.
Test the performance of filesystems or applications under different storage conditions.
-
Scenario: Benchmarking application performance on different storage media.
-
Solution: Use ramdisks and regular loop devices to compare performance.
# Ramdisk sudo ./mount-loop.sh tmpfsmountfs 1G # Regular loop device sudo ./mount-loop.sh automountfs 1G
Experiment with different filesystem types and configurations.
-
Scenario: Testing the features of a new filesystem.
-
Solution: Modify the script to format loop devices with the desired filesystem.
# Modify mkfs command in the script to use mkfs.xfs or another filesystem. sudo ./mount-loop.sh automountfs 1G
The script automates the following processes:
- File Creation: Uses
dd
to create a file of the specified size, filled with zeros. - Loop Device Setup: Utilizes
losetup
to associate the file with a loop device. - Faulty Block Simulation (Optional): Uses the Device Mapper with the
error
target to simulate faulty blocks. - Filesystem Creation (Optional): Formats the loop device with
mkfs.ext4
. - Mounting (Optional): Mounts the loop device to a temporary directory created with
mktemp
. - User Interaction: Waits for user input before proceeding to unmount and detach devices.
- Cleanup: Unmounts filesystems, detaches loop devices, deletes temporary files and directories.
- Automatic Cleanup: The script ensures that all temporary files, loop devices, and mount points are cleaned up after use.
- User Prompts: Before unmounting and detaching, the script waits for user confirmation, allowing for any necessary operations to be performed.
- Error Handling: Includes checks and error messages for common issues, such as insufficient permissions or invalid input.
- Faulty Devices: When creating devices with faulty blocks, the script uses the Device Mapper to safely simulate errors without affecting the underlying storage.
Contributions are welcome! If you have suggestions, enhancements, or bug fixes, please open an issue or submit a pull request on the GitHub repository.
-
Fork the Repository
-
Create a Feature Branch
git checkout -b feature/YourFeature
-
Commit Your Changes
git commit -m "Add YourFeature"
-
Push to Your Branch
git push origin feature/YourFeature
-
Open a Pull Request
This project is licensed under the MIT License. See the COPYING file for details.
Disclaimer: Use this script at your own risk. Ensure you have backups and understand the operations being performed, especially when running as root.