Add shared db locking.

Also:

  * improve bootstrap sequence.
  * move some common functions and global variables to common.
  * improve security of recovery document.
  * improve documentation.