5

Suppose I mounted a disk in this way:

mount /dev/sdb /mnt/tmp

I have some files opened on this filesystem and don't want to unmount it. However I want to temporarily extract the device, then reattach it later. I want all reads and writes to this filesystem to be performed in cache only or be hung until I reattach the device.

If I thought about temporarily detaching in advance, I would have used the device mapper:

# ls -lh /dev/sdb
brw-rw---- 1 root floppy 8, 16 Sep 12 17:38 /dev/sdb
# blockdev --getsize /dev/sdb
2211840
# dmsetup create sdb_detachable --table '0 2211840 linear 8:16 0'
# mount /dev/mapper/sdb_detachable /mnt/tmp
(start working with the filesystem)
(suddenly need to detach the device)
# dmsetup suspend sdb_detachable
# dmsetup load sdb_detachable --table '0 2211840 error'
# blockdev --flushbufs /dev/sdb
(eject the device)
(maybe even use the cached part of the filesystem)
(reattach the device, now it appears as /dev/sdc)
# ls -lh /dev/sdc && blockdev --getsize /dev/sdc
brw-rw---- 1 root floppy 8, 32 Sep 12 17:51 /dev/sdc
2211840
# dmsetup load sdb_detachable --table '0 2211840 linear 8:32 0'
# dmsetup resume sdb_detachable
(filesystem is usable again)
(finished using it, now need to clean up)
# umount /mnt/tmp/
# dmsetup remove sdb_detachable
# eject /dev/sdc

How can this be accomplished if the device is mounted directly? Can I "steal" it into the device mapper?

Timothy Martin
  • 8,447
  • 1
  • 34
  • 40
Vi.
  • 5,528
  • 7
  • 34
  • 68
  • This seems risky. If I remember correctly the problem of a colleage, just because the kernel has written the data to the disk controller does not mean the controller has written the data do disk. – Thomas Erker Sep 05 '15 at 20:04
  • @ThomasErker, So how do I get real, ultimate configuration that the data has been written? Will `eject` command do it? – Vi. Sep 05 '15 at 22:04
  • The case I had in mind was something like this: write -> sync -> power failure. The disk controller had commited the write only to it's battary backed cache but not to disk. And the battary was low on power... For usb sticks, my remark is probably completely irreleavant. – Thomas Erker Sep 05 '15 at 23:22

1 Answers1

0

Hm, that's cool. I didn't know dmsetup could do that. :)

My only answer would be to suspend/hibernate the machine, pull out the flash drive and do your manipulating somewhere else, then put the flash drive back in before resuming the system.

Unfortunately, the filesystem on the device is likely to be corrupted by doing anything like this. Perhaps if all you do is read from it (and no blocks are written anywhere on the device), this could work.

However, most flash devices have re-write iteration limits so (in some devices) the firmware might move things around when a write occurs, even if the write contains the exact same data. This would mean that bringing the device back to the first system would likely corrupt the filesystem.

Interesting problem, though. :)

Azhrei
  • 341
  • 1
  • 8