@@ -182,9 +182,9 @@ private static class UI {
182182 public Runnable device_addr_complete ;
183183
184184 public PopupPanel snapshots_popup ;
185- public VirtualMachine snapshots_vm ;
185+ public String snapshots_vm ;
186186 public Runnable snapshots_init ;
187- public Snapshot [] snapshots_list ;
187+ public String [] [] snapshots_list ;
188188
189189 public PopupPanel snapshots_add_popup ;
190190 public Runnable snapshots_add_init ;
@@ -755,6 +755,11 @@ private PopupPanel vm_network_PopupPanel(UI ui) {
755755 return panel ;
756756 }
757757
758+ private static final int SS_NAME = 0 ;
759+ private static final int SS_DESC = 1 ;
760+ private static final int SS_PARENT = 2 ;
761+ private static final int SS_CURRENT = 3 ;
762+
758763 private PopupPanel snapshots_PopupPanel (UI ui ) {
759764 PopupPanel panel = new PopupPanel ("Snapshots" );
760765 panel .setPosition (256 , 128 );
@@ -792,16 +797,16 @@ private PopupPanel snapshots_PopupPanel(UI ui) {
792797
793798 ui .snapshots_init = () -> {
794799 if (ui .snapshots_vm == null ) return ;
795- ui .snapshots_list = ui .snapshots_vm . snapshotList ( );
796- Snapshot current = ui .snapshots_vm . snapshotGetCurrent ( );
800+ ui .snapshots_list = ui .host . snapshot_list ( ui . snapshots_vm );
801+ String current = ui .host . snapshot_get_current ( ui . snapshots_vm );
797802 if (current != null ) {
798- if (debug ) JFLog .log ("VM:snapshotList:current=" + current . name );
803+ if (debug ) JFLog .log ("VM:snapshotList:current=" + current );
799804 }
800805 table .removeAll ();
801806 table .addRow (new String [] {"Name" , "Description" , "Parent" , "Current" });
802807 if (ui .snapshots_list == null ) return ;
803- for (Snapshot ss : ui .snapshots_list ) {
804- table .addRow (new String [] { ss . name , ss . desc , ss . parent , ss . current ? "yes" : "" } );
808+ for (String [] ss : ui .snapshots_list ) {
809+ table .addRow (ss );
805810 }
806811 };
807812
@@ -824,8 +829,8 @@ private PopupPanel snapshots_PopupPanel(UI ui) {
824829 Task task = new Task (createEvent ("Delete Snapshot" , ui )) {
825830 public void doTask () {
826831 try {
827- Snapshot ss = ui .snapshots_list [idx ];
828- if (! ui .snapshots_vm . snapshotDelete ( ss . name ) ) {
832+ String [] ss = ui .snapshots_list [idx ];
833+ if (ui .host . snapshot_delete ( ui . snapshots_vm , ss [ SS_NAME ]) == null ) {
829834 throw new Exception ("Delete failed" );
830835 }
831836 setResult ("Completed" , true );
@@ -843,8 +848,8 @@ public void doTask() {
843848 errmsg .setText ("" );
844849 int idx = table .getSelectedRow ();
845850 if (idx == -1 ) return ;
846- Snapshot ss = ui .snapshots_list [idx ];
847- if (ss . current ) {
851+ String [] ss = ui .snapshots_list [idx ];
852+ if (ss [ SS_CURRENT ]. equals ( "yes" ) ) {
848853 errmsg .setText ("Error:snapshot already current" );
849854 return ;
850855 }
@@ -854,7 +859,7 @@ public void doTask() {
854859 Task task = new Task (createEvent ("Restore Snapshot" , ui )) {
855860 public void doTask () {
856861 try {
857- if (! ui .snapshots_vm . snapshotRestore ( ss . name ) ) {
862+ if (ui .host . snapshot_restore ( ui . snapshots_vm , ss [ SS_NAME ]) == null ) {
858863 throw new Exception ("Restore failed" );
859864 }
860865 setResult ("Completed" , true );
@@ -919,7 +924,8 @@ private PopupPanel snapshots_add_PopupPanel(UI ui) {
919924 desc .setText ("" );
920925 cb_memory .setSelected (false );
921926 if (ui .snapshots_vm != null ) {
922- int state = ui .snapshots_vm .getState ();
927+ String str_state = ui .host .vm_get_state (ui .snapshots_vm );
928+ int state = VirtualMachine .getState (str_state );
923929 if (state == VirtualMachine .STATE_ON || state == VirtualMachine .STATE_SUSPEND ) {
924930 cb_memory .setDisabled (false );
925931 } else {
@@ -944,7 +950,7 @@ public void doTask() {
944950 if (!cb_memory .isSelected ()) {
945951 flags |= VirtualMachine .SNAPSHOT_CREATE_DISK_ONLY ;
946952 }
947- if (! ui .snapshots_vm . snapshotCreate ( _name , _desc , flags )) {
953+ if (ui .host . snapshot_create ( ui . snapshots_vm , _name , _desc , flags ) == null ) {
948954 throw new Exception ("create failed" );
949955 }
950956 setResult ("Completed" , true );
@@ -3107,7 +3113,7 @@ public void doTask() {
31073113 errmsg .setText ("Error:no selection" );
31083114 return ;
31093115 }
3110- ui .snapshots_vm = vms [idx ];
3116+ ui .snapshots_vm = vms [idx ]. name ;
31113117 ui .snapshots_init .run ();
31123118 ui .snapshots_popup .setVisible (true );
31133119 });
@@ -3202,6 +3208,10 @@ public void doTask() {
32023208 return panel ;
32033209 }
32043210
3211+ private static final int VM_NAME = 0 ;
3212+ private static final int VM_STATE = 1 ;
3213+ private static final int VM_STORAGE = 2 ;
3214+
32053215 private void vmsPanel_addHost (Panel panel , Host host , UI ui ) {
32063216 Row row ;
32073217
@@ -3221,6 +3231,8 @@ private void vmsPanel_addHost(Panel panel, Host host, UI ui) {
32213231 tools .add (restart );
32223232 Button poweroff = new Button (new Icon ("power" ), "PowerOff" );
32233233 tools .add (poweroff );
3234+ Button snapshots = new Button (new Icon ("clock" ), "Snapshots" );
3235+ tools .add (snapshots );
32243236
32253237 row = new Row ();
32263238 panel .add (row );
@@ -3256,7 +3268,7 @@ private void vmsPanel_addHost(Panel panel, Host host, UI ui) {
32563268 errmsg .setText ("Error:no selection" );
32573269 return ;
32583270 }
3259- String vm_name = host_vms [idx ][0 ];
3271+ String vm_name = host_vms [idx ][VM_NAME ];
32603272 Hardware hardware = ui .host .vm_load (vm_name );
32613273 if (hardware == null ) {
32623274 errmsg .setText ("Error:Failed to load config for vm:" + vm_name );
@@ -3274,8 +3286,8 @@ private void vmsPanel_addHost(Panel panel, Host host, UI ui) {
32743286 errmsg .setText ("Error:no selection" );
32753287 return ;
32763288 }
3277- String vm_name = host_vms [idx ][0 ];
3278- int state = VirtualMachine .getState (host_vms [idx ][1 ]);
3289+ String vm_name = host_vms [idx ][VM_NAME ];
3290+ int state = VirtualMachine .getState (host_vms [idx ][VM_STATE ]);
32793291 if (state != VirtualMachine .STATE_OFF && state != VirtualMachine .STATE_SUSPEND ) {
32803292 errmsg .setText ("Error:VM is already running." );
32813293 return ;
@@ -3304,8 +3316,8 @@ public void doTask() {
33043316 errmsg .setText ("Error:no selection" );
33053317 return ;
33063318 }
3307- String vm_name = host_vms [idx ][0 ];
3308- int state = VirtualMachine .getState (host_vms [idx ][1 ]);
3319+ String vm_name = host_vms [idx ][VM_NAME ];
3320+ int state = VirtualMachine .getState (host_vms [idx ][VM_STATE ]);
33093321 if (state != VirtualMachine .STATE_ON ) {
33103322 errmsg .setText ("Error:VM is not running." );
33113323 return ;
@@ -3334,8 +3346,8 @@ public void doTask() {
33343346 errmsg .setText ("Error:no selection" );
33353347 return ;
33363348 }
3337- String vm_name = host_vms [idx ][0 ];
3338- int state = VirtualMachine .getState (host_vms [idx ][1 ]);
3349+ String vm_name = host_vms [idx ][VM_NAME ];
3350+ int state = VirtualMachine .getState (host_vms [idx ][VM_STATE ]);
33393351 if (state != VirtualMachine .STATE_ON ) {
33403352 errmsg .setText ("Error:VM is not running." );
33413353 return ;
@@ -3364,8 +3376,8 @@ public void doTask() {
33643376 errmsg .setText ("Error:no selection" );
33653377 return ;
33663378 }
3367- String vm_name = host_vms [idx ][0 ];
3368- int state = VirtualMachine .getState (host_vms [idx ][1 ]);
3379+ String vm_name = host_vms [idx ][VM_NAME ];
3380+ int state = VirtualMachine .getState (host_vms [idx ][VM_STATE ]);
33693381 if (state != VirtualMachine .STATE_ON ) {
33703382 errmsg .setText ("Error:VM is not running." );
33713383 return ;
@@ -3394,8 +3406,8 @@ public void doTask() {
33943406 errmsg .setText ("Error:no selection" );
33953407 return ;
33963408 }
3397- String vm_name = host_vms [idx ][0 ];
3398- int state = VirtualMachine .getState (host_vms [idx ][1 ]);
3409+ String vm_name = host_vms [idx ][VM_NAME ];
3410+ int state = VirtualMachine .getState (host_vms [idx ][VM_STATE ]);
33993411 if (state != VirtualMachine .STATE_ON && state != VirtualMachine .STATE_SUSPEND ) {
34003412 errmsg .setText ("Error:VM is not running." );
34013413 return ;
@@ -3416,6 +3428,23 @@ public void doTask() {
34163428 };
34173429 ui .confirm_popup .setVisible (true );
34183430 });
3431+ snapshots .addClickListener ((me , cmp ) -> {
3432+ ui .host = host ;
3433+ errmsg .setText ("" );
3434+ if (Linux .distro == Linux .DistroTypes .Debian && Linux .derived == Linux .DerivedTypes .Unknown ) {
3435+ errmsg .setText ("Debian snapshot support is broken, please use Ubuntu!" );
3436+ return ;
3437+ }
3438+ int idx = host_table .getSelectedRow ();
3439+ if (idx == -1 ) {
3440+ errmsg .setText ("Error:no selection" );
3441+ return ;
3442+ }
3443+ String vm_name = host_vms [idx ][VM_NAME ];
3444+ ui .snapshots_vm = vm_name ;
3445+ ui .snapshots_init .run ();
3446+ ui .snapshots_popup .setVisible (true );
3447+ });
34193448 }
34203449
34213450 private Panel vmAddPanel (VirtualMachine vm , Hardware hardware , UI ui ) {
@@ -7307,6 +7336,8 @@ public void doTask() {
73077336 list .append (ss .desc );
73087337 list .append ("\t " );
73097338 list .append (ss .parent );
7339+ list .append ("\t " );
7340+ list .append (ss .current ? "yes" : "" );
73107341 list .append ("\n " );
73117342 }
73127343 return list .toString ().getBytes ();
@@ -7340,6 +7371,26 @@ public void doTask() {
73407371 Tasks .tasks .addTask (null , task );
73417372 return result .getBytes ();
73427373 }
7374+ case "snapshot_get_current" : {
7375+ String token = params .get ("token" );
7376+ if (!token .equals (Config .current .token )) return null ;
7377+ String vm_name = params .get ("vm" );
7378+ String result = "" ;
7379+ try {
7380+ VirtualMachine vm = VirtualMachine .get (vm_name );
7381+ if (vm == null ) {
7382+ throw new Exception ("VM not found" );
7383+ }
7384+ Snapshot ss = vm .snapshotGetCurrent ();
7385+ if (ss != null ) {
7386+ result = ss .name ;
7387+ }
7388+ } catch (Exception e ) {
7389+ JFLog .log (e );
7390+ result = "error" ;
7391+ }
7392+ return result .getBytes ();
7393+ }
73437394 case "backup" : {
73447395 String token = params .get ("token" );
73457396 if (!token .equals (Config .current .token )) return null ;
@@ -7819,6 +7870,23 @@ public void doTask() {
78197870 Tasks .tasks .addTask (null , task );
78207871 return result .getBytes ();
78217872 }
7873+ case "vm_get_state" : {
7874+ String token = params .get ("token" );
7875+ if (!token .equals (Config .current .token )) return null ;
7876+ String vm_name = params .get ("vm" );
7877+ String result = "" ;
7878+ try {
7879+ VirtualMachine vm = VirtualMachine .get (vm_name );
7880+ if (vm == null ) {
7881+ throw new Exception ("VM not found" );
7882+ }
7883+ result = Integer .toString (vm .getState ());
7884+ } catch (Exception e ) {
7885+ JFLog .log (e );
7886+ result = "error" ;
7887+ }
7888+ return result .getBytes ();
7889+ }
78227890 case "vm_start" : {
78237891 String token = params .get ("token" );
78247892 if (!token .equals (Config .current .token )) return null ;
0 commit comments