festplatten partitionieren

peshay

Mitglied
hi,
ich kenn mich leider null aus mit Perl und habe ein script das mir (in einem abschnitt) die festplatte partitioniert, dabei bietet es mir ein paar auswahl möglichkeiten an, ich hätte aber gern noch eine zusätzliche auswahlmöglichkeit bei der er die größe der festplatte rausfindet und dann hälfte c: und andere hälfte d: macht

hier mal der auszug aus dem script:

Code:
# Find an interval of free space on the drive of at least SIZE
# megabytes.  Return as a pair (START, END).  Note that the only
# guarantee is that the interval does not overlap any partitions; it
# may stretch beyond the end of the drive.
sub find_free_space ($) {
    my ($size) = @_;

    $is_linux
        or croak "internal error";

    my @partitions;

    # Read the current partition table.
    my $cmd = 'parted -s /dev/dsk print';
    open PARTED, "$cmd|"
        or die "Unable to fork: $^E";

    while (my $line = <PARTED>) {
        my ($start, $end) = ($line =~ /^\d+\s+(\d+\.\d{3})\s+(\d+\.\d{3})/);
        defined $end
            or next;

        push @partitions, [ $start, $end ];
    }

    close PARTED
        or die "'$cmd' failed: $^E $?";

    # Now loop through looking for free space.
  LOOP:
    foreach my $temp_part ([0, 0], @partitions) {
        my (undef, $temp_end) = @$temp_part;
        my ($start, $end) = ($temp_end, $temp_end + $size);
        foreach my $part (@partitions) {
            my ($part_start, $part_end) = @$part;
            # If we overlap a partition, no good.
            $start < $part_end && $end > $part_start
                and next LOOP;
        }
        return ($start, $end);
    }

    die 'Internal error';
}

# Convert an fdisk command to a parted command, more or less.
sub convert_fdisk_parted ($) {
    my ($fdisk_cmd) = @_;
    my $ret;

    # "--" is required, lest "-0" on the command line look like an
    # option.
    my $parted = 'parted -s /dev/dsk --';

    my ($cmd) = ($fdisk_cmd =~ /^\s*fdisk\s+(.*?)\s*\z/i);
    defined $cmd
        or croak 'internal error';

    if ($cmd =~ /^\/clear\s+1\z/i) {
        $ret = "$parted mklabel msdos";
    }
    elsif ($cmd =~ /^\/delete\s+\/pri:(\d+)\z/i) {
        $ret = "$parted rm $1";
    }
    elsif ($cmd =~ /^\/activate:(\d+)\z/i) {
        $ret = "$parted set $1 boot on";
    }
    elsif ($cmd =~ /^\/xo/i) {
        $ret = 'parted /dev/dsk';
    }
    elsif ($cmd =~ /\/pri(o)?:(\d+)(,100)?(?:\s+\/spec:(\d+))?/i) {
        my ($fat16, $size, $is_percent, $type) = ($1, $2, $3, $4);

        # We really want "infinity" here.  But I suppose a
        # petabyte will do.
        my $infinity = 1000000000;
        if (defined $is_percent) {
            $size eq '100'
                or croak "We only support 100,100 for size spec ($fdisk_cmd)";
            $size = $infinity;
        }

        my ($start, $end) = find_free_space ($size);

        # Sanity-check size of FAT16 partitions.
        defined $fat16 && $end - $start > 2047
            and die "Unable to execute fdisk $cmd\n"
            . "because it would create a FAT16 partition > 2047M\n"
            . "I suggest using /pri:XXX instead of /prio:XXX\n"
            . 'Bailing out';

        $end >= $infinity
            and $end = '-0';

        my $fs = (defined $fat16 ? 'fat16' : 'fat32');
        if (defined $type) {
            $type == 7
                or croak "Sorry, only type 7 (NTFS) is allowed ($fdisk_cmd)";
            $fs = 'ntfs';
        }

        $ret = "$parted mkpart primary $fs $start $end";
    }
    else {
        die "Unable to convert '$fdisk_cmd' to Parted commands; bailing";
    }

    return $ret;
}

# fdisk commands to run
sub ask_fdisk_cmds () {
    # Read current partition table.
    my $partition_layout = partition_table ();

    # Display it.
    print "\nCurrent partition table:";
    print $partition_layout;
    print "\n";

    print "Choose partitioning scheme.\n";
    $is_linux
        or print "NOTE: If partition table changes, machine will reboot.\n";
    # Commands to erase partition table
    my $pre_cmds = 'fdisk /clear 1';

    # Commands to replace the first partition with a 4G FAT32
    # partition and activate it
    my $post_cmds = 'fdisk /delete /pri:1;fdisk /pri:4000;fdisk /activate:1';

    # Command to run fdisk interactively
    my $interactive_cmd = 'fdisk /xo';

    my $ret = menu_choice
        ('Do nothing (continue)' => undef,
         'Run partitioning tool manually (experts only)' => $interactive_cmd,
         'Whole disk C:', =>
         'fdisk /pri:100,100',
         '4G C:, rest D:' =>
         'fdisk /pri:4096;fdisk /pri:100,100 /spec:7',
         '12G C:, 5G D:, rest E:' =>
         'fdisk /pri:12288;fdisk /pri:5120 /spec:7;fdisk /pri:100,100 /spec:7'
         );

    defined $ret
        or return undef;

    $ret eq $interactive_cmd
        or $ret = "$pre_cmds;$ret;$post_cmds";

    return $ret;
}
 

Neue Beiträge

Zurück