core: Convert all cmdline users to veyron2.Init.

This is part of the runtimeX migration.

This CL, depends on vcl/1921, vcl/1922, and vcl/1923.

Change-Id: I72f3adbdea048b2e1599262472e67aa94b86f748
diff --git a/services/mgmt/pprof/client/proxy.go b/services/mgmt/pprof/client/proxy.go
index a185d0c..7ad3cfb 100644
--- a/services/mgmt/pprof/client/proxy.go
+++ b/services/mgmt/pprof/client/proxy.go
@@ -16,17 +16,18 @@
 	"strings"
 	"time"
 
-	"v.io/core/veyron2"
+	"v.io/core/veyron2/context"
 	"v.io/core/veyron2/services/mgmt/pprof"
+	"v.io/core/veyron2/vtrace"
 )
 
 // StartProxy starts the pprof proxy to a remote pprof object.
-func StartProxy(rt veyron2.Runtime, name string) (net.Listener, error) {
+func StartProxy(ctx *context.T, name string) (net.Listener, error) {
 	listener, err := net.Listen("tcp", "127.0.0.1:0")
 	if err != nil {
 		return nil, err
 	}
-	p := &proxy{rt, name}
+	p := &proxy{ctx, name}
 	mux := http.NewServeMux()
 	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 		w.Header().Add("Location", "/pprof/")
@@ -47,7 +48,7 @@
 }
 
 type proxy struct {
-	rt   veyron2.Runtime
+	ctx  *context.T
 	name string
 }
 
@@ -66,7 +67,8 @@
 		}
 	}
 	c := pprof.PProfClient(p.name)
