]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/extrahd.cgi
bb0c2261021a8ea7b474e480a43bf6c2cf8fe8d1
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2011 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 # Array which contains the supported file systems.
49 my @supported_filesystems = (
58 # Grab all available block devices.
59 my @devices = & get_block_devices
();
61 # Grab all known UUID's.
62 my %uuids = & get_uuids
();
64 # Grab all mountpoints.
65 my %mountpoints = & get_mountpoints
();
67 # Omit the file system types of the mounted devices.
68 my %filesystems = & get_mountedfs
();
70 # Gather all used swap devices.
71 my @swaps = & get_swaps
();
73 # The config file which contains the configured devices.
74 my $devicefile = "/var/ipfire/extrahd/devices" ;
76 #workaround to suppress a warning when a variable is used only once
77 my @dummy = ( ${ Header
:: colourgreen
}, ${ Header
:: colourred
} );
80 & Header
:: showhttpheaders
();
82 ### Values that have to be initialized
83 $extrahdsettings { 'PATH' } = '' ;
84 $extrahdsettings { 'FS' } = '' ;
85 $extrahdsettings { 'DEVICE' } = '' ;
86 $extrahdsettings { 'ACTION' } = '' ;
87 $extrahdsettings { 'UUID' } = '' ;
89 & Header
:: getcgihash
( \
%extrahdsettings );
91 & Header
:: openpage
( 'ExtraHD' , 1 , '' );
92 & Header
:: openbigbox
( '100%' , 'left' , '' , $errormessage );
94 ############################################################################################################################
95 ############################################################################################################################
100 if ( $extrahdsettings { 'ACTION' } eq $Lang :: tr
{ 'add' }) {
101 # Open device file for reading.
102 open ( FILE
, "< $devicefile " ) or die "Unable to read $devicefile " ;
103 my @devices = < FILE
>;
106 # Loop through the entries line-by-line.
107 foreach my $entry ( sort @devices ) {
108 # Split the line into pieces and assign nice variables.
109 my ( $uuid , $fs , $path ) = split ( /\;/ , $entry );
111 # Check if the path is allready used.
112 if ( " $extrahdsettings {'PATH'}" eq " $path " ) {
113 $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'}." ;
116 # Check if the uuid is allready used.
117 if ( " $extrahdsettings {'DEVICE'} eq $uuid " ) {
118 $errormessage = " $extrahdsettings {'DEVICE'} is allready mounted." ;
122 # Check if a valid mount path has been choosen.
123 unless (& is_valid_dir
( " $extrahdsettings {'PATH'}" )) {
124 $errormessage = " $Lang ::tr{'extrahd you cant mount'} $extrahdsettings {'DEVICE'} $Lang ::tr{'extrahd to root'}." ;
127 # Check if the given path allready is mounted somewhere.
128 if (& is_mounted
( " $extrahdsettings {'PATH'}" )) {
129 $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'}." ;
132 # Check if there was an error message.
133 unless ( $errormessage ) {
134 # Re-open the device file for writing.
135 open ( FILE
, ">> $devicefile " ) or die "Unable to write $devicefile " ;
137 # Write the config line.
138 print FILE
"UUID= $extrahdsettings {'UUID'}; $extrahdsettings {'FS'}; $extrahdsettings {'PATH'}; \n " ;
143 # Call helper binary to mount the device.
144 & General
:: system ( "/usr/local/bin/extrahdctrl" , "mount" , " $extrahdsettings {'PATH'}" );
148 # Remove an existing one.
150 } elsif ( $extrahdsettings { 'ACTION' } eq $Lang :: tr
{ 'delete' }) {
151 # Call helper binary to unmount the device.
152 unless (& General
:: system ( "/usr/local/bin/extrahdctrl" , "umount" , " $extrahdsettings {'PATH'}" )) {
153 # Open the device file for reading.
154 open ( FILE
, "< $devicefile " ) or die "Unable to read $devicefile " ;
156 # Read the file content into a temporary array.
162 # Re-open device file for writing.
163 open ( FILE
, "> $devicefile " ) or die "Unable to write $devicefile " ;
165 # Loop through the previous read file content.
166 foreach my $line ( sort @tmp ) {
167 # Split line content and assign nice variables.
168 my ( $uuid , $fs , $path ) = split ( /\;/ , $line );
170 # Write the line in case it does not contain our element to delete.
171 if ( $path ne $extrahdsettings { 'PATH' }) {
179 $errormessage = " $Lang ::tr{'extrahd cant umount'} $extrahdsettings {'PATH'} $Lang ::tr{'extrahd maybe the device is in use'}?" ;
184 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'error messages' });
185 print "<class name='base'> $errormessage \n " ;
186 print " </class> \n " ;
190 ############################################################################################################################
191 ############################################################################################################################
193 & Header
:: openbox
( '100%' , 'center' , $Lang :: tr
{ 'extrahd detected drives' });
195 # Re-read mountpoints.
196 %mountpoints = & get_mountpoints
();
198 # Read-in the device config file.
199 open ( FILE
, "< $devicefile " ) or die "Unable to read $devicefile " ;
201 # Loop through the file content.
203 # Cut the line into pieces.
204 my ( $uuid , $fs , $path ) = split ( /\;/ , $_ );
206 # Add the found entry to the hash of configured drives.
207 $configured_drives { $uuid } = $path ;
210 # Close the file handle.
214 <table border='0' width='600' cellspacing="0">
217 foreach my $device ( sort @devices ) {
218 # Grab the device details.
219 my $vendor = & get_device_vendor
( $device );
220 my $model = & get_device_model
( $device );
221 my $bsize = & get_device_size
( $device );
223 # Convert size into human-readable format.
224 my $size = & General
:: formatBytes
( $bsize );
227 <tr><td colspan="5"> </td></tr>
228 <tr><td align='left' colspan="2"><b>/dev/ $device </b></td>
229 <td align='center' colspan="2"> $vendor $model </td>
231 <td align='center'> $Lang ::tr{'size'} $size </td>
233 <tr><td colspan="5"> </td></tr>
237 # Grab the known partitions of the current block device.
238 my @partitions = & get_device_partitions
( $device );
240 foreach my $partition ( @partitions ) {
243 # Omit the partition size.
244 my $bsize = & get_device_size
( $partition );
246 # Convert into human-readable format.
247 my $size = & General
:: formatBytes
( $bsize );
249 # Get the mountpoint.
250 my $mountpoint = $mountpoints { $partition };
252 # If no mountpoint could be determined try to grab from
254 unless ( $mountpoint ) {
255 my $uuid = $uuids { $partition };
258 $uuid = "UUID=" . $uuid ;
260 # Try to obtain a possible moutpoint from configured drives.
261 $mountpoint = $configured_drives { $uuid } if ( $configured_drives { $uuid });
264 # Check if the mountpoint is used as root or boot device.
265 if ( $mountpoint eq "/" or $mountpoint =~ "^/boot" ) {
266 $disabled = "disabled" ;
268 # Check if it is mounted.
269 } elsif (& is_mounted
( $mountpoint )) {
270 $disabled = "disabled" ;
272 # Check if the device is used as swap.
273 } elsif (& is_swap
( $partition )) {
274 $disabled = "disabled" ;
275 $mountpoint = "swap" ;
278 # Omit the used filesystem.
279 my $fs = $filesystems { $partition };
283 <form method='post' action=' $ENV {'SCRIPT_NAME'}'>
284 <tr><td align="left" colspan=5><strong>UUID= $uuids { $partition }</strong></td></tr>
286 <td align="list">/dev/ $partition </td>
287 <td align="center"> $Lang ::tr{'size'} $size </td>
288 <td align="center"><select name="FS" $disabled >
291 # Loop through the array of supported filesystems.
292 foreach my $filesystem ( @supported_filesystems ) {
295 # Mark the used filesystem as selected.
296 if ( $filesystem eq $fs ) {
297 $selected = "selected" ;
300 print "<option value=' $filesystem ' $selected > $filesystem </option> \n " ;
305 <td align="center"><input type='text' name='PATH' value= $mountpoint $disabled ></td>
307 <input type='hidden' name='DEVICE' value='/dev/ $partition ' />
308 <input type='hidden' name='UUID' value=' $uuids { $partition }' />
309 <input type='hidden' name='ACTION' value=' $Lang ::tr{'add'}' />
311 ; unless ( $disabled ) {
312 print "<input type='image' alt=' $Lang ::tr{'add'}' title=' $Lang ::tr{'add'}' src='/images/add.gif' /> \n " ;
323 <tr><td align="center" colspan="5"> </td></tr>
324 <tr><td align="center" colspan="5"> </td></tr>
325 <tr><td align="center" colspan="5"> $Lang ::tr{'extrahd install or load driver'}</td></tr>
331 & Header
:: closebigbox
();
332 & Header
:: closepage
();
335 ## Function which return an array with all available block devices.
337 sub get_block_devices
() {
340 # Open directory from kernel sysfs.
341 opendir ( DEVICES
, "/sys/block" );
343 # Loop through the directory.
344 while ( readdir ( DEVICES
)) {
346 next if ( $_ =~ /^\.$/ );
347 next if ( $_ =~ /^\..$/ );
349 # Skip any loopback and ram devices.
350 next if ( $_ =~ "^loop" );
351 next if ( $_ =~ "^ram" );
353 # Add the device to the array of found devices.
357 # Close directory handle.
360 # Return the devices array.
365 ## Function which return all partitions of a given block device.
367 sub get_device_partitions
($) {
370 # Array to store the known partitions for the given
374 # Assign device directory.
375 my $device_dir = " $sysfs_block_dir / $device " ;
377 # Abort and return nothing if the device dir does not exist.
378 return unless (- d
" $device_dir " );
380 opendir ( DEVICE
, " $sysfs_block_dir / $device " );
381 while ( readdir ( DEVICE
)) {
382 next unless ( $_ =~ "^ $device " );
384 push ( @partitions , $_ );
389 @partitions = sort ( @partitions );
395 ## Returns the vendor of a given block device.
397 sub get_device_vendor
($) {
400 # Assign device directory.
401 my $device_dir = " $sysfs_block_dir / $device " ;
403 # Abort and return nothing if the device dir does not exist
404 # or no vendor file exists.
405 return unless (- d
" $device_dir " );
406 return unless (- f
" $device_dir /device/vendor" );
408 # Open and read-in the device vendor.
409 open ( VENDOR
, " $device_dir /device/vendor" );
410 my $vendor = < VENDOR
>;
413 # Abort and return nothing if no vendor could be read.
414 return unless ( $vendor );
416 # Remove any newlines from the vendor string.
419 # Return the omited vendor.
424 ## Returns the model name (string) of a given block device.
426 sub get_device_model
($) {
429 # Assign device directory.
430 my $device_dir = " $sysfs_block_dir / $device " ;
432 # Abort and return nothing if the device dir does not exist
433 # or no model file exists.
434 return unless (- d
" $device_dir " );
435 return unless (- f
" $device_dir /device/model" );
437 # Open and read-in the device model.
438 open ( MODEL
, " $device_dir /device/model" );
442 # Abort and return nothing if no model could be read.
443 return unless ( $model );
445 # Remove any newlines from the model string.
448 # Return the model string.
453 ## Returns the size of a given device in bytes.
455 sub get_device_size
($) {
458 # Assign device directory.
459 my $device_dir = " $sysfs_block_dir / $device " ;
461 # Abort and return nothing if the device dir does not exist
462 # or no size file exists.
463 return unless (- d
" $device_dir " );
464 return unless (- f
" $device_dir /size" );
466 # Open and read-in the device size.
467 open ( SIZE
, " $device_dir /size" );
471 # Abort and return nothing if the size could not be read.
472 return unless ( $size );
474 # Remove any newlines for the size string.
477 # The omited size only contains the amount of blocks from the
478 # given device. To convert this into bytes we have to multiply this
479 # value with 512 bytes for each block. This is a static value used by
483 # Return the size in bytes.
488 ## Function which returns all currently mounted devices as a hash.
489 ## example: "sda1" -> "/boot"
491 sub get_mountpoints
() {
494 # Open and read-in the current mounts from the
495 # kernel file system.
496 open ( MOUNT
, "/proc/mounts" );
498 # Loop through the known mounts.
500 # Skip mounts which does not belong to a device.
501 next unless ( $_ =~ "^/dev" );
503 # Cut the line into pieces and assign nice variables.
504 my ( $dev , $mpoint , $fs , $options , $a , $b ) = split ( / / , $_ );
506 # Split the device name.
507 my @tmp = split ( "/" , $dev );
509 # Assign the plain device name to a new variable.
510 # It is the last element of the array.
511 my $device = $tmp [- 1 ];
513 # Add the mountpoint to the hash of mountpoints.
514 $mounts { " $device " } = $mpoint ;
520 # Return the hash of known mountpoints.
527 # Open and read the swaps file.
528 open ( SWAP
, "/proc/swaps" );
530 # Loop though the file content.
532 # Skip lines which does not belong to a device.
533 next unless ( $_ =~ "^/dev" );
535 # Split the line and assign nice variables.
536 my ( $dev , $type , $size , $used , $prio ) = split ( / / , $_ );
538 # Cut the device line into pieces.
539 my @tmp = split ( "/" , $dev );
541 my $device = @tmp [- 1 ];
543 # Add the found swap to the array of swaps.
544 push ( @swaps , $device );
551 @swaps = sort ( @swaps );
558 ## Function with returns the mounted devices and the used filesystems as a hash.
559 ## Example: "sda1" -> "ext4"
561 sub get_mountedfs
() {
564 # Open and read the current mounts from the kernel
566 open ( MOUNT
, "/proc/mounts" );
568 # Loop through the known mounts.
570 # Skip mounts which does not belong to a device.
571 next unless ( $_ =~ "^/dev" );
573 # Split line and assign nice variables.
574 my ( $dev , $mpoint , $fs , $options , $a , $b ) = split ( / / , $_ );
576 # Cut the device line into pieces.
577 my @tmp = split ( "/" , $dev );
579 # Assign the plain device name to a variable
580 # It is the last element of the temporary array.
581 my $device = $tmp [- 1 ];
583 # Convert the filesystem into lower case format.
586 # Add the mounted file system.
587 $mountedfs { $device } = $fs ;
593 # Return the hash with the mounted filesystems.
598 ## Function which returns all known UUID's as a hash.
599 ## Example: "sda1" -> "1234-5678-abcd"
604 # Directory where the uuid mappings can be found.
605 my $uuid_dir = "/dev/disk/by-uuid" ;
607 # Open uuid directory and read-in the current known uuids.
608 opendir ( UUIDS
, " $uuid_dir " );
610 # Loop through the uuids.
611 foreach my $uuid ( readdir ( UUIDS
)) {
613 next if ( $uuid eq "." or $uuid eq ".." );
615 # Skip everything which is not a symbolic link.
616 next unless (- l
" $uuid_dir / $uuid " );
618 # Resolve the target of the symbolic link.
619 my $target = readlink ( " $uuid_dir / $uuid " );
621 # Split the link target into pieces.
622 my @tmp = split ( "/" , $target );
624 # Assign the last element of the array to the dev variable.
625 my $dev = " $tmp [-1]" ;
627 # Add the device and uuid to the hash of uuids.
628 $uuids { $dev } = $uuid ;
631 # Close directory handle.
634 # Return the hash of uuids.
639 ## Returns the device name of a given uuid.
641 sub device_by_uuid
($) {
644 # Reverse the main uuids hash.
645 my %uuids = reverse %uuids ;
647 # Lookup and return the device name.
648 return $uuids { $uuid };
652 ## Returns "True" in case a given path is a known mountpoint.
657 my %mountpoints = reverse %mountpoints ;
659 # Return "True" if the requested mountpoint is known and
661 return 1 if ( $mountpoints { $mpoint });
665 ## Returns "True" if a given mountpoint is a subdirectory of one
666 ## of the directories specified by the valid_mount_dirs array abouve.
668 sub is_valid_dir
($) {
671 # Split the given mountpoint into pieces and store them
672 # in a temporay array.
673 my @tmp = split ( "/" , $mpoint );
675 # Exit and return nothing if the temporary array is empty.
678 # Build the root path based on the given mount point.
679 my $root_path = "/" . @tmp [ 1 ];
681 # Check if the root path is valid.
682 return 1 if ( grep /$root_path/ , @valid_mount_dirs );
686 # Returns "True" if a device is used as swap.
691 return 1 if ( grep /$device/ , @swaps );