func connectNBD(srv *torus.Server, f *block.BlockFile, target string, closer chan bool) error { defer f.Close() size := f.Size() gmd := srv.MDS.GlobalMetadata() handle := nbd.Create(f, int64(size), int64(gmd.BlockSize)) if target == "" { t, err := nbd.FindDevice() if err != nil { return err } target = t } _, err := handle.OpenDevice(target) if err != nil { return err } go func(n *nbd.NBD) { <-closer n.Disconnect() }(handle) err = handle.Serve() if err != nil { return fmt.Errorf("error from nbd server: %s", err) } return nil }
func connectNBD(srv *torus.Server, f *block.BlockFile, target string, closer chan bool) error { defer f.Close() size := f.Size() gmd, err := srv.MDS.GlobalMetadata() if err != nil { return err } handle := nbd.Create(f, int64(size), int64(gmd.BlockSize)) if target == "" { target, err = nbd.FindDevice() if err != nil { fmt.Println(err) return err } } dev, err := handle.OpenDevice(target) if err != nil { fmt.Println(err) return err } fmt.Println("Connected to", dev) go func(n *nbd.NBD) { <-closer n.Disconnect() }(handle) err = handle.Serve() if err != nil { fmt.Fprintf(os.Stderr, "error from nbd server: %s\n", err) os.Exit(1) } return handle.Close() }
//{"kubernetes.io/fsType":"ext4","kubernetes.io/readwrite":"rw","volume":"block1"} func attachAction(cmd *cobra.Command, args []string) { if len(args) != 1 { onErr(errors.New("unexpected number of arguments")) } vol := parseJSONArg(args[0]) dev, err := nbd.FindDevice() if err != nil { onErr(err) } sysd := connectSystemd() svc := devToUnitName(dev) me, err := osext.Executable() if err != nil { onErr(err) } cmdList := []string{ me, "-C", vol.Etcd, "nbd", vol.VolumeName, dev, } if vol.WriteLevel != "" { _, err := torus.ParseWriteLevel(vol.WriteLevel) if err != nil { onErr(err) } cmdList = append(cmdList, []string{"--write-level", vol.WriteLevel}...) } if vol.WriteCacheSize != "" { cmdList = append(cmdList, []string{"--write-cache-size", vol.WriteCacheSize}...) } ch := make(chan string) sysd.ResetFailedUnit(svc) _, err = sysd.StartTransientUnit(svc, "fail", []dbus.Property{ dbus.PropExecStart(cmdList, false), }, ch) if err != nil { onErr(err) } <-ch status := sysd.wait(svc) if status == "failed" { onErr(errors.New("Couldn't attach")) } else if status == "active" { writeResponse(Response{ Status: "Success", Device: dev, }) } else { onErr(errors.New(status)) } os.Exit(0) }