There is a new dm target called "snapshot-merge".
If you format your USB flash memory as a LVM physical volume, and then locate your desired filesystem atop it in a logical volume, you can
- Activate a volume group containing your USB flash memory and another LVM physical volume on a local disk.
- Create a snapshot of the logical volume on the local disk.
- Mount the snapshot, do whatever you want with it, then umount it.
- Merge the snapshot back to the origin.
This should achieve close to what you've asked for, although it requires a scratch block device rather than a temporary directory.
Substitute the parts enclosed in {braces} as appropriate.
# Initial setup of the USB drive.
pvcreate /dev/{USB}
vgcreate {removable} /dev/{USB}
lvcreate -n {base} -l 100%PVS {removable} /dev/{USB}
mkfs -t {fs} {...} /dev/mapper/{removable}-{base}
# Initial setup of the scratch device.
pvcreate /dev/{SCRATCH}
# Mounting the device.
vgextend {removable} /dev/{SCRATCH}
lvcreate -s -n {snap} -l 100%ORIGIN /dev/mapper/{removable}-{base} /dev/{SCRATCH}
mount -t {fs} -o {...} /dev/mapper/{removable}-{snap} {MOUNTPOINT}
# Unmounting the device.
umount {MOUNTPOINT}
lvconvert --merge /dev/mapper/{removable}-{snap}
vgreduce {removable} /dev/{SCRATCH}
vgchange -a n {removable}
Untested, but all the LVM commands have manpages so you should be able to figure things out from here. You might need a vgscan
invocation in there somewhere, if the volume group doesn't get automatically detected when you plug the USB drive in.