Skip to content

Instantly share code, notes, and snippets.

@thejhh
Created January 24, 2012 20:42
Show Gist options
  • Save thejhh/1672448 to your computer and use it in GitHub Desktop.
Save thejhh/1672448 to your computer and use it in GitHub Desktop.
Testing btrfs snapshots and git on ramdisk (tmpfs)

These are my notes while testing.

Installing newer btrfs-tools

Ubuntu LTS 10.04 doesn't support removing btrfs snapshots. It can be fixed by installing newer btrfs.

cd /tmp
wget "http://se.archive.ubuntu.com/ubuntu/pool/main/b/btrfs-tools/btrfs-tools_0.19+20100601-3ubuntu3_i386.deb"
dpkg -i btrfs-tools_0.19+20100601-3ubuntu3_i386.deb

Creating the master git repository (on a flash drive):

mkdir -p /flash/disk0
cd /flash/disk0
git init
echo 'This is a test file.' > test.txt
git add test.txt
git commit -a -m 'Added initial test data.'

Initialising tmpfs

mkdir /ramdisks
mount -t tmpfs tmpfs /ramdisks
mkdir /ramdisks/raw /ramdisks/mnt

Creating images on ramdisk

dd if=/dev/zero of=/ramdisks/raw/disk0 bs=1M count=256

Initializing btrfs image

mkfs.btrfs /ramdisks/raw/disk0

Mounting btrfs image

mount -o loop /ramdisks/raw/disk0 /ramdisks/mnt/disk0

Cloning flash to the ramdisk

git clone /flash/disk0 /ramdisks/mnt/disk0/data

Creating snapshot

btrfsctl -s /ramdisks/mnt/disk0/snapshot/disk0-20120124-2149 /ramdisks/mnt/disk0/data

Removing snapshot

cd /ramdisks/mnt/disk0/snapshot && btrfsctl -D disk0-20120124-2149 . && cd -

For some strange reason this doesn't work:

# btrfsctl -D /ramdisks/mnt/disk0/snapshot/disk0-20120124-2149 /ramdisks/mnt/disk0/snapshot
ioctl:: Invalid argument

...or any of these -- although these are probably wrong anyway:

# btrfsctl -D disk0-20120124-2149 /ramdisks/mnt/disk0/data
ioctl:: No such file or directory
# btrfsctl -D /ramdisks/mnt/disk0/snapshot/disk0-20120124-2149 /ramdisks/mnt/disk0/data
ioctl:: Invalid argument
# btrfsctl -D disk0-20120124-2149 /ramdisks/mnt/disk0/snapshot
ioctl:: No such file or directory
#!/bin/sh
ramdisk_dir=/ramdisks/mnt/disk0
snapshot_dir="$ramdisk_dir"/snapshot
master_ram_git="$ramdisk_dir"/data
orig_pwd="$(pwd)"
# Create working copy in a COW snapshot
snapshot_create() {
name="$1"
sync
btrfsctl -s "$snapshot_dir/$name" "$master_ram_git"|grep -qxF 'operation complete' || return 1
return 0
}
# Delete snapshot
snapshot_delete() {
name="$1"
status=0
cd "$snapshot_dir" || return 2
btrfsctl -D "$name" .|grep -qxF 'operation complete' || status=1
cd "$orig_pwd"
return "$status"
}
# Start working on snapshot
work_begin() {
name="$1"
snapshot_create "disk0-$time"
cd "$snapshot_dir/$name/data"
return 0
}
# Commit changes to master ram git
work_commit() {
name="$1"
status=0
git commit -q -a -m 'Changed data' || return 1
cd "$master_ram_git" || return 3
git pull -q "$snapshot_dir/$name/data" master || status=2
cd "$orig_pwd" || return 4
snapshot_delete "$name" || return 5
return "$status"
}
# Rollback, lcean up snapshot without saving data
work_rollback() {
name="$1"
status=0
cd "$orig_pwd" || return 4
snapshot_delete "$name" || return 5
return "$status"
}
set -e
time="$(date +%Y%m%d-%H%M%S-%N)"
# Test modifying data
do_test() {
echo 'New test line written '"$(date -u)" >> test.txt || return 1
return 0
}
if work_begin "disk0-$time"; then
if do_test; then
if work_commit "disk0-$time"; then
echo 'Commit successful.'
else
echo 'Error: Commit failed!' >&2
work_rollback "disk0-$time"
fi
else
echo 'Error: Test failed!' >&2
work_rollback "disk0-$time"
fi
else
echo 'Error: Failed to begin transaction!' >&2
exit 1
fi
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment