I needed a script that lets me download AWS EC2 EBS volumes and mount them locally. I wrote this using Claude 3.7, so expect the code to look like it was written by Claude 3.7.
AWS does not natively support downloading and mounting an EC2 EBS volume. This script does that for me so I don't have to do the intermediate steps
The following diagram illustrates the workflow of downloading and mounting an EBS volume:
flowchart TD
A[List EBS Volumes] -->|Select volume to download| B[Download EBS Volume]
B -->|Create snapshot| C[Use EBS Direct APIs]
C -->|Download blocks| D[Save to local file]
D -->|Clean up snapshot| E[Local EBS image file]
E -->|Mount with Docker| F[Mount script]
F -->|Standard partitions| G[mount_ebs.sh]
F -->|LVM volumes| H[mount_lvm_ebs.sh]
G -->|1. Calculate offset| I[Mount partition]
H -->|1. Setup loop device| J[Activate LVM]
I -->|2. Mount filesystem| K[Interactive shell]
J -->|2. Mount logical volume| K
K -->|Exit shell| L[Unmount volume]
The process involves:
- Listing available EBS volumes in your AWS account
- Creating a snapshot of the selected volume
- Using EBS Direct APIs to download all blocks
- Saving the data to a local file
- Mounting the file using Docker with the appropriate script:
mount_ebs.shfor standard partitioned volumesmount_lvm_ebs.shfor volumes using Logical Volume Management (LVM)
# Install dependencies
uv venv
uv pip install -e .
# List your EBS volumes
python ebs_manager.py --list
# Download an EBS volume
python ebs_manager.py --download vol-1234567890abcdef0 -o my_volume
# Mount the downloaded volume (drops you directly into the mounted directory)
./mount_ebs.sh -i my_volume
# When done, type 'exit' or press Ctrl+D to unmount and exit- Python 3.7+
- UV package manager
- AWS credentials configured
- Docker (for mounting volumes)
- Clone this repository
- Create a virtual environment and install dependencies:
# Create a virtual environment uv venv # Activate the virtual environment source .venv/bin/activate # On Unix/macOS # or .venv\Scripts\activate # On Windows # Install dependencies uv pip install -e . # For development dependencies uv pip install -e ".[dev]"
- Make scripts executable:
chmod +x ebs_manager.py mount_ebs.sh mount_lvm_ebs.sh
python ebs_manager.py --list
python ebs_manager.py --list --region us-east-1python ebs_manager.py --download vol-1234567890abcdef0 -o output_file
python ebs_manager.py --download vol-1234567890abcdef0 -o output_file --region us-west-2 --forceFor standard partitioned volumes:
# Mount the first partition (default)
./mount_ebs.sh -i volume_file
# You'll be dropped directly into the mounted directory
# Type 'exit' or press Ctrl+D to unmount and exit
# Mount a specific partition
./mount_ebs.sh -i volume_file -p 2
# List partitions without mounting
./mount_ebs.sh -i volume_file -lFor LVM volumes:
./mount_lvm_ebs.sh -i volume_file
# You'll be dropped directly into the mounted directory
# Type 'exit' or press Ctrl+D to unmount and exit{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:DescribeVolumes",
"ec2:DescribeInstances",
"ec2:CreateSnapshot",
"ec2:DeleteSnapshot",
"ec2:DescribeSnapshots",
"ebs:ListSnapshotBlocks",
"ebs:GetSnapshotBlock"
],
"Resource": "*"
}
]
}This project is licensed under the MIT License.