-	profiles, err := c.Profiles(p.rt.NewContext())
+	ctx, _ := vtrace.SetNewTrace(p.ctx)
+	profiles, err := c.Profiles(ctx)
 	if err != nil {
 		replyUnavailable(w, err)
 		return
@@ -83,7 +85,8 @@
 	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
 	debug, _ := strconv.Atoi(r.FormValue("debug"))
 	c := pprof.PProfClient(p.name)
-	prof, err := c.Profile(p.rt.NewContext(), name, int32(debug))
+	ctx, _ := vtrace.SetNewTrace(p.ctx)
+	prof, err := c.Profile(ctx, name, int32(debug))
 	if err != nil {
 		replyUnavailable(w, err)
 		return
@@ -113,7 +116,8 @@
 	}
 	w.Header().Set("Content-Type", "application/octet-stream")
 	c := pprof.PProfClient(p.name)
-	prof, err := c.CPUProfile(p.rt.NewContext(), int32(sec))
+	ctx, _ := vtrace.SetNewTrace(p.ctx)
+	prof, err := c.CPUProfile(ctx, int32(sec))
 	if err != nil {
 		replyUnavailable(w, err)
 		return
@@ -138,7 +142,8 @@
 // cmdLine replies with the command-line arguments of the process.
 func (p *proxy) cmdLine(w http.ResponseWriter, r *http.Request) {
 	c := pprof.PProfClient(p.name)
-	cmdline, err := c.CmdLine(p.rt.NewContext())
+	ctx, _ := vtrace.SetNewTrace(p.ctx)
+	cmdline, err := c.CmdLine(ctx)
 	if err != nil {
 		replyUnavailable(w, err)
 		return
@@ -175,7 +180,8 @@
 		}
 	}
 	c := pprof.PProfClient(p.name)
-	pcMap, err := c.Symbol(p.rt.NewContext(), pcList)
+	ctx, _ := vtrace.SetNewTrace(p.ctx)
+	pcMap, err := c.Symbol(ctx, pcList)
 	if err != nil {
 		replyUnavailable(w, err)
 		return
diff --git a/services/mgmt/pprof/client/proxy_test.go b/services/mgmt/pprof/client/proxy_test.go
index 174719a..53f0ef0 100644
--- a/services/mgmt/pprof/client/proxy_test.go
+++ b/services/mgmt/pprof/client/proxy_test.go
@@ -7,7 +7,6 @@
 	"testing"
 
 	"v.io/core/veyron2"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/security"
 
 	"v.io/core/veyron/profiles"
@@ -24,12 +23,8 @@
 }
 
 func TestPProfProxy(t *testing.T) {
-	r, err := rt.New()
-	if err != nil {
-		t.Fatalf("Could not initialize runtime: %v", err)
-	}
-	defer r.Cleanup()
-	ctx := r.NewContext()
+	ctx, shutdown := veyron2.Init()
+	defer shutdown()
 
 	s, err := veyron2.NewServer(ctx)
 	if err != nil {
@@ -43,7 +38,7 @@
 	if err := s.ServeDispatcher("", &dispatcher{impl.NewPProfService()}); err != nil {
 		t.Fatalf("failed to serve: %v", err)
 	}
-	l, err := client.StartProxy(r, endpoints[0].Name())
+	l, err := client.StartProxy(ctx, endpoints[0].Name())
 	if err != nil {
 		t.Fatalf("failed to start proxy: %v", err)
 	}
diff --git a/tools/application/impl.go b/tools/application/impl.go
index 3471add..026f2c3 100644
--- a/tools/application/impl.go
+++ b/tools/application/impl.go
@@ -66,7 +66,7 @@
 	}
 	name, profiles := args[0], args[1]
 	app := repository.ApplicationClient(name)
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	j, err := getEnvelopeJSON(ctx, app, profiles)
 	if err != nil {
@@ -98,7 +98,7 @@
 	if err != nil {
 		return fmt.Errorf("ReadFile(%v): %v", envelope, err)
 	}
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	if err = putEnvelopeJSON(ctx, app, profiles, j); err != nil {
 		return err
@@ -124,7 +124,7 @@
 	}
 	name, profile := args[0], args[1]
 	app := repository.ApplicationClient(name)
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	if err := app.Remove(ctx, profile); err != nil {
 		return err
@@ -158,7 +158,7 @@
 	f.Close()
 	defer os.Remove(fileName)
 
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	envData, err := getEnvelopeJSON(ctx, app, profile)
 	if err != nil {
diff --git a/tools/application/impl_test.go b/tools/application/impl_test.go
index 4917d7d..4bd9452 100644
--- a/tools/application/impl_test.go
+++ b/tools/application/impl_test.go
@@ -11,8 +11,6 @@
 	"v.io/core/veyron2/context"
 	"v.io/core/veyron2/ipc"
 	"v.io/core/veyron2/naming"
-	"v.io/core/veyron2/options"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/security"
 	"v.io/core/veyron2/services/mgmt/application"
 	"v.io/core/veyron2/services/security/access"
@@ -118,18 +116,15 @@
 }
 
 func TestApplicationClient(t *testing.T) {
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
+	defer shutdown()
 	var err error
-	// TODO(ataly, mattr, suharshs): This is a HACK to ensure that the server and the
-	// client have the same freshly created principal. One way to avoid the RuntimePrincipal
-	// option is to have a global client context.T (in main.go) instead of a veyron2.Runtime.
-	runtime, err = rt.New(options.RuntimePrincipal{tsecurity.NewPrincipal("test-blessing")})
-	if err != nil {
-		t.Fatalf("Unexpected error initializing runtime: %s", err)
+	if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+		panic(err)
 	}
-	defer runtime.Cleanup()
-	ctx := runtime.NewContext()
 
-	server, endpoint, err := startServer(t, ctx)
+	server, endpoint, err := startServer(t, gctx)
 	if err != nil {
 		return
 	}
diff --git a/tools/application/main.go b/tools/application/main.go
index 539e0f3..65f7187 100644
--- a/tools/application/main.go
+++ b/tools/application/main.go
@@ -7,20 +7,17 @@
 	"os"
 
 	"v.io/core/veyron2"
-	"v.io/core/veyron2/rt"
+	"v.io/core/veyron2/context"
 
 	_ "v.io/core/veyron/profiles"
 )
 
-var runtime veyron2.Runtime
+var gctx *context.T
 
 func main() {
-	var err error
-	runtime, err = rt.New()
-	if err != nil {
-		panic(err)
-	}
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
 	exitCode := root().Main()
-	runtime.Cleanup()
+	shutdown()
 	os.Exit(exitCode)
 }
diff --git a/tools/binary/main.go b/tools/binary/main.go
index a2b7bfa..65f7187 100644
--- a/tools/binary/main.go
+++ b/tools/binary/main.go
@@ -6,8 +6,8 @@
 import (
 	"os"
 
+	"v.io/core/veyron2"
 	"v.io/core/veyron2/context"
-	"v.io/core/veyron2/rt"
 
 	_ "v.io/core/veyron/profiles"
 )
@@ -15,12 +15,9 @@
 var gctx *context.T
 
 func main() {
-	runtime, err := rt.New()
-	if err != nil {
-		panic(err)
-	}
-	gctx = runtime.NewContext()
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
 	exitCode := root().Main()
-	runtime.Cleanup()
+	shutdown()
 	os.Exit(exitCode)
 }
diff --git a/tools/build/impl.go b/tools/build/impl.go
index 2a91d30..b8e0b90 100644
--- a/tools/build/impl.go
+++ b/tools/build/impl.go
@@ -215,7 +215,7 @@
 	errchan := make(chan error)
 	defer close(errchan)
 
-	ctx, ctxCancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, ctxCancel := context.WithTimeout(gctx, time.Minute)
 	defer ctxCancel()
 
 	// Start all stages of the pipeline.
diff --git a/tools/build/impl_test.go b/tools/build/impl_test.go
index 83e085c..76e1a13 100644
--- a/tools/build/impl_test.go
+++ b/tools/build/impl_test.go
@@ -9,8 +9,6 @@
 	"v.io/core/veyron2/context"
 	"v.io/core/veyron2/ipc"
 	"v.io/core/veyron2/naming"
-	"v.io/core/veyron2/options"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/services/mgmt/binary"
 	"v.io/core/veyron2/services/mgmt/build"
 	verror "v.io/core/veyron2/verror2"
@@ -64,16 +62,15 @@
 }
 
 func TestBuildClient(t *testing.T) {
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
+	defer shutdown()
 	var err error
-	// TODO(ataly, mattr, suharshs): This is a HACK to ensure that the server and the
-	// client have the same freshly created principal. One way to avoid the RuntimePrincipal
-	// option is to have a global client context.T (in main.go) instead of a veyron2.Runtime.
-	runtime, err = rt.New(options.RuntimePrincipal{tsecurity.NewPrincipal("test-blessing")})
-	if err != nil {
-		t.Fatalf("Unexpected error initializing runtime: %s", err)
+	if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+		panic(err)
 	}
-	ctx := runtime.NewContext()
-	server, endpoint := startServer(ctx, t)
+
+	server, endpoint := startServer(gctx, t)
 	defer stopServer(t, server)
 
 	cmd := root()
diff --git a/tools/build/main.go b/tools/build/main.go
index 049d55b..65f7187 100644
--- a/tools/build/main.go
+++ b/tools/build/main.go
@@ -7,22 +7,17 @@
 	"os"
 
 	"v.io/core/veyron2"
-	"v.io/core/veyron2/rt"
+	"v.io/core/veyron2/context"
 
 	_ "v.io/core/veyron/profiles"
 )
 
-var runtime veyron2.Runtime
+var gctx *context.T
 
 func main() {
-	var err error
-	runtime, err = rt.New()
-	if err != nil {
-		panic(err)
-	}
-
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
 	exitCode := root().Main()
-	runtime.Cleanup()
-
+	shutdown()
 	os.Exit(exitCode)
 }
diff --git a/tools/debug/impl.go b/tools/debug/impl.go
index e85ad2a..531e61d 100644
--- a/tools/debug/impl.go
+++ b/tools/debug/impl.go
@@ -87,7 +87,6 @@
 }
 
 func runVtrace(cmd *cmdline.Command, args []string) error {
-	ctx := runtime.NewContext()
 	arglen := len(args)
 	if arglen == 0 {
 		return cmd.UsageErrorf("vtrace: incorrect number of arguments, got %d want >= 1", arglen)
@@ -96,7 +95,7 @@
 	name := args[0]
 	client := vtracesvc.StoreClient(name)
 	if arglen == 1 {
-		call, err := client.AllTraces(ctx)
+		call, err := client.AllTraces(gctx)
 		if err != nil {
 			return err
 		}
@@ -121,7 +120,7 @@
 		if err != nil {
 			return err
 		}
-		go doFetchTrace(ctx, &wg, client, id, traces, errors)
+		go doFetchTrace(gctx, &wg, client, id, traces, errors)
 	}
 	go func() {
 		wg.Wait()
@@ -154,7 +153,7 @@
 	}
 	results := make(chan naming.MountEntry)
 	errors := make(chan error)
-	doGlobs(runtime.NewContext(), args, results, errors)
+	doGlobs(gctx, args, results, errors)
 	var lastErr error
 	for {
 		select {
@@ -220,7 +219,7 @@
 	}
 	name := args[0]
 	lf := logreader.LogFileClient(name)
-	stream, err := lf.ReadLog(runtime.NewContext(), startPos, int32(numEntries), follow)
+	stream, err := lf.ReadLog(gctx, startPos, int32(numEntries), follow)
 	if err != nil {
 		return err
 	}
@@ -263,7 +262,7 @@
 	}
 	name := args[0]
 	lf := logreader.LogFileClient(name)
-	size, err := lf.Size(runtime.NewContext())
+	size, err := lf.Size(gctx)
 	if err != nil {
 		return err
 	}
@@ -287,17 +286,16 @@
 	if min, got := 1, len(args); got < min {
 		return cmd.UsageErrorf("read: incorrect number of arguments, got %d, want >=%d", got, min)
 	}
-	ctx := runtime.NewContext()
 	globResults := make(chan naming.MountEntry)
 	errors := make(chan error)
-	doGlobs(ctx, args, globResults, errors)
+	doGlobs(gctx, args, globResults, errors)
 
 	output := make(chan string)
 	go func() {
 		var wg sync.WaitGroup
 		for me := range globResults {
 			wg.Add(1)
-			go doValue(ctx, me.Name, output, errors, &wg)
+			go doValue(gctx, me.Name, output, errors, &wg)
 		}
 		wg.Wait()
 		close(output)
@@ -348,11 +346,10 @@
 
 	results := make(chan string)
 	errors := make(chan error)
-	ctx := runtime.NewContext()
 	var wg sync.WaitGroup
 	wg.Add(len(args))
 	for _, arg := range args {
-		go doWatch(ctx, arg, results, errors, &wg)
+		go doWatch(gctx, arg, results, errors, &wg)
 	}
 	go func() {
 		wg.Wait()
@@ -456,7 +453,7 @@
 		return showPProfProfiles(cmd, name)
 	}
 	profile := args[1]
-	listener, err := client.StartProxy(runtime, name)
+	listener, err := client.StartProxy(gctx, name)
 	if err != nil {
 		return err
 	}
@@ -479,7 +476,7 @@
 }
 
 func showPProfProfiles(cmd *cmdline.Command, name string) error {
-	v, err := pprof.PProfClient(name).Profiles(runtime.NewContext())
+	v, err := pprof.PProfClient(name).Profiles(gctx)
 	if err != nil {
 		return err
 	}
@@ -516,7 +513,7 @@
 		return cmd.UsageErrorf("proxy: incorrect number of arguments, got %d, want %d", got, want)
 	}
 	name := args[0]
-	listener, err := client.StartProxy(runtime, name)
+	listener, err := client.StartProxy(gctx, name)
 	if err != nil {
 		return err
 	}
@@ -527,9 +524,7 @@
 	fmt.Fprintln(cmd.Stdout())
 	fmt.Fprintln(cmd.Stdout(), "Hit CTRL-C to exit")
 
-	ctx := runtime.NewContext()
-
-	<-signals.ShutdownOnSignals(ctx)
+	<-signals.ShutdownOnSignals(gctx)
 	return nil
 }
 
diff --git a/tools/debug/main.go b/tools/debug/main.go
index cab2605..65f7187 100644
--- a/tools/debug/main.go
+++ b/tools/debug/main.go
@@ -7,21 +7,17 @@
 	"os"
 
 	"v.io/core/veyron2"
-	"v.io/core/veyron2/rt"
+	"v.io/core/veyron2/context"
 
 	_ "v.io/core/veyron/profiles"
 )
 
-var runtime veyron2.Runtime
+var gctx *context.T
 
 func main() {
-	var err error
-	runtime, err = rt.New()
-	if err != nil {
-		panic(err)
-	}
-
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
 	exitCode := root().Main()
-	runtime.Cleanup()
+	shutdown()
 	os.Exit(exitCode)
 }
diff --git a/tools/mgmt/device/main.go b/tools/mgmt/device/main.go
index c23c137..65f7187 100644
--- a/tools/mgmt/device/main.go
+++ b/tools/mgmt/device/main.go
@@ -6,8 +6,8 @@
 import (
 	"os"
 
+	"v.io/core/veyron2"
 	"v.io/core/veyron2/context"
-	"v.io/core/veyron2/rt"
 
 	_ "v.io/core/veyron/profiles"
 )
@@ -15,13 +15,9 @@
 var gctx *context.T
 
 func main() {
-	runtime, err := rt.New()
-	if err != nil {
-		panic(err)
-	}
-	gctx = runtime.NewContext()
-
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
 	exitCode := root().Main()
-	runtime.Cleanup()
+	shutdown()
 	os.Exit(exitCode)
 }
diff --git a/tools/mounttable/impl.go b/tools/mounttable/impl.go
index d949dab..5046a09 100644
--- a/tools/mounttable/impl.go
+++ b/tools/mounttable/impl.go
@@ -27,7 +27,7 @@
 }
 
 func runGlob(cmd *cmdline.Command, args []string) error {
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 
 	if len(args) == 1 {
@@ -102,7 +102,7 @@
 			}
 		}
 	}
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	client := veyron2.GetClient(ctx)
 	call, err := client.StartCall(ctx, args[0], "Mount", []interface{}{args[1], seconds, 0}, options.NoResolve{})
@@ -136,7 +136,7 @@
 	if expected, got := 2, len(args); expected != got {
 		return cmd.UsageErrorf("unmount: incorrect number of arguments, expected %d, got %d", expected, got)
 	}
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	client := veyron2.GetClient(ctx)
 	call, err := client.StartCall(ctx, args[0], "Unmount", []interface{}{args[1]}, options.NoResolve{})
@@ -169,7 +169,7 @@
 	if expected, got := 1, len(args); expected != got {
 		return cmd.UsageErrorf("mount: incorrect number of arguments, expected %d, got %d", expected, got)
 	}
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	client := veyron2.GetClient(ctx)
 	call, err := client.StartCall(ctx, args[0], "ResolveStep", []interface{}{}, options.NoResolve{})
diff --git a/tools/mounttable/impl_test.go b/tools/mounttable/impl_test.go
index 0e7efad..7fd56d3 100644
--- a/tools/mounttable/impl_test.go
+++ b/tools/mounttable/impl_test.go
@@ -9,8 +9,6 @@
 	"v.io/core/veyron2/context"
 	"v.io/core/veyron2/ipc"
 	"v.io/core/veyron2/naming"
-	"v.io/core/veyron2/options"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/security"
 	"v.io/core/veyron2/services/mounttable"
 	"v.io/core/veyron2/services/security/access"
@@ -104,18 +102,15 @@
 }
 
 func TestMountTableClient(t *testing.T) {
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
+	defer shutdown()
 	var err error
-	// TODO(ataly, mattr, suharshs): This is a HACK to ensure that the server and the
-	// client have the same freshly created principal. One way to avoid the RuntimePrincipal
-	// option is to have a global client context.T (in main.go) instead of a veyron2.Runtime.
-	runtime, err = rt.New(options.RuntimePrincipal{tsecurity.NewPrincipal("test-blessing")})
-	if err != nil {
-		t.Fatalf("Unexpected error initializing runtime: %s", err)
+	if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+		panic(err)
 	}
-	defer runtime.Cleanup()
-	ctx := runtime.NewContext()
 
-	server, endpoint, err := startServer(t, ctx)
+	server, endpoint, err := startServer(t, gctx)
 	if err != nil {
 		return
 	}
diff --git a/tools/mounttable/main.go b/tools/mounttable/main.go
index cab2605..65f7187 100644
--- a/tools/mounttable/main.go
+++ b/tools/mounttable/main.go
@@ -7,21 +7,17 @@
 	"os"
 
 	"v.io/core/veyron2"
-	"v.io/core/veyron2/rt"
+	"v.io/core/veyron2/context"
 
 	_ "v.io/core/veyron/profiles"
 )
 
