Logo Search packages:      
Sourcecode: schroot version File versions  Download package

void chroot_lvm_snapshot::setup_lock ( setup_type  type,
bool  lock 
) [virtual]

Lock a chroot during setup. The locking technique (if any) may vary depending upon the chroot type and setup stage. For example, during creation of an LVM snapshot a block device might require locking, but afterwards this will change to the new block device.

An error will be thrown on failure.

Parameters:
type the type of setup being performed
lock true to lock, false to unlock

Reimplemented from sbuild::chroot_block_device.

Definition at line 118 of file sbuild-chroot-lvm-snapshot.cc.

References sbuild::chroot_block_device::device, sbuild::chroot::EXEC_START, sbuild::chroot::EXEC_STOP, sbuild::chroot_block_device::get_device(), sbuild::chroot::get_name(), get_snapshot_device(), sbuild::lock::LOCK_EXCLUSIVE, sbuild::device_lock::set_lock(), sbuild::chroot::setup_session_info(), sbuild::chroot::SETUP_START, sbuild::chroot::SETUP_STOP, and sbuild::device_lock::unset_lock().

{
  std::string device;
  struct stat statbuf;

  /* Lock is removed by setup script on setup stop.  Unlocking here
     would fail: the LVM snapshot device no longer exists. */
  if (!(type == SETUP_STOP && lock == false))
    {
      if (type == SETUP_START)
      device = get_device();
      else
      device = get_snapshot_device();

      if (device.empty())
      {
        format fmt(_("%1% chroot: device name not set"));
        fmt % get_name();
        throw error(fmt);
      }
      else if (stat(device.c_str(), &statbuf) == -1)
      {
        format fmt(_("%1% chroot: failed to stat device %2%: %3%"));
        fmt % get_name() % device % strerror(errno);
        throw error(fmt);
      }
      else if (!S_ISBLK(statbuf.st_mode))
      {
        format fmt(_("%1% chroot: %2% is not a block device\n"));
        fmt % get_name() % device;
        throw error(fmt);
      }
      else
      {
        /* Lock is preserved while running a command. */
        if ((type == EXEC_START && lock == false) ||
            (type == EXEC_STOP && lock == true))
          return;

        sbuild::device_lock dlock(device);
        if (lock)
          {
            try
            {
              dlock.set_lock(lock::LOCK_EXCLUSIVE, 15);
            }
            catch (sbuild::lock::error const& e)
            {
              format fmt(_("%1%: failed to lock device: %2%"));
              fmt % device % e.what();
              throw error(fmt);
            }
          }
        else
          {
            try
            {
              dlock.unset_lock();
            }
            catch (sbuild::lock::error const& e)
            {
              format fmt(_("%1%: failed to unlock device: %2%"));
              fmt % device % e.what();
              throw error(fmt);
            }
          }
      }
    }

  /* Create or unlink session information. */
  if ((type == SETUP_START && lock == true) ||
      (type == SETUP_STOP && lock == false))
    {
      bool start = (type == SETUP_START);
      setup_session_info(start);
    }
}


Generated by  Doxygen 1.6.0   Back to index