]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/extrahd.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2023 IPFire Team <info@ipfire.org> #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
23 # enable only the following on debugging purpose
25 #use CGI::Carp 'fatalsToBrowser';
27 require '/var/ipfire/general-functions.pl' ;
28 require "${General::swroot}/lang.pl" ;
29 require "${General::swroot}/header.pl" ;
31 my %extrahdsettings = ();
32 my $errormessage = "" ;
34 # Hash to store the configured drives.
35 my %configured_drives ;
37 # SYSFS directory which contains all block device data.
38 my $sysfs_block_dir = "/sys/class/block" ;
40 # Array which contains the valid mount directories.
41 # Only mounting to subdirectories inside them is allowed.
42 my @valid_mount_dirs = (
48 # Grab all available block devices.
49 my @devices = & get_block_devices
();
51 # Grab all known UUID's.
52 my %uuids = & get_uuids
();
54 # Grab all mountpoints.
55 my %mountpoints = & get_mountpoints
();
57 # Omit the file system types of the mounted devices.
58 my %filesystems = & get_mountedfs
();
60 # Gather all used swap devices.
61 my @swaps = & get_swaps
();
63 # The config file which contains the configured devices.
64 my $devicefile = "/var/ipfire/extrahd/devices" ;
66 #workaround to suppress a warning when a variable is used only once
67 my @dummy = ( ${ Header
:: colourgreen
}, ${ Header
:: colourred
} );
70 & Header
:: showhttpheaders
();
72 ### Values that have to be initialized
73 $extrahdsettings { 'PATH' } = '' ;
74 $extrahdsettings { 'FS' } = '' ;
75 $extrahdsettings { 'DEVICE' } = '' ;
76 $extrahdsettings { 'ACTION' } = '' ;
77 $extrahdsettings { 'UUID' } = '' ;
79 & Header
:: getcgihash
( \
%extrahdsettings );
81 & Header
:: openpage
( 'ExtraHD' , 1 , '' );
82 & Header
:: openbigbox
( '100%' , 'left' , '' , $errormessage );
84 ############################################################################################################################
85 ############################################################################################################################
90 if ( $extrahdsettings { 'ACTION' } eq $Lang :: tr
{ 'add' }) {
91 # Check if a mount path has been given.
92 if ( not $extrahdsettings { 'PATH' }) {
93 $errormessage = " $Lang ::tr{'extrahd no mount point given'}." ;
95 # Check if a valid mount path has been choosen.
96 } elsif ( not & is_valid_dir
( " $extrahdsettings {'PATH'}" )) {
97 $errormessage = " $Lang ::tr{'extrahd you cant mount'} $extrahdsettings {'DEVICE'} $Lang ::tr{'extrahd to'} $extrahdsettings {'PATH'} $Lang ::tr{'extrahd because it is outside the allowed mount path'}." ;
99 # Check if the given path allready is mounted somewhere.
100 } elsif (& is_mounted
( " $extrahdsettings {'PATH'}" )) {
101 $errormessage = " $Lang ::tr{'extrahd you cant mount'} $extrahdsettings {'DEVICE'} $Lang ::tr{'extrahd to'} $extrahdsettings {'PATH'} $Lang ::tr{'extrahd because there is already a device mounted'}." ;
104 # Check against may previously configured drives.
105 unless ( $errormessage ) {
106 # Open device file for reading.
107 open ( FILE
, "< $devicefile " ) or die "Unable to read $devicefile " ;
108 my @devices = < FILE
>;
111 # Loop through the entries line-by-line.
112 foreach my $entry ( sort @devices ) {
113 # Split the line into pieces and assign nice variables.
114 my ( $uuid , $fs , $path ) = split ( /\;/ , $entry );
116 # Check if the path is allready used.
117 if ( " $extrahdsettings {'PATH'}" eq " $path " ) {
118 $errormessage = " $Lang ::tr{'extrahd you cant mount'} $extrahdsettings {'DEVICE'} $Lang ::tr{'extrahd to'} $extrahdsettings {'PATH'} $Lang ::tr{'extrahd because there is already a device mounted'}." ;
121 # Check if the uuid is allready used.
122 if ( " $extrahdsettings {'DEVICE'} eq $uuid " ) {
123 $errormessage = " $extrahdsettings {'DEVICE'} is allready mounted." ;
128 # Go further if there was no error message.
129 unless ( $errormessage ) {
130 # Re-open the device file for writing.
131 open ( FILE
, ">> $devicefile " ) or die "Unable to write $devicefile " ;
133 # Write the config line.
134 print FILE
"UUID= $extrahdsettings {'UUID'}; $extrahdsettings {'FS'}; $extrahdsettings {'PATH'}; \n " ;
139 # Call helper binary to mount the device.
140 & General
:: system ( "/usr/local/bin/extrahdctrl" , "mount" , " $extrahdsettings {'PATH'}" );
144 # Remove an existing one.
146 } elsif ( $extrahdsettings { 'ACTION' } eq $Lang :: tr
{ 'delete' }) {
147 # Call helper binary to unmount the device.
148 unless (& General
:: system ( "/usr/local/bin/extrahdctrl" , "umount" , " $extrahdsettings {'PATH'}" )) {
149 # Open the device file for reading.
150 open ( FILE
, "< $devicefile " ) or die "Unable to read $devicefile " ;
152 # Read the file content into a temporary array.
158 # Re-open device file for writing.
159 open ( FILE
, "> $devicefile " ) or die "Unable to write $devicefile " ;
161 # Loop through the previous read file content.
162 foreach my $line ( sort @tmp ) {
163 # Split line content and assign nice variables.
164 my ( $uuid , $fs , $path ) = split ( /\;/ , $line );
166 # Write the line in case it does not contain our element to delete.
167 if ( $path ne $extrahdsettings { 'PATH' }) {
175 $errormessage = " $Lang ::tr{'extrahd cant umount'} $extrahdsettings {'PATH'} $Lang ::tr{'extrahd maybe the device is in use'}?" ;
180 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'error messages' });
181 print "<class name='base'> $errormessage \n " ;
182 print " </class> \n " ;
186 ############################################################################################################################
187 ############################################################################################################################
189 & Header
:: openbox
( '100%' , 'center' , $Lang :: tr
{ 'extrahd detected drives' });
191 # Re-read mountpoints.
192 %mountpoints = & get_mountpoints
();
194 # Read-in the device config file.
195 open ( FILE
, "< $devicefile " ) or die "Unable to read $devicefile " ;
197 # Loop through the file content.
199 # Cut the line into pieces.
200 my ( $uuid , $fs , $path ) = split ( /\;/ , $_ );
202 # Add the found entry to the hash of configured drives.
203 $configured_drives { $uuid } = $path ;
206 # Close the file handle.
210 <table border='0' width='600' cellspacing="0">
213 foreach my $device ( sort @devices ) {
214 # Grab the device details.
215 my $vendor = & get_device_vendor
( $device );
216 my $model = & get_device_model
( $device );
217 my $bsize = & get_device_size
( $device );
219 # Convert size into human-readable format.
220 my $size = & General
:: formatBytes
( $bsize );
223 <tr><td colspan="5"> </td></tr>
224 <tr><td align='left' colspan="2"><b>/dev/ $device </b></td>
225 <td align='center' colspan="2"> $vendor $model </td>
227 <td align='center'> $Lang ::tr{'size'} $size </td>
229 <tr><td colspan="5"> </td></tr>
233 # Grab the known partitions of the current block device.
234 my @partitions = & get_device_partitions
( $device );
236 foreach my $partition ( @partitions ) {
239 # Omit the partition size.
240 my $bsize = & get_device_size
( $partition );
242 # Convert into human-readable format.
243 my $size = & General
:: formatBytes
( $bsize );
245 # Try to omit the used filesystem.
246 my $fs = $filesystems { $partition };
248 # Get the mountpoint.
249 my $mountpoint = $mountpoints { $partition };
251 # If no mountpoint could be determined try to grab from
253 unless ( $mountpoint ) {
254 my $uuid = $uuids { $partition };
257 $uuid = "UUID=" . $uuid ;
259 # Try to obtain a possible moutpoint from configured drives.
260 $mountpoint = $configured_drives { $uuid } if ( $configured_drives { $uuid });
263 # Check if the mountpoint is used as root or boot device.
264 if ( $mountpoint eq "/" or $mountpoint =~ "^/boot" ) {
265 $disabled = "disabled" ;
267 # Check if it is mounted.
268 } elsif (& is_mounted
( $mountpoint )) {
269 $disabled = "disabled" ;
271 # Check if the device is used as swap.
272 } elsif (& is_swap
( $partition )) {
273 $disabled = "disabled" ;
274 $mountpoint = "swap" ;
280 <form method='post' action=' $ENV {'SCRIPT_NAME'}'>
281 <tr><td align="left" colspan=5><strong>UUID= $uuids { $partition }</strong></td></tr>
283 <td align="list">/dev/ $partition </td>
284 <td align="center"> $Lang ::tr{'size'} $size </td>
285 <td align="center"> $fs </td>
286 <td align="center"><input type='text' name='PATH' value=' $mountpoint ' $disabled ></td>
288 <input type='hidden' name='DEVICE' value='/dev/ $partition ' />
289 <input type='hidden' name='UUID' value=' $uuids { $partition }' />
292 # Check if the mountpoint refers to a known configured drive.
293 if (& is_configured
( $mountpoint )) {
294 print "<input type='hidden' name='ACTION' value=' $Lang ::tr{'delete'}'> \n " ;
295 print "<input type='hidden' name='PATH' value=' $mountpoint '> \n " ;
297 # Check if the device is mounted properly.
298 if (& is_mounted
( $mountpoint )) {
299 print "<img src='/images/updbooster/updxl-led-green.gif' alt=' $Lang ::tr{'extrahd mounted'}' title=' $Lang ::tr{'extrahd mounted'}'> \n " ;
301 print "<img src='/images/updbooster/updxl-led-red.gif' alt=' $Lang ::tr{'extrahd not mounted'}' title=' $Lang ::tr{'extrahd not mounted'}'> \n " ;
304 print "<input type='image' alt=' $Lang ::tr{'delete'}' title=' $Lang ::tr{'delete'}' src='/images/delete.gif'> \n " ;
307 print "<input type='hidden' name='ACTION' value=' $Lang ::tr{'add'}'> \n " ;
308 print "<input type='hidden' name='FS' value='auto'> \n " ;
309 print "<img src='/images/updbooster/updxl-led-gray.gif' alt=' $Lang ::tr{'extrahd not configured'}' title=' $Lang ::tr{'extrahd not configured'}'> \n " ;
310 print "<input type='image' alt=' $Lang ::tr{'add'}' title=' $Lang ::tr{'add'}' src='/images/add.gif'> \n " ;
322 <tr><td align="center" colspan="5"> </td></tr>
323 <tr><td align="center" colspan="5"> </td></tr>
324 <tr><td align="center" colspan="5"> $Lang ::tr{'extrahd install or load driver'}</td></tr>
330 & Header
:: closebigbox
();
331 & Header
:: closepage
();
334 ## Function which return an array with all available block devices.
336 sub get_block_devices
() {
339 # Open directory from kernel sysfs.
340 opendir ( DEVICES
, "/sys/block" );
342 # Loop through the directory.
343 while ( readdir ( DEVICES
)) {
345 next if ( $_ =~ /^\.$/ );
346 next if ( $_ =~ /^\..$/ );
348 # Skip any loopback and ram devices.
349 next if ( $_ =~ "^loop" );
350 next if ( $_ =~ "^ram" );
352 # Add the device to the array of found devices.
356 # Close directory handle.
359 # Return the devices array.
364 ## Function which return all partitions of a given block device.
366 sub get_device_partitions
($) {
369 # Array to store the known partitions for the given
373 # Assign device directory.
374 my $device_dir = " $sysfs_block_dir / $device " ;
376 # Abort and return nothing if the device dir does not exist.
377 return unless (- d
" $device_dir " );
379 opendir ( DEVICE
, " $sysfs_block_dir / $device " );
380 while ( readdir ( DEVICE
)) {
381 next unless ( $_ =~ "^ $device " );
383 push ( @partitions , $_ );
388 @partitions = sort ( @partitions );
394 ## Returns the vendor of a given block device.
396 sub get_device_vendor
($) {
399 # Assign device directory.
400 my $device_dir = " $sysfs_block_dir / $device " ;
402 # Abort and return nothing if the device dir does not exist
403 # or no vendor file exists.
404 return unless (- d
" $device_dir " );
405 return unless (- f
" $device_dir /device/vendor" );
407 # Open and read-in the device vendor.
408 open ( VENDOR
, " $device_dir /device/vendor" );
409 my $vendor = < VENDOR
>;
412 # Abort and return nothing if no vendor could be read.
413 return unless ( $vendor );
415 # Remove any newlines from the vendor string.
418 # Return the omited vendor.
423 ## Returns the model name (string) of a given block device.
425 sub get_device_model
($) {
428 # Assign device directory.
429 my $device_dir = " $sysfs_block_dir / $device " ;
431 # Abort and return nothing if the device dir does not exist
432 # or no model file exists.
433 return unless (- d
" $device_dir " );
434 return unless (- f
" $device_dir /device/model" );
436 # Open and read-in the device model.
437 open ( MODEL
, " $device_dir /device/model" );
441 # Abort and return nothing if no model could be read.
442 return unless ( $model );
444 # Remove any newlines from the model string.
447 # Return the model string.
452 ## Returns the size of a given device in bytes.
454 sub get_device_size
($) {
457 # Assign device directory.
458 my $device_dir = " $sysfs_block_dir / $device " ;
460 # Abort and return nothing if the device dir does not exist
461 # or no size file exists.
462 return unless (- d
" $device_dir " );
463 return unless (- f
" $device_dir /size" );
465 # Open and read-in the device size.
466 open ( SIZE
, " $device_dir /size" );
470 # Abort and return nothing if the size could not be read.
471 return unless ( $size );
473 # Remove any newlines for the size string.
476 # The omited size only contains the amount of blocks from the
477 # given device. To convert this into bytes we have to multiply this
478 # value with 512 bytes for each block. This is a static value used by
482 # Return the size in bytes.
487 ## Function which returns all currently mounted devices as a hash.
488 ## example: "sda1" -> "/boot"
490 sub get_mountpoints
() {
493 # Open and read-in the current mounts from the
494 # kernel file system.
495 open ( MOUNT
, "/proc/mounts" );
497 # Loop through the known mounts.
499 # Skip mounts which does not belong to a device.
500 next unless ( $_ =~ "^/dev" );
502 # Cut the line into pieces and assign nice variables.
503 my ( $dev , $mpoint , $fs , $options , $a , $b ) = split ( / / , $_ );
505 # Split the device name.
506 my @tmp = split ( "/" , $dev );
508 # Assign the plain device name to a new variable.
509 # It is the last element of the array.
510 my $device = $tmp [- 1 ];
512 # Add the mountpoint to the hash of mountpoints.
513 $mounts { " $device " } = $mpoint ;
519 # Return the hash of known mountpoints.
526 # Open and read the swaps file.
527 open ( SWAP
, "/proc/swaps" );
529 # Loop though the file content.
531 # Skip lines which does not belong to a device.
532 next unless ( $_ =~ "^/dev" );
534 # Split the line and assign nice variables.
535 my ( $dev , $type , $size , $used , $prio ) = split ( / / , $_ );
537 # Cut the device line into pieces.
538 my @tmp = split ( "/" , $dev );
540 my $device = @tmp [- 1 ];
542 # Add the found swap to the array of swaps.
543 push ( @swaps , $device );
550 @swaps = sort ( @swaps );
557 ## Function with returns the mounted devices and the used filesystems as a hash.
558 ## Example: "sda1" -> "ext4"
560 sub get_mountedfs
() {
563 # Open and read the current mounts from the kernel
565 open ( MOUNT
, "/proc/mounts" );
567 # Loop through the known mounts.
569 # Skip mounts which does not belong to a device.
570 next unless ( $_ =~ "^/dev" );
572 # Split line and assign nice variables.
573 my ( $dev , $mpoint , $fs , $options , $a , $b ) = split ( / / , $_ );
575 # Cut the device line into pieces.
576 my @tmp = split ( "/" , $dev );
578 # Assign the plain device name to a variable
579 # It is the last element of the temporary array.
580 my $device = $tmp [- 1 ];
582 # Convert the filesystem into lower case format.
585 # Add the mounted file system.
586 $mountedfs { $device } = $fs ;
592 # Return the hash with the mounted filesystems.
597 ## Function which returns all known UUID's as a hash.
598 ## Example: "sda1" -> "1234-5678-abcd"
603 # Directory where the uuid mappings can be found.
604 my $uuid_dir = "/dev/disk/by-uuid" ;
606 # Open uuid directory and read-in the current known uuids.
607 opendir ( UUIDS
, " $uuid_dir " );
609 # Loop through the uuids.
610 foreach my $uuid ( readdir ( UUIDS
)) {
612 next if ( $uuid eq "." or $uuid eq ".." );
614 # Skip everything which is not a symbolic link.
615 next unless (- l
" $uuid_dir / $uuid " );
617 # Resolve the target of the symbolic link.
618 my $target = readlink ( " $uuid_dir / $uuid " );
620 # Split the link target into pieces.
621 my @tmp = split ( "/" , $target );
623 # Assign the last element of the array to the dev variable.
624 my $dev = " $tmp [-1]" ;
626 # Add the device and uuid to the hash of uuids.
627 $uuids { $dev } = $uuid ;
630 # Close directory handle.
633 # Return the hash of uuids.
638 ## Returns the device name of a given uuid.
640 sub device_by_uuid
($) {
643 # Reverse the main uuids hash.
644 my %uuids = reverse %uuids ;
646 # Lookup and return the device name.
647 return $uuids { $uuid };
651 ## Returns "True" in case a given path is a known mountpoint.
656 my %mountpoints = reverse %mountpoints ;
658 # Return "True" if the requested mountpoint is known and
660 return 1 if ( $mountpoints { $mpoint });
664 ## Returns "True" if a given mountpoint is a subdirectory of one
665 ## of the directories specified by the valid_mount_dirs array abouve.
667 sub is_valid_dir
($) {
670 # Do not allow "/mnt" or "/media" as mount points.
671 return if ( $mpoint eq "/mnt" );
672 return if ( $mpoint eq "/media" );
674 # Split the given mountpoint into pieces and store them
675 # in a temporay array.
676 my @tmp = split ( "/" , $mpoint );
678 # Exit and return nothing if the temporary array is empty.
681 # Build the root path based on the given mount point.
682 my $root_path = "/" . @tmp [ 1 ];
684 # Check if the root path is valid.
685 return 1 if ( grep /$root_path/ , @valid_mount_dirs );
689 # Returns "True" if a device is used as swap.
694 return 1 if ( grep /$device/ , @swaps );
698 ## Returns "True" if a drive is a configured one.
700 sub is_configured
($) {
703 # Loop through the hash of configured drives.
704 foreach my $uuid ( keys %configured_drives ) {
705 return 1 if ( $configured_drives { $uuid } eq " $path " );