-var runtime veyron2.Runtime
+var gctx *context.T
 
 func main() {
-	var err error
-	runtime, err = rt.New()
-	if err != nil {
-		panic(err)
-	}
-
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
 	exitCode := root().Main()
-	runtime.Cleanup()
+	shutdown()
 	os.Exit(exitCode)
 }
diff --git a/tools/namespace/impl.go b/tools/namespace/impl.go
index cae7c40..53f73ae 100644
--- a/tools/namespace/impl.go
+++ b/tools/namespace/impl.go
@@ -29,7 +29,7 @@
 	}
 	pattern := args[0]
 
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 
 	ns := veyron2.GetNamespace(ctx)
@@ -80,7 +80,7 @@
 		return fmt.Errorf("TTL parse error: %v", err)
 	}
 
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 
 	ns := veyron2.GetNamespace(ctx)
@@ -112,7 +112,7 @@
 	name := args[0]
 	server := args[1]
 
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 
 	ns := veyron2.GetNamespace(ctx)
@@ -140,7 +140,7 @@
 	}
 	name := args[0]
 
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 
 	ns := veyron2.GetNamespace(ctx)
@@ -171,7 +171,7 @@
 	}
 	name := args[0]
 
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 
 	ns := veyron2.GetNamespace(ctx)
diff --git a/tools/namespace/main.go b/tools/namespace/main.go
index 539e0f3..65f7187 100644
--- a/tools/namespace/main.go
+++ b/tools/namespace/main.go
@@ -7,20 +7,17 @@
 	"os"
 
 	"v.io/core/veyron2"
-	"v.io/core/veyron2/rt"
+	"v.io/core/veyron2/context"
 
 	_ "v.io/core/veyron/profiles"
 )
 
-var runtime veyron2.Runtime
+var gctx *context.T
 
 func main() {
-	var err error
-	runtime, err = rt.New()
-	if err != nil {
-		panic(err)
-	}
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
 	exitCode := root().Main()
-	runtime.Cleanup()
+	shutdown()
 	os.Exit(exitCode)
 }
diff --git a/tools/principal/main.go b/tools/principal/main.go
index bb2debd..f39f63c 100644
--- a/tools/principal/main.go
+++ b/tools/principal/main.go
@@ -12,13 +12,12 @@
 	"os/user"
 	"time"
 
-	profile "v.io/core/veyron/profiles/static"
+	_ "v.io/core/veyron/profiles/static"
 	vsecurity "v.io/core/veyron/security"
 	"v.io/core/veyron/services/identity"
 	"v.io/core/veyron2"
 	"v.io/core/veyron2/context"
 	"v.io/core/veyron2/ipc"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/security"
 	"v.io/core/veyron2/vom"
 	"v.io/lib/cmdline"
