1

I am trying to monitor the attached USB-Drives of my Raspberry Pi and unmount them on request.

I am using apache2 webserver and html/javascript and php.

Basic description.

When I press the "Refresh button", php checks which devices are mounted at /media/ and creates for each drive an unmount button.

When I press one of the buttons, php shall unmount the selected drive by pumount call.

Problem

The refresh part works fine so far. When I press one of the unmount buttons, it seems that the drive is removed, because it is no longer listed when I refresh again. But when I log in to the raspi via ssh I can see that the drive is not unmounted at all.

QuestionS

Why do I get different results, when I call the same command (lsblk) from apache and normal user.

How can I solve this issue.

Hope somebody can help.

Info Update

  • Same result, when I use exec insted of shell_exec

  • When I restart apache, the drives are shown again. So I asume,that the problem is apache related. Any ideas?

index.html

 <html lang="de">
<head>
    <meta charset="utf-8"/>
    <title>Status</title>
    <script type="text/javascript" src="status.js"></script>
    <link rel="stylesheet" href="css/styles.css">
</head>

<body>
    <h1>USB-Drive Handler</h1>
    <form method="POST" action="">
        <p><input class="button" value="Refresh" type="button" id="refreshButton"  onclick="refresh()"></p>
    </form>
    <p><h2>Avail. USB-Drives:</h2>  <span id="availDrives"></span></p>
</body>

status.js

function remove(device, mnt){
  document.getElementById("availDrives").innerHTML="";
  xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
    document.getElementById("availDrives").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","status.php?command=remove&device="+device,true);
  xmlhttp.send();
}

function refresh(){

//Clear Page
  document.getElementById("availDrives").innerHTML="";

  xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
        document.getElementById("availDrives").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","status.php?command=refresh",true);
  xmlhttp.send();
}

status.php

<?php

   $command = $_GET["command"];
   echo $command;
   switch($command){
     case "refresh" :
        refresh();
        break;
     case "remove" :
        $dev=$_GET["device"];
        remove($dev);
        break;
   }

   function remove($dev){
      echo shell_exec("pumount /dev/$dev");
   }

   function refresh(){

     $blocks = preg_split('/\n/',shell_exec("lsblk -l -o NAME,MOUNTPOINT"));
     foreach ($blocks as $block){
       if (strpos($block, " /media/") !== false){
          $b = preg_split('/\s+/',$block);
          $dev = $b[0];
          $mnt = $b[1];
          echo "<p><input value=\"".$mnt."\" class=\"button button1\" type=\"button\"  onclick=\"remove('$dev' , '$mnt')\"></p>";
       }
     }

   }
 ?>
JustMe
  • 366
  • 1
  • 4
  • 14

0 Answers0