From 62b62b2a9835c4b6451b141ba87b7e5f57bab4b9 Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Sun, 11 Sep 2016 19:39:28 +0900 Subject: [PATCH 1/3] nbd: clearn up disconnection process --- cmd/torusblk/nbd.go | 2 +- internal/nbd/nbd.go | 58 ++++++++++++++++++++------------------------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/cmd/torusblk/nbd.go b/cmd/torusblk/nbd.go index 4b5063d..957e41d 100644 --- a/cmd/torusblk/nbd.go +++ b/cmd/torusblk/nbd.go @@ -116,7 +116,7 @@ func connectNBD(srv *torus.Server, f *block.BlockFile, target string, closer cha fmt.Fprintf(os.Stderr, "error from nbd server: %s\n", err) os.Exit(1) } - return handle.Close() + return nil } type finder struct { diff --git a/internal/nbd/nbd.go b/internal/nbd/nbd.go index 474b56f..8eab117 100644 --- a/internal/nbd/nbd.go +++ b/internal/nbd/nbd.go @@ -218,51 +218,45 @@ func (nbd *NBD) Serve() error { }(nbd) } - waitDisconnect(nbd.nbd) + // NBD_DO_IT does not return until disconnect + if err := ioctl(nbd.nbd.Fd(), ioctlDoIt, 0); err != nil { + clog.Errorf("error %s: ioctl returned %v", nbd.nbd.Name(), err) + } wg.Wait() return nil } -func (nbd *NBD) Close() error { - // FIXME: Now I understand what these did, and it terrifies me. - // syscall.Write(nbd.setsocket, make([]byte, 28)) - // syscall.Close(nbd.setsocket) - nbd.Disconnect() - syscall.Close(nbd.socket) - return nbd.nbd.Close() -} - -func (nbd *NBD) Disconnect() error { +func (nbd *NBD) Disconnect() { + var err error if nbd.nbd == nil { - return nil + return } - if err := ioctl(nbd.nbd.Fd(), ioctlDisconnect, 0); err != nil { - return &os.PathError{ - Path: nbd.nbd.Name(), - Op: "ioctl NBD_DISCONNECT", - Err: err, - } - } - return nil -} -func waitDisconnect(f *os.File) { + clog.Infof("Running disconnection to %s", nbd.nbd.Name()) + runtime.LockOSThread() defer runtime.UnlockOSThread() - // NBD_DO_IT does not return until disconnect - if err := ioctl(f.Fd(), ioctlDoIt, 0); err != nil { - clog.Errorf("error %s: ioctl returned %v", f.Name(), err) + err = ioctl(nbd.nbd.Fd(), ioctlDisconnect, 0) + if err != nil { + clog.Errorf("error disconnecting %s. ioctl returned: %v", nbd.nbd.Name(), err) } - - clog.Debugf("Running disconnection to %s", f.Name()) - - if err := ioctl(f.Fd(), ioctlClearQueue, 0); err != nil { - clog.Errorf("error clear queue for %s. ioctl returned: %v", f.Name(), err) + err = ioctl(nbd.nbd.Fd(), ioctlClearSock, 0) + if err != nil { + clog.Errorf("error clear socket for %s. ioctl returned: %v", nbd.nbd.Name(), err) } - if err := ioctl(f.Fd(), ioctlClearSock, 0); err != nil { - clog.Errorf("error clear socket for %s. ioctl returned: %v", f.Name(), err) + err = ioctl(nbd.nbd.Fd(), ioctlClearQueue, 0) + if err != nil { + clog.Errorf("error clear queue for %s. ioctl returned: %v", nbd.nbd.Name(), err) + } + err = ioctl(nbd.nbd.Fd(), ioctlClearQueue, 0) + if err != nil { + clog.Errorf("error clear queue for %s. ioctl returned: %v", nbd.nbd.Name(), err) + } + err = nbd.nbd.Close() + if err != nil { + clog.Errorf("error close nbd device %s. ioctl returned: %v", nbd.nbd.Name(), err) } } From 49c00804977b71ef0d615064d04a85ca816f8d1c Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Sun, 11 Sep 2016 23:21:06 +0900 Subject: [PATCH 2/3] torusblk: support detach command to detach NBD device from a volume --- cmd/torusblk/nbd.go | 37 +++++++++++++++++++++++++++++++++---- internal/nbd/nbd.go | 10 ++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/cmd/torusblk/nbd.go b/cmd/torusblk/nbd.go index 957e41d..f46e9d9 100644 --- a/cmd/torusblk/nbd.go +++ b/cmd/torusblk/nbd.go @@ -12,11 +12,23 @@ import ( "github.com/spf13/cobra" ) +var nbdCommand = &cobra.Command{ + Use: "nbd", + Short: "manage NBD device", + Run: nbdAction, +} + var ( - nbdCommand = &cobra.Command{ - Use: "nbd VOLUME [NBD-DEV]", + nbdAttachCommand = &cobra.Command{ + Use: "attach VOLUME [NBD-DEV]", Short: "attach a block volume to an NBD device", - Run: nbdAction, + Run: nbdAttachAction, + } + + nbdDetachCommand = &cobra.Command{ + Use: "detach NBD-DEV", + Short: "detach an NBD device from a block volume", + Run: nbdDetachAction, } nbdServeCommand = &cobra.Command{ @@ -30,12 +42,19 @@ var ( func init() { rootCommand.AddCommand(nbdCommand) + nbdCommand.AddCommand(nbdAttachCommand) + nbdCommand.AddCommand(nbdDetachCommand) + nbdCommand.AddCommand(nbdServeCommand) nbdServeCommand.Flags().StringVarP(&serveListenAddress, "listen", "l", "0.0.0.0:10809", "nbd server listen address") - rootCommand.AddCommand(nbdServeCommand) } func nbdAction(cmd *cobra.Command, args []string) { + cmd.Usage() + os.Exit(1) +} + +func nbdAttachAction(cmd *cobra.Command, args []string) { if len(args) != 1 && len(args) != 2 { cmd.Usage() os.Exit(1) @@ -82,6 +101,16 @@ func nbdAction(cmd *cobra.Command, args []string) { } } +func nbdDetachAction(cmd *cobra.Command, args []string) { + if len(args) != 1 { + cmd.Usage() + os.Exit(1) + } + if err := nbd.Detach(args[0]); err != nil { + die("failed to detach: %v", err) + } +} + func connectNBD(srv *torus.Server, f *block.BlockFile, target string, closer chan bool) error { defer f.Close() size := f.Size() diff --git a/internal/nbd/nbd.go b/internal/nbd/nbd.go index 8eab117..c0d93ce 100644 --- a/internal/nbd/nbd.go +++ b/internal/nbd/nbd.go @@ -227,6 +227,16 @@ func (nbd *NBD) Serve() error { return nil } +func Detach(dev string) error { + f, err := os.Open(dev) + if err != nil { + return err + } + n := &NBD{nbd: f} + n.Disconnect() + return nil +} + func (nbd *NBD) Disconnect() { var err error if nbd.nbd == nil { From a5fbf0966dc61925610aa7b73ac8bd6f5c4b21fb Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Sun, 11 Sep 2016 23:32:23 +0900 Subject: [PATCH 3/3] torusblk: change subcommand from detach to --detach option --- cmd/torusblk/nbd.go | 46 ++++++++++++++------------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/cmd/torusblk/nbd.go b/cmd/torusblk/nbd.go index f46e9d9..6c2b517 100644 --- a/cmd/torusblk/nbd.go +++ b/cmd/torusblk/nbd.go @@ -12,23 +12,11 @@ import ( "github.com/spf13/cobra" ) -var nbdCommand = &cobra.Command{ - Use: "nbd", - Short: "manage NBD device", - Run: nbdAction, -} - var ( - nbdAttachCommand = &cobra.Command{ - Use: "attach VOLUME [NBD-DEV]", + nbdCommand = &cobra.Command{ + Use: "nbd VOLUME [NBD-DEV]", Short: "attach a block volume to an NBD device", - Run: nbdAttachAction, - } - - nbdDetachCommand = &cobra.Command{ - Use: "detach NBD-DEV", - Short: "detach an NBD device from a block volume", - Run: nbdDetachAction, + Run: nbdAction, } nbdServeCommand = &cobra.Command{ @@ -36,25 +24,29 @@ var ( Short: "serve a block volume over the NBD protocol", Run: nbdServeAction, } +) +var ( serveListenAddress string + detachDevice string ) func init() { rootCommand.AddCommand(nbdCommand) - nbdCommand.AddCommand(nbdAttachCommand) - nbdCommand.AddCommand(nbdDetachCommand) - nbdCommand.AddCommand(nbdServeCommand) + rootCommand.AddCommand(nbdServeCommand) + nbdCommand.Flags().StringVarP(&detachDevice, "detach", "d", "", "detach an NBD device from a block volume. (e.g. torsublk nbd -d /dev/nbd0)") nbdServeCommand.Flags().StringVarP(&serveListenAddress, "listen", "l", "0.0.0.0:10809", "nbd server listen address") } func nbdAction(cmd *cobra.Command, args []string) { - cmd.Usage() - os.Exit(1) -} + if len(detachDevice) > 0 && len(args) == 0 { + if err := nbd.Detach(detachDevice); err != nil { + die("failed to detach: %v", err) + } + os.Exit(0) + } -func nbdAttachAction(cmd *cobra.Command, args []string) { if len(args) != 1 && len(args) != 2 { cmd.Usage() os.Exit(1) @@ -101,16 +93,6 @@ func nbdAttachAction(cmd *cobra.Command, args []string) { } } -func nbdDetachAction(cmd *cobra.Command, args []string) { - if len(args) != 1 { - cmd.Usage() - os.Exit(1) - } - if err := nbd.Detach(args[0]); err != nil { - die("failed to detach: %v", err) - } -} - func connectNBD(srv *torus.Server, f *block.BlockFile, target string, closer chan bool) error { defer f.Close() size := f.Size()