@@ -60,13 +59,10 @@
 that this tool is running in.
 `,
 		Run: func(cmd *cmdline.Command, args []string) error {
-			runtime, err := rt.New()
-			if err != nil {
-				panic(err)
-			}
-			defer runtime.Cleanup()
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
 
-			p := veyron2.GetPrincipal(runtime.NewContext())
+			p := veyron2.GetPrincipal(ctx)
 			fmt.Printf("Public key : %v\n", p.PublicKey())
 			fmt.Println("---------------- BlessingStore ----------------")
 			fmt.Printf("%v", p.BlessingStore().DebugString())
@@ -154,12 +150,9 @@
 				caveats = append(caveats, cav)
 			}
 
-			runtime, err := rt.New()
-			if err != nil {
-				panic(err)
-			}
-			defer runtime.Cleanup()
-			principal := veyron2.GetPrincipal(runtime.NewContext())
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
+			principal := veyron2.GetPrincipal(ctx)
 			blessing, err := principal.BlessSelf(name, caveats...)
 			if err != nil {
 				return fmt.Errorf("failed to create self-signed blessing for name %q: %v", name, err)
@@ -210,14 +203,12 @@
 				return fmt.Errorf("require exactly two arguments, provided %d", len(args))
 			}
 
-			runtime, err := rt.New()
-			if err != nil {
-				panic(err)
-			}
-			defer runtime.Cleanup()
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
 
-			p := veyron2.GetPrincipal(runtime.NewContext())
+			p := veyron2.GetPrincipal(ctx)
 
+			var err error
 			var with security.Blessings
 			var caveats []security.Caveat
 			if len(flagBlessWith) > 0 {
@@ -242,7 +233,7 @@
 			if len(flagBlessRemoteKey) > 0 {
 				// Send blessings to a "server" started by a "recvblessings" command
 				granter := &granter{p, with, extension, caveats, flagBlessRemoteKey}
-				return sendBlessings(runtime.NewContext(), tobless, granter, flagBlessRemoteToken)
+				return sendBlessings(ctx, tobless, granter, flagBlessRemoteToken)
 			}
 			// Blessing a principal whose key is available locally.
 			var key security.PublicKey
@@ -285,12 +276,9 @@
 blessings set on the store with the "..." pattern).
 `,
 		Run: func(cmd *cmdline.Command, args []string) error {
-			runtime, err := rt.New()
-			if err != nil {
-				panic(err)
-			}
-			defer runtime.Cleanup()
-			principal := veyron2.GetPrincipal(runtime.NewContext())
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
+			principal := veyron2.GetPrincipal(ctx)
 			return dumpBlessings(principal.BlessingStore().ForPeer(args...))
 		},
 	}
