cros_vm: use more lib helpers

Re-use existing utility libraries that chromite already provides.

BUG=None
TEST=precq passes

Change-Id: I32b474e01c022ac7c74a06b4825b3b3d37ff90a2
Reviewed-on: https://chromium-review.googlesource.com/663479
Commit-Ready: Achuith Bhandarkar <achuith@chromium.org>
Tested-by: Achuith Bhandarkar <achuith@chromium.org>
Reviewed-by: Achuith Bhandarkar <achuith@chromium.org>
diff --git a/scripts/cros_vm.py b/scripts/cros_vm.py
index 14685dd..f9186a3 100644
--- a/scripts/cros_vm.py
+++ b/scripts/cros_vm.py
@@ -88,10 +88,9 @@
     Args:
       recreate: recreate vm_dir.
     """
-    self._RunCommand(['rm', '-rf', self.vm_dir])
+    osutils.RmDir(self.vm_dir, ignore_missing=True, sudo=self.use_sudo)
     if recreate:
-      self._RunCommand(['mkdir', self.vm_dir])
-      self._RunCommand(['chmod', '777', self.vm_dir])
+      osutils.SafeMakedirs(self.vm_dir)
 
   def PerformAction(self, start=False, stop=False, cmd=None):
     """Performs an action, one of start, stop, or run a command in the VM.
@@ -133,9 +132,11 @@
       raise VMError('VM image path %s does not exist.' % self.image_path)
 
     self._CleanupFiles(recreate=True)
-    open(self.kvm_serial, 'w')
+    # Make sure we can read these files later on by creating them as ourselves.
+    osutils.Touch(self.kvm_serial)
     for pipe in [self.kvm_pipe_in, self.kvm_pipe_out]:
       os.mkfifo(pipe, 0600)
+    osutils.Touch(self.pidfile)
 
     args = [self.qemu_path, '-m', '2G', '-smp', '4', '-vga', 'cirrus',
             '-daemonize',
@@ -151,7 +152,7 @@
       args.append('-enable-kvm')
     if not self.display:
       args.extend(['-display', 'none'])
-    logging.info(' '.join(args))
+    logging.info(cros_build_lib.CmdToStr(args))
     logging.info('Pid file: %s', self.pidfile)
     if not self.dry_run:
       self._RunCommand(args)
@@ -170,10 +171,11 @@
       logging.info('%s does not exist.', self.pidfile)
       return 0
 
-    pid = self._RunCommand(['cat', self.pidfile],
-                           redirect_stdout=True).output.rstrip()
+    pid = osutils.ReadFile(self.pidfile).rstrip()
     if not pid.isdigit():
-      logging.error('%s in %s is not a pid.', pid, self.pidfile)
+      # Ignore blank/empty files.
+      if pid:
+        logging.error('%s in %s is not a pid.', pid, self.pidfile)
       return 0
 
     return int(pid)
@@ -189,8 +191,7 @@
       return False
 
     # Make sure the process actually exists.
-    res = self._RunCommand(['kill', '-0', str(pid)], error_code_ok=True)
-    return res.returncode == 0
+    return os.path.isdir('/proc/%i' % pid)
 
   def Stop(self):
     """Stop the VM."""