"gocage migrate jail -d destination_dataset" working for cold migrations
This commit is contained in:
		
							
								
								
									
										31
									
								
								cmd/root.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								cmd/root.go
									
									
									
									
									
								
							| @ -15,7 +15,7 @@ import ( | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| 	gVersion = "0.26a" | ||||
| 	gVersion = "0.27c" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| @ -34,6 +34,8 @@ var ( | ||||
| 	gTimeZone     string | ||||
| 	gSnapshotName string | ||||
|  | ||||
| 	gMigrateDestPool string | ||||
|  | ||||
| 	rootCmd = &cobra.Command{ | ||||
| 		Use:   "gocage", | ||||
| 		Short: "GoCage is a FreeBSD Jail management tool", | ||||
| @ -189,6 +191,28 @@ You can specify multiple jails.`, | ||||
| 			DeleteJailSnapshot(args) | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	migrateCmd = &cobra.Command{ | ||||
| 		Use:   "migrate", | ||||
| 		Short: "Migrate jail to another zpool", | ||||
| 		Run: func(cmd *cobra.Command, args []string) { | ||||
| 			// Load inventory | ||||
| 			ListJails(args, false) | ||||
| 			MigrateJail(args) | ||||
| 			WriteConfigToDisk(false) | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	migrateCleanCmd = &cobra.Command{ | ||||
| 		Use:   "clean", | ||||
| 		Short: "Clean previous aborted/in error jail migration", | ||||
| 		Run: func(cmd *cobra.Command, args []string) { | ||||
| 			// Load inventory | ||||
| 			ListJails(args, false) | ||||
| 			CleanMigrateMess(args) | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| ) | ||||
|  | ||||
| // TODO : Init log level and log output | ||||
| @ -216,6 +240,9 @@ func init() { | ||||
| 	snapshotRollbackCmd.Flags().StringVarP(&gSnapshotName, "snapname", "n", "", "Name of the snapshot to rollback to") | ||||
| 	snapshotRollbackCmd.MarkFlagRequired("snapname") | ||||
|  | ||||
| 	migrateCmd.Flags().StringVarP(&gMigrateDestPool, "destpool", "d", "", "Name of zfs destination pool for jail") | ||||
| 	migrateCmd.MarkFlagRequired("destpool") | ||||
|  | ||||
| 	// Now declare commands | ||||
| 	rootCmd.AddCommand(versionCmd) | ||||
| 	rootCmd.AddCommand(listCmd) | ||||
| @ -225,10 +252,12 @@ func init() { | ||||
| 	rootCmd.AddCommand(getCmd) | ||||
| 	rootCmd.AddCommand(setCmd) | ||||
| 	rootCmd.AddCommand(snapshotCmd) | ||||
| 	rootCmd.AddCommand(migrateCmd) | ||||
| 	snapshotCmd.AddCommand(snapshotListCmd) | ||||
| 	snapshotCmd.AddCommand(snapshotCreateCmd) | ||||
| 	snapshotCmd.AddCommand(snapshotDeleteCmd) | ||||
| 	snapshotCmd.AddCommand(snapshotRollbackCmd) | ||||
| 	migrateCmd.AddCommand(migrateCleanCmd) | ||||
|  | ||||
| 	// Get FreeBSD version | ||||
| 	out, err := executeCommand("freebsd-version") | ||||
|  | ||||
							
								
								
									
										64
									
								
								cmd/utils.go
									
									
									
									
									
								
							
							
						
						
									
										64
									
								
								cmd/utils.go
									
									
									
									
									
								
							| @ -1,17 +1,18 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"io" | ||||
| 	"os" | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"sort" | ||||
| 	"bufio" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"reflect" | ||||
| 	"sort" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| 	"io/ioutil" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| @ -214,6 +215,59 @@ func executeCommandInJail(jail *Jail, cmdline string) (string, error) { | ||||
| 	return string(out), err | ||||
| } | ||||
|  | ||||
| func zfsSnapshot(dataset string, snapname string) error { | ||||
| 	cmd := fmt.Sprintf("zfs snapshot %s@%s", dataset, snapname) | ||||
| 	out, err := executeCommand(cmd) | ||||
| 	if err != nil { | ||||
| 		return errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out)) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func zfsCopy(src string, dest string) error { | ||||
| 	// First, declare sending process & pipe | ||||
| 	cmd_send := exec.Command("zfs", "send", src) | ||||
| 	stdout_send, err := cmd_send.StdoutPipe() | ||||
| 	if err != nil { | ||||
| 		//fmt.Printf("Error executing command \"zfs send %s\": %v\n", fmt.Sprintf("%s@gocage_mig_init", dsconf), err) | ||||
| 		return errors.New(fmt.Sprintf("Error: %v\n", err)) | ||||
| 	} | ||||
| 	 | ||||
| 	// then declare receiving process & pipe | ||||
| 	cmd_recv := exec.Command("zfs", "receive", dest) | ||||
| 	stdin_recv, err := cmd_recv.StdinPipe() | ||||
| 	if err != nil { | ||||
| 		//fmt.Printf("Error executing command \"zfs receive %s\": %v\n", dest, err) | ||||
| 		return errors.New(fmt.Sprintf("Error: %v\n", err)) | ||||
| 	} | ||||
| 	 | ||||
| 	// Copy data in a go routine | ||||
| 	go io.Copy(stdin_recv, stdout_send) | ||||
| 	 | ||||
| 	// then start processes and wait for finish | ||||
| 	if err := cmd_recv.Start(); err != nil { | ||||
| 		//fmt.Printf("Error: %v\n", err) | ||||
| 		return errors.New(fmt.Sprintf("Error starting receive process: %v\n", err)) | ||||
| 	} | ||||
| 	//fmt.Printf("DEBUG: Start \"zfs send %s\"\n", dsconf) | ||||
| 	if err := cmd_send.Start(); err != nil { | ||||
| 		//fmt.Printf("Error: %v\n", err) | ||||
| 		return errors.New(fmt.Sprintf("Error starting send process: %v\n", err)) | ||||
| 	} | ||||
| 	 | ||||
| 	//fmt.Printf("DEBUG: Wait for zfs send to finish\n") | ||||
| 	if err := cmd_send.Wait(); err != nil { | ||||
| 		//fmt.Printf("Error: zfs send halted with %v\n", err) | ||||
| 		return errors.New(fmt.Sprintf("send halted with: %v\n", err)) | ||||
| 	} | ||||
| 	//fmt.Printf("DEBUG: Wait for zfs recv to finish\n") | ||||
| 	if err := cmd_recv.Wait(); err != nil { | ||||
| 		//fmt.Printf("Error: zfs recv halted with %v\n", err) | ||||
| 		return errors.New(fmt.Sprintf("receive halted with: %v\n", err)) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
| /***************************************************************************** | ||||
|  * | ||||
|  * rc.conf management | ||||
|  | ||||
		Reference in New Issue
	
	Block a user