Last active
July 12, 2023 16:33
-
-
Save mailinglists35/8b57e15505579d575ee7a0662292bfa8 to your computer and use it in GitHub Desktop.
Create writable device files from readonly images on Linux using device mapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
copied from https://git.scc.kit.edu/-/snippets/563, found on search for "device mapper volatile writeable readonly" | |
#!/bin/bash | |
set -e | |
# test if $1 exists | |
test -f "$1" | |
BASEDIR=$(dirname "$1") | |
original_image=$(basename "$1") | |
BASENAME=`basename "${original_image}" .raw` | |
cow_file=".${BASENAME}.cow" | |
cow_device_name="${BASENAME}.cow" | |
# cow-size in bytes | |
cow_file_size=$((800*1024*1024)) | |
# create new loopdev for original image | |
losetup --read-only --verbose --find "${BASEDIR}/${original_image}" | |
# find existing loopdev | |
loopdev_image=$(losetup --associated "${BASEDIR}/${original_image}" --output NAME --noheadings | cut -d: -f1) | |
# create new COW file | |
fallocate -l ${cow_file_size} "${cow_file}" | |
# create new loopdev for COW file | |
losetup --verbose --find "${cow_file}" | |
# find existing loopdev | |
loopdev_cow=$(losetup --associated "${cow_file}" --output NAME --noheadings | cut -d: -f1) | |
# construct cow-file | |
dmsetup create "${cow_device_name}" --table "0 $(blockdev --getsz ${loopdev_image}) snapshot ${loopdev_image} ${loopdev_cow} p 64" | |
# add partitions | |
kpartx -v -a "/dev/mapper/${cow_device_name}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment