// checks if the required cgroups subsystems are mounted. // As of now, only 'cpu' and 'memory' are required. func validateSystemRequirements(mountUtil mount.Interface) error { const ( cgroupMountType = "cgroup" localErr = "system validation failed" ) mountPoints, err := mountUtil.List() if err != nil { return fmt.Errorf("%s - %v", localErr, err) } expectedCgroups := sets.NewString("cpu", "cpuacct", "cpuset", "memory") for _, mountPoint := range mountPoints { if mountPoint.Type == cgroupMountType { for _, opt := range mountPoint.Opts { if expectedCgroups.Has(opt) { expectedCgroups.Delete(opt) } } } } if expectedCgroups.Len() > 0 { return fmt.Errorf("%s - Following Cgroup subsystem not mounted: %v", localErr, expectedCgroups.List()) } return nil }
// utility to mount a disk based filesystem func diskSetUp(manager diskManager, b iscsiDiskBuilder, volPath string, mounter mount.Interface) error { globalPDPath := manager.MakeGlobalPDName(*b.iscsiDisk) // TODO: handle failed mounts here. notMnt, err := mounter.IsLikelyNotMountPoint(volPath) if err != nil && !os.IsNotExist(err) { glog.Errorf("cannot validate mountpoint: %s", volPath) return err } if !notMnt { return nil } if err := manager.AttachDisk(b); err != nil { glog.Errorf("failed to attach disk") return err } if err := os.MkdirAll(volPath, 0750); err != nil { glog.Errorf("failed to mkdir:%s", volPath) return err } // Perform a bind mount to the full path to allow duplicate mounts of the same disk. options := []string{"bind"} if b.readOnly { options = append(options, "ro") } err = mounter.Mount(globalPDPath, volPath, "", options) if err != nil { glog.Errorf("failed to bind mount:%s", globalPDPath) return err } return nil }
// getDevicePrefixRefCount: given a prefix of device path, find its reference count from /proc/mounts // returns the reference count to the device and error code // for services like iscsi construct multiple device paths with the same prefix pattern. // this function aggregates all references to a service based on the prefix pattern // More specifically, this prefix semantics is to aggregate disk paths that belong to the same iSCSI target/iqn pair. // an iSCSI target could expose multiple LUNs through the same IQN, and Linux iSCSI initiator creates disk paths that start the same prefix but end with different LUN number // When we decide whether it is time to logout a target, we have to see if none of the LUNs are used any more. // That's where the prefix based ref count kicks in. If we only count the disks using exact match, we could log other disks out. func getDevicePrefixRefCount(mounter mount.Interface, deviceNamePrefix string) (int, error) { mps, err := mounter.List() if err != nil { return -1, err } // Find the number of references to the device. refCount := 0 for i := range mps { if strings.HasPrefix(mps[i].Device, deviceNamePrefix) { refCount++ } } return refCount, nil }
// utility to tear down a disk based filesystem func diskTearDown(manager diskManager, c iscsiDiskCleaner, volPath string, mounter mount.Interface) error { notMnt, err := mounter.IsLikelyNotMountPoint(volPath) if err != nil { glog.Errorf("cannot validate mountpoint %s", volPath) return err } if notMnt { return os.Remove(volPath) } refs, err := mount.GetMountRefs(mounter, volPath) if err != nil { glog.Errorf("failed to get reference count %s", volPath) return err } if err := mounter.Unmount(volPath); err != nil { glog.Errorf("failed to unmount %s", volPath) return err } // If len(refs) is 1, then all bind mounts have been removed, and the // remaining reference is the global mount. It is safe to detach. if len(refs) == 1 { mntPath := refs[0] if err := manager.DetachDisk(c, mntPath); err != nil { glog.Errorf("failed to detach disk from %s", mntPath) return err } } notMnt, mntErr := mounter.IsLikelyNotMountPoint(volPath) if mntErr != nil { glog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr) return err } if notMnt { if err := os.Remove(volPath); err != nil { return err } } return nil }