#!/usr/local/bin/perl # Create a filesystem on a partition use strict; use warnings; no warnings 'redefine'; no warnings 'uninitialized'; require './bsdfdisk-lib.pl'; our ( %in, %text, $module_name ); &ReadParse(); &error_setup( $text{'newfs_err'} ); # Get the disk and slice $in{'device'} =~ /^[a-zA-Z0-9_\/.-]+$/ or &error( $text{'disk_edevice'} || 'Invalid device' ); $in{'device'} !~ /\.\./ or &error( $text{'disk_edevice'} || 'Invalid device' ); $in{'slice'} =~ /^\d+$/ or &error( $text{'slice_egone'} ); $in{'part'} =~ /^[a-z]$/ or &error( $text{'part_egone'} ) if $in{'part'}; my @disks = &list_disks_partitions(); my ($disk) = grep { $_->{'device'} eq $in{'device'} } @disks; $disk || &error( $text{'disk_egone'} ); my ($slice) = grep { $_->{'number'} eq $in{'slice'} } @{ $disk->{'slices'} }; $slice || &error( $text{'slice_egone'} ); my ( $object, $part ); if ( $in{'part'} ne '' ) { ($part) = grep { $_->{'letter'} eq $in{'part'} } @{ $slice->{'parts'} }; $part || &error( $text{'part_egone'} ); $object = $part; } else { $object = $slice; } # Safety checks: do not run newfs on boot partitions or in-use devices if ( is_boot_partition($object) ) { &error( $in{'part'} ne '' ? $text{'part_eboot'} : $text{'slice_eboot'} ); } my @st_obj = &fdisk::device_status( $object->{'device'} ); my $use_obj = &fdisk::device_status_link(@st_obj); if ( @st_obj && $st_obj[2] ) { &error( &text( 'part_esave', &html_escape($use_obj) ) ); } # Validate inputs my $newfs = {}; $in{'free_def'} || ( $in{'free'} =~ /^\d+$/ && $in{'free'} >= 0 && $in{'free'} <= 100 ) || &error( $text{'newfs_efree'} ); $newfs->{'free'} = $in{'free_def'} ? undef : $in{'free'}; $newfs->{'trim'} = $in{'trim'}; $in{'label_def'} || length( $in{'label'} ) > 0 || &error( $text{'newfs_elabel'} ); $newfs->{'label'} = $in{'label_def'} ? undef : $in{'label'}; &ui_print_unbuffered_header( $object->{'desc'}, $text{'newfs_title'}, "" ); # Do the creation print &text( 'newfs_creating', "" . &html_escape( $object->{'device'} ) . "" ), "
\n"; print "
\n";
my $cmd = &get_create_filesystem_command( $disk, $slice, $part, $newfs );
&additional_log( 'exec', undef, $cmd );
my $out = &backquote_command( $cmd . " 2>&1" );
foreach my $line ( split( /\n/, $out ) ) {
    $line =~ s/[^\x09\x0A\x0D\x20-\x7E]//g;
    print &html_escape($line) . "\n";
}
print "
"; my $rc = $? >> 8; if ($rc) { print $text{'newfs_failed'}, "

\n"; } else { print $text{'newfs_done'}, "

\n"; &webmin_log( "newfs", $in{'part'} ne '' ? "part" : "object", $object->{'device'}, $object ); # Verify filesystem signature if possible if ( has_command('fstyp') ) { my $fstyp_out = &backquote_command( "fstyp " . quote_path( $object->{'device'} ) . " 2>&1" ); $fstyp_out =~ s/[\r\n]+$//; if ($fstyp_out) { print "

\n";
            print &html_escape($fstyp_out) . "\n";
            print "
\n"; } else { print "Warning: fstyp did not detect a filesystem on this device.

\n"; } } # If a label was provided, set the partition label (GPT slice or BSD sub-partition) if ( !$in{'label_def'} && defined $in{'label'} && length $in{'label'} ) { my $errlbl = set_partition_label( disk => $disk, slice => $slice, part => ( $in{'part'} ne '' ? $part : undef ), label => $in{'label'} ); if ($errlbl) { print "Warning: failed to set partition label: \n" . &html_escape($errlbl) . "\n"; } } } if ( $in{'part'} ne '' ) { my $url_device = &urlize( $in{'device'} ); my $url_slice = &urlize( $in{'slice'} ); my $url_part = &urlize( $in{'part'} ); &ui_print_footer( "edit_part.cgi?device=$url_device&slice=$url_slice&part=$url_part", $text{'part_return'} ); } else { my $url_device = &urlize( $in{'device'} ); my $url_slice = &urlize( $in{'slice'} ); &ui_print_footer( "edit_slice.cgi?device=$url_device&slice=$url_slice", $text{'slice_return'} ); }