@@ -55,6 +55,7 @@ type Instance struct {
5555 Errors []error `json:"errors,omitempty"`
5656 Config * limayaml.LimaYAML `json:"config,omitempty"`
5757 SSHAddress string `json:"sshAddress,omitempty"`
58+ Protected bool `json:"protected"`
5859}
5960
6061func (inst * Instance ) LoadYAML () (* limayaml.LimaYAML , error ) {
@@ -139,6 +140,11 @@ func Inspect(instName string) (*Instance, error) {
139140 inst .Disk = 0
140141 }
141142
143+ protected := filepath .Join (instDir , filenames .Protected )
144+ if _ , err := os .Lstat (protected ); ! errors .Is (err , os .ErrNotExist ) {
145+ inst .Protected = true
146+ }
147+
142148 inspectStatus (instDir , inst , y )
143149
144150 tmpl , err := template .New ("format" ).Parse (y .Message )
@@ -394,3 +400,27 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
394400 }
395401 return nil
396402}
403+
404+ // Protect protects the instance to prohibit accidental removal.
405+ // Protect does not return an error even when the instance is already protected.
406+ func (inst * Instance ) Protect () error {
407+ protected := filepath .Join (inst .Dir , filenames .Protected )
408+ // TODO: Do an equivalent of `chmod +a "everyone deny delete,delete_child,file_inherit,directory_inherit"`
409+ // https://github.com/lima-vm/lima/issues/1595
410+ if err := os .WriteFile (protected , nil , 0400 ); err != nil {
411+ return err
412+ }
413+ inst .Protected = true
414+ return nil
415+ }
416+
417+ // Unprotect unprotects the instance.
418+ // Unprotect does not return an error even when the instance is already unprotected.
419+ func (inst * Instance ) Unprotect () error {
420+ protected := filepath .Join (inst .Dir , filenames .Protected )
421+ if err := os .RemoveAll (protected ); err != nil {
422+ return err
423+ }
424+ inst .Protected = false
425+ return nil
426+ }
0 commit comments