@@ -303,12 +291,9 @@
 the environment that this tool is running in.
 `,
 		Run: func(cmd *cmdline.Command, args []string) error {
-			runtime, err := rt.New()
-			if err != nil {
-				panic(err)
-			}
-			defer runtime.Cleanup()
-			principal := veyron2.GetPrincipal(runtime.NewContext())
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
+			principal := veyron2.GetPrincipal(ctx)
 			return dumpBlessings(principal.BlessingStore().Default())
 		},
 	}
@@ -348,13 +333,10 @@
 			}
 			pattern := security.BlessingPattern(args[1])
 
-			runtime, err := rt.New()
-			if err != nil {
-				panic(err)
-			}
-			defer runtime.Cleanup()
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
 
-			p := veyron2.GetPrincipal(runtime.NewContext())
+			p := veyron2.GetPrincipal(ctx)
 			if _, err := p.BlessingStore().Set(blessings, pattern); err != nil {
 				return fmt.Errorf("failed to set blessings %v for peers %v: %v", blessings, pattern, err)
 			}
@@ -396,13 +378,10 @@
 				return fmt.Errorf("failed to decode provided blessings: %v", err)
 			}
 
-			runtime, err := rt.New()
-			if err != nil {
-				panic(err)
-			}
-			defer runtime.Cleanup()
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
 
-			p := veyron2.GetPrincipal(runtime.NewContext())
+			p := veyron2.GetPrincipal(ctx)
 			if err := p.AddToRoots(blessings); err != nil {
 				return fmt.Errorf("AddToRoots failed: %v", err)
 			}
@@ -434,13 +413,10 @@
 				return fmt.Errorf("failed to decode provided blessings: %v", err)
 			}
 
-			runtime, err := rt.New()
-			if err != nil {
-				panic(err)
-			}
-			defer runtime.Cleanup()
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
 
-			p := veyron2.GetPrincipal(runtime.NewContext())
+			p := veyron2.GetPrincipal(ctx)
 			if err := p.BlessingStore().SetDefault(blessings); err != nil {
 				return fmt.Errorf("failed to set blessings %v as default: %v", blessings, err)
 			}
@@ -531,11 +507,8 @@
 				return err
 			}
 
-			runtime, err := rt.New()
-			if err != nil {
-				return err
-			}
-			defer runtime.Cleanup()
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
 
 			var (
 				with    security.Blessings
@@ -546,7 +519,7 @@
 					return fmt.Errorf("failed to read blessings from --with=%q: %v", flagForkWith, err)
 				}
 			} else {
-				with = veyron2.GetPrincipal(runtime.NewContext()).BlessingStore().Default()
+				with = veyron2.GetPrincipal(ctx).BlessingStore().Default()
 			}
 			if c, err := security.ExpiryCaveat(time.Now().Add(flagForkFor)); err != nil {
 				return fmt.Errorf("failed to create ExpiryCaveat: %v", err)
@@ -557,7 +530,7 @@
 			// revocation, method etc.
 
 			key := p.PublicKey()
-			rp := veyron2.GetPrincipal(runtime.NewContext())
+			rp := veyron2.GetPrincipal(ctx)
 			blessings, err := rp.Bless(key, with, extension, caveats[0], caveats[1:]...)
 			if err != nil {
 				return fmt.Errorf("Bless(%v, %v, %q, ...) failed: %v", key, with, extension, err)
@@ -587,11 +560,8 @@
 		Run: func(cmd *cmdline.Command, args []string) error {
 			// Initialize the runtime first so that any local errors are reported
 			// before the HTTP roundtrips for obtaining the macaroon begin.
-			runtime, err := rt.New()
-			if err != nil {
-				panic(err)
-			}
-			defer runtime.Cleanup()
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
 
 			blessedChan := make(chan string)
 			defer close(blessedChan)
@@ -601,7 +571,7 @@
 			}
 			macaroon := <-macaroonChan
 			service := <-macaroonChan
-			ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+			ctx, cancel := context.WithTimeout(ctx, time.Minute)
 			defer cancel()
 
 			var reply security.WireBlessings
@@ -617,7 +587,7 @@
 			// Wait for getTokenForBlessRPC to clean up:
 			<-macaroonChan
 
-			p := veyron2.GetPrincipal(runtime.NewContext())
+			p := veyron2.GetPrincipal(ctx)
 
 			if flagSeekBlessingsSetDefault {
 				if err := p.BlessingStore().SetDefault(blessings); err != nil {
@@ -677,18 +647,15 @@
 				return fmt.Errorf("command accepts no arguments")
 			}
 
-			runtime, err := rt.New()
-			if err != nil {
-				panic(err)
-			}
-			defer runtime.Cleanup()
+			ctx, shutdown := veyron2.Init()
+			defer shutdown()
 
-			server, err := veyron2.NewServer(runtime.NewContext())
+			server, err := veyron2.NewServer(ctx)
 			if err != nil {
 				return fmt.Errorf("failed to create server to listen for blessings: %v", err)
 			}
 			defer server.Stop()
-			eps, err := server.Listen(profile.ListenSpec)
+			eps, err := server.Listen(veyron2.GetListenSpec(ctx))
 			if err != nil {
 				return fmt.Errorf("failed to setup listening: %v", err)
 			}
@@ -697,7 +664,7 @@
 				return fmt.Errorf("unable to generate token: %v", err)
 			}
 
-			p := veyron2.GetPrincipal(runtime.NewContext())
+			p := veyron2.GetPrincipal(ctx)
 			service := &recvBlessingsService{
 				principal: p,
 				token:     base64.URLEncoding.EncodeToString(token[:]),
diff --git a/tools/profile/impl.go b/tools/profile/impl.go
index 1925450..029d8ff 100644
--- a/tools/profile/impl.go
+++ b/tools/profile/impl.go
@@ -26,7 +26,7 @@
 	}
 	name := args[0]
 	p := repository.ProfileClient(name)
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	label, err := p.Label(ctx)
 	if err != nil {
@@ -51,7 +51,7 @@
 	}
 	name := args[0]
 	p := repository.ProfileClient(name)
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	desc, err := p.Description(ctx)
 	if err != nil {
@@ -76,7 +76,7 @@
 	}
 	name := args[0]
 	p := repository.ProfileClient(name)
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	spec, err := p.Specification(ctx)
 	if err != nil {
@@ -111,7 +111,7 @@
 		Label:       "example",
 		OS:          build.Linux,
 	}
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	if err := p.Put(ctx, spec); err != nil {
 		return err
@@ -135,7 +135,7 @@
 	}
 	name := args[0]
 	p := repository.ProfileClient(name)
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	if err := p.Remove(ctx); err != nil {
 		return err
diff --git a/tools/profile/impl_test.go b/tools/profile/impl_test.go
index 75e14d6..891e36d 100644
--- a/tools/profile/impl_test.go
+++ b/tools/profile/impl_test.go
@@ -10,8 +10,6 @@
 	"v.io/core/veyron2/context"
 	"v.io/core/veyron2/ipc"
 	"v.io/core/veyron2/naming"
-	"v.io/core/veyron2/options"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/security"
 	"v.io/core/veyron2/services/mgmt/build"
 	"v.io/core/veyron2/vlog"
@@ -111,18 +109,15 @@
 }
 
 func TestProfileClient(t *testing.T) {
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
+	defer shutdown()
 	var err error
-	// TODO(ataly, mattr, suharshs): This is a HACK to ensure that the server and the
-	// client have the same freshly created principal. One way to avoid the RuntimePrincipal
-	// option is to have a global client context.T (in main.go) instead of a veyron2.Runtime.
-	runtime, err = rt.New(options.RuntimePrincipal{tsecurity.NewPrincipal("test-blessing")})
-	if err != nil {
-		t.Fatalf("Unexpected error initializing runtime: %s", err)
+	if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+		panic(err)
 	}
-	defer runtime.Cleanup()
-	ctx := runtime.NewContext()
 
-	server, endpoint, err := startServer(t, ctx)
+	server, endpoint, err := startServer(t, gctx)
 	if err != nil {
 		return
 	}
diff --git a/tools/profile/main.go b/tools/profile/main.go
index 49f20c5..e9b7573 100644
--- a/tools/profile/main.go
+++ b/tools/profile/main.go
@@ -7,21 +7,17 @@
 	"os"
 
 	"v.io/core/veyron2"
-	"v.io/core/veyron2/rt"
+	"v.io/core/veyron2/context"
 
 	_ "v.io/core/veyron/profiles"
 )
 
-var runtime veyron2.Runtime
+var gctx *context.T
 
 func main() {
-	var err error
-	runtime, err = rt.New()
-	if err != nil {
-		panic(err)
-	}
-
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
 	exitCode := root().Main()
-	runtime.Cleanup()
+	shutdown()
 	os.Exit(exitCode)
 }
diff --git a/tools/vrpc/vrpc.go b/tools/vrpc/vrpc.go
index 66d7b05..a10cf56 100644
--- a/tools/vrpc/vrpc.go
+++ b/tools/vrpc/vrpc.go
@@ -13,7 +13,6 @@
 	"v.io/core/veyron2"
 	"v.io/core/veyron2/context"
 	"v.io/core/veyron2/ipc/reserved"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/vdl"
 	"v.io/core/veyron2/vdl/build"
 	"v.io/core/veyron2/vdl/codegen/vdlgen"
@@ -24,16 +23,13 @@
 	_ "v.io/core/veyron/profiles"
 )
 
-var runtime veyron2.Runtime
+var gctx *context.T
 
 func main() {
-	var err error
-	runtime, err = rt.New()
-	if err != nil {
-		panic(err)
-	}
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
 	exitCode := cmdVRPC.Main()
-	runtime.Cleanup()
+	shutdown()
 	os.Exit(exitCode)
 }
 
@@ -129,7 +125,7 @@
 	}
 	// Get the interface or method signature, and pretty-print.  We print the
 	// named types after the signatures, to aid in readability.
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	var types signature.NamedTypes
 	if method != "" {
@@ -176,7 +172,7 @@
 		}
 	}
 	// Get the method signature and parse args.
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	methodSig, err := reserved.MethodSignature(ctx, server, method)
 	if err != nil {
@@ -264,7 +260,7 @@
 		return cmd.UsageErrorf("wrong number of arguments")
 	}
 	server := args[0]
-	ctx, cancel := context.WithTimeout(runtime.NewContext(), time.Minute)
+	ctx, cancel := context.WithTimeout(gctx, time.Minute)
 	defer cancel()
 	// The method name does not matter - only interested in authentication,
 	// not in actually making an RPC.
diff --git a/tools/vrpc/vrpc_test.go b/tools/vrpc/vrpc_test.go
index f8fd359..573c367 100644
--- a/tools/vrpc/vrpc_test.go
+++ b/tools/vrpc/vrpc_test.go
@@ -7,8 +7,6 @@
 
 	"v.io/core/veyron2"
 	"v.io/core/veyron2/ipc"
-	"v.io/core/veyron2/options"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/vlog"
 
 	tsecurity "v.io/core/veyron/lib/testutil/security"
@@ -109,30 +107,16 @@
 	return nil
 }
 
-func initTest(t *testing.T) (name string, cleanup func()) {
-	// The runtime initialized here is the global runtime defined in vrpc.go.
-	var ipcServer ipc.Server
-	cleanup = func() {
-		if ipcServer != nil {
-			if err := ipcServer.Stop(); err != nil {
-				t.Errorf("server.Stop failed: %v", err)
-			}
-		}
-		if runtime != nil {
-			runtime.Cleanup()
-		}
-	}
+func initTest(t *testing.T) (name string, shutdown veyron2.Shutdown) {
+	// The gctx initialized here is the global context defined in vrpc.go.
+	gctx, shutdown = veyron2.Init()
 	var err error
-	// TODO(ataly, mattr, suharshs): This is a HACK to ensure that the server and the
-	// client have the same freshly created principal. One way to avoid the RuntimePrincipal
-	// option is to have a global client context.T (in main.go) instead of a veyron2.Runtime.
-	if runtime, err = rt.New(options.RuntimePrincipal{tsecurity.NewPrincipal("test-blessing")}); err != nil {
-		t.Fatalf("rt.New() failed: %v", err)
-		return
+	if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+		panic(err)
 	}
-	ctx := runtime.NewContext()
 
-	if ipcServer, err = veyron2.NewServer(ctx); err != nil {
+	ipcServer, err := veyron2.NewServer(gctx)
+	if err != nil {
 		t.Fatalf("NewServer failed: %v", err)
 		return
 	}
@@ -151,8 +135,8 @@
 }
 
 func TestSignature(t *testing.T) {
-	name, cleanup := initTest(t)
-	defer cleanup()
+	name, shutdown := initTest(t)
+	defer shutdown()
 	var stdout, stderr bytes.Buffer
 	cmdVRPC.Init(nil, &stdout, &stderr)
 
@@ -240,8 +224,8 @@
 }
 
 func TestMethodSignature(t *testing.T) {
-	name, cleanup := initTest(t)
-	defer cleanup()
+	name, shutdown := initTest(t)
+	defer shutdown()
 
 	tests := []struct {
 		Method, Want string
@@ -275,8 +259,8 @@
 }
 
 func TestCall(t *testing.T) {
-	name, cleanup := initTest(t)
-	defer cleanup()
+	name, shutdown := initTest(t)
+	defer shutdown()
 
 	tests := []struct {
 		Method, InArgs, Want string