Bash script behave differently when executed from php

94 Views Asked by At

I have a simple script used to recreate and format partitions in a disk that runs fine from the command line, but fails complaining about device being busy at the first command that changes the partition table when called from php. I've done a lot of reading in stackoverflow and other sources and most of the times in similar situations the problem is a relative path and/or permissions that don't let the script run but in my case the script runs but isn't able to do the partitioning/formating.

Things I've checked/tried:

  1. Paths: I use absolute paths/chdir and the script executes.

  2. Permissions: all set to rwx to be on the safe side while testing.

  3. Sudoers file: granted all permissions for testing.

  4. Relevant (imho) php.ini directives: (also for testing purposes)

     disable_functions = 
     safe_mode =
    
  5. fuser command reports nothing.

  6. I've used parted/fdisk/sfdisk and a mix of them just in case, same results.

  7. The script runs as expected with sudo from the command line.

The php code is this:

$path="/usr/share/test";
$logFile=$path . "/partition.log";
chdir(path);
if($_GET['run']) {
    $command="sudo ./partition.sh 2>&1 | tee $logFile";
    shell_exec($command);
}
<a href=?run=true>Run script</a>

And the partitioning script goes like this:


umount -v -f -l /dev/sda1
umount -v -f -l /dev/sda2
umount -v -f -l /dev/sda3
sfdisk --delete /dev/sda
sleep 5
/sbin/partprobe
/sbin/parted -a optimal -s /dev/sda mkpart primary ext4 32.3kB 409600MB
/sbin/parted -a optimal -s /dev/sda mkpart primary ext4 409600MB 614400MB
/sbin/parted -a optimal -s /dev/sda mkpart primary ext4 614400MB 1024GB
sleep 5
/sbin/partprobe
umount -v -f -l /dev/sda1
umount -v -f -l /dev/sda2
umount -v -f -l /dev/sda3
echo "***formating partitions"
/sbin/mkfs.ext4 -F /dev/sda1
/sbin/mkfs.ext4 -F /dev/sda2
/sbin/mkfs.ext4 -F /dev/sda3
echo "***mounting"
mount /dev/sda1 /media/vms
mount /dev/sda2 /media/apps
mount /dev/sda3 /media/novos

I'm unmounting my partitions twice because I discovered the hard way parted mount them automatically and occasionally they've been reported mounted by umount (ref: https://unix.stackexchange.com/questions/432850/is-parted-auto-mounting-new-partitions)

Sample output: (please omit the warning messages, optimization comes later!)

+ umount -v -l /dev/sda1
umount: /media/vms (/dev/sda1) unmounted
+ umount -v -l /dev/sda2
umount: /media/apps (/dev/sda2) unmounted
+ umount -v -l /dev/sda3
umount: /media/novos (/dev/sda3) unmounted
+ sfdisk --delete /dev/sda

The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
Syncing disks.
 + sleep 5
 + /sbin/partprobe
Error: Partition(s) 1, 2, 3 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.
 + /sbin/parted -a optimal -s /dev/sda mkpart primary ext4 32.3kB 409600MB
Warning: The resulting partition is not properly aligned for best performance.
Error: Partition(s) 2, 3 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.
 + /sbin/parted -a optimal -s /dev/sda mkpart primary ext4 409600MB 614400MB
Warning: The resulting partition is not properly aligned for best performance.
Error: Partition(s) 3 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.
 + /sbin/parted -a optimal -s /dev/sda mkpart primary ext4 614400MB 1024GB
Warning: The resulting partition is not properly aligned for best performance.
 + sleep 5
 + /sbin/partprobe
 + umount -v -l /dev/sda1
umount: /dev/sda1: not mounted
 + umount -v -l /dev/sda2
umount: /dev/sda2: not mounted
 + umount -v -l /dev/sda3
umount: /dev/sda3: not mounted
 + echo '***formating partitions'
***formating partitions
 + /sbin/mkfs.ext4 -F /dev/sda1
mke2fs 1.43.4 (31-Jan-2017)
/dev/sda1 is apparently in use by the system; will not make a filesystem here!
 + /sbin/mkfs.ext4 -F /dev/sda2
mke2fs 1.43.4 (31-Jan-2017)
/dev/sda2 is apparently in use by the system; will not make a filesystem here!
 + /sbin/mkfs.ext4 -F /dev/sda3
mke2fs 1.43.4 (31-Jan-2017)
/dev/sda3 is apparently in use by the system; will not make a filesystem here!
 + echo '***mounting'
***mounting
 + mount /dev/sda1 /media/vms
 + mount /dev/sda2 /media/apps
 + mount /dev/sda3 /media/novos

My system runs from a CFast card mounted on /dev/sdc and /dev/sda is used for logging. All tests where made ssh to the box.

  • Linux test 4.9.0-12-rt-amd64 #1 SMP PREEMPT RT Debian 4.9.210-1 (2020-01-20) x86_64 GNU/Linux
  • PHP 7.0.33-0+deb9u7 (cli) (built: Feb 16 2020 15:11:40) ( NTS )
  • parted (GNU parted) 3.2
  • util-linux 2.29.2 (fdisk, sfdisk)

Any pointers/comments will be very appreciated. Thanks in advance.

0

There are 0 best solutions below