aboutsummaryrefslogtreecommitdiffstats
path: root/lib/AdminPanel/Module/Proxy.pm
blob: 3a7ea6cc407ff589c9de0099574cb64d1ef488fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# vim: set et ts=4 sw=4:
#*****************************************************************************
# 
#  Copyright (c) 2013-2014 Matteo Pasotti <matteo.pasotti@gmail.com>
# 
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2, as
#  published by the Free Software Foundation.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
#*****************************************************************************

package AdminPanel::Module::Proxy;

use Modern::Perl '2011';
use autodie;
use Moose;
use POSIX qw(ceil);
use English;
use utf8;

use yui;
use AdminPanel::Shared qw(trim);
use AdminPanel::Shared::GUI;
use AdminPanel::Shared::Proxy;

# TODROP but provides network::network
use lib qw(/usr/lib/libDrakX);
use network::network;
use MDK::Common::System qw(getVarsFromSh);

extends qw( AdminPanel::Module );


has '+icon' => (
    default => "/usr/share/mcc/themes/default/drakproxy-mdk.png"
);

has '+name' => (
    default => "Proxymanager",
);

=head1 VERSION

Version 1.0.0

=cut

our $VERSION = '1.0.0';

has 'dialog' => (
    is => 'rw',
    init_arg => undef
);

has 'table' => (
    is => 'rw',
    init_arg => undef
);

has 'proxy' => (
    is      => 'rw',
    isa     => 'HashRef',
    builder => "init_proxy"
);

has 'sh_gui' => (
        is => 'rw',
        init_arg => undef,
        builder => '_SharedUGUIInitialize'
);

has 'loc' => (
        is => 'rw',
        init_arg => undef,
        builder => '_localeInitialize'
);


sub _localeInitialize {
    my $self = shift();

    # TODO fix domain binding for translation
    $self->loc(AdminPanel::Shared::Locales->new(domain_name => 'drakx-net') );
    # TODO if we want to give the opportunity to test locally add dir_name => 'path'
}

sub _SharedUGUIInitialize {
    my $self = shift();

    $self->sh_gui( AdminPanel::Shared::GUI->new() );
}

#=============================================================

=head2 init_proxy

=head3 DESCRIPTION

=over 4

=item This method does initialize the proxy attribute provided by this class.

=item $self->proxy is structured as follows:

=over 6

=item B<no_proxy>    the string with the list of the excluded domains/addresses

=item B<http_proxy>  the url of the http proxy

=item B<https_proxy> the url of the https proxy

=item B<ftp_proxy>   the url for the ftp proxy

=back

=back

=cut

#=============================================================

sub init_proxy {
    my %p = (
                'no_proxy'    => '',
                'http_proxy'  => '',
                'https_proxy' => '',
                'ftp_proxy'   => '',
            );
    return \%p;
}

#=============================================================

=head2 start

=head3 INPUT

    $self: this object

=head3 DESCRIPTION

    This method extends Module::start and is invoked to
    start proxy manager

=cut

#=============================================================
sub start {
    my $self = shift;

    if ($EUID != 0) {
        $self->sh_gui->warningMsgBox({
                                title => $self->name,
                                text  => $self->loc->N("root privileges required"),
                                });
        return;
    }

    $self->_manageProxyDialog();
};

#=============================================================

=head2 ask_for_X_restart

=head3 INPUT

    $self: this object

=head3 DESCRIPTION

    This method shows a message box warning the user
    that a X server restart is required

=cut

#=============================================================

sub ask_for_X_restart {
    my $self = shift;

    $self->sh_gui->warningMsgBox({title=>$self->loc->N("X Restart Required"),text=>$self->loc->N("You need to log out and back in again for changes to take effect"),richtext=>1});
}

#=============================================================

=head2 validate

=head3 INPUT

    $self: this object

    $proxy: the hash containing what returns from getVarFromSh
            eventually modified by the user

=head3 DESCRIPTION

    This method returns true if the each value match
    certain conditions like the leading http:// for http proxy
    or https:// for the https proxy, etc.
    
    $proxy is passed by reference thus $proxy->{no_proxy} value
    is sanitized (trimmed).

=cut

#=============================================================

sub validate {
    my $self = shift;
    my $proxy = shift;
    my $retval = 1;
    $proxy->{no_proxy} =~ s/\s//g;
    # using commas rather than slashes 
    if($proxy->{http_proxy} !~ m,^($|http://),)
    {
        $self->sh_gui->warningMsgBox({title=>'Error',text=>$self->loc->N("Proxy should be http://..."),richtext=>0});
        $retval = 0;
    }
    if($proxy->{https_proxy} !~ m,^($|https?://),)
    {
        $self->sh_gui->warningMsgBox({title=>'Error',text=>$self->loc->N("Proxy should be http://... or https://..."),richtext=>0});
        $retval = 0;
    }
    if($proxy->{ftp_proxy} !~ m,^($|ftp://|http://),)
    {
        $self->sh_gui->warningMsgBox({title=>'Error',text=>$self->loc->N("URL should begin with 'ftp:' or 'http:'"),richtext=>0});
        $retval = 0;
    }
    return $retval;
}

sub _manageProxyDialog {
    my $self = shift;

    ## TODO fix for adminpanel
    my $appTitle = yui::YUI::app()->applicationTitle();
    my $appIcon = yui::YUI::app()->applicationIcon();
    ## set new title to get it in dialog
    my $newTitle = $self->loc->N("Proxies configuration");
    yui::YUI::app()->setApplicationTitle($newTitle);

    my $factory  = yui::YUI::widgetFactory;
    my $optional = yui::YUI::optionalWidgetFactory;
    
    my $label_width = 25;
    my $inputfield_width = 45;
    # getVarsFromSh returns an empty hash if no vars are defined
    # possible alternatives:
    # . Config::Auto::parse
    my $proxy_curr_settings = { getVarsFromSh('/etc/profile.d/proxy.sh') };
    my $httpsProxyEqualToHttpProxy = 0;
    if((defined($proxy_curr_settings->{http_proxy}) && defined($proxy_curr_settings->{https_proxy}))&&
        (($proxy_curr_settings->{http_proxy} eq $proxy_curr_settings->{https_proxy}) && 
            ($proxy_curr_settings->{http_proxy} ne ""))){
        $httpsProxyEqualToHttpProxy = 1;
    }

    #
    # @layout
    #
    # +------------------------------+
    # | +------------+-------------+ |
    # | |LABELS      | VALUES      | |
    # | |            |             | |
    # | |            |             | |
    # | |            |             | |
    # | +------------+-------------+ |
    # +------------------------------+

    $self->dialog($factory->createMainDialog());
    my $layout    = $factory->createVBox($self->dialog);

    my $hbox_header = $factory->createHBox($layout);
    my $headLeft = $factory->createHBox($factory->createLeft($hbox_header));
    my $headRight = $factory->createHBox($factory->createRight($hbox_header));

    my $logoImage = $factory->createImage($headLeft, $appIcon);
    my $labelAppDescription = $factory->createLabel($headRight,$newTitle); 
    $logoImage->setWeight($yui::YD_HORIZ,0);
    $labelAppDescription->setWeight($yui::YD_HORIZ,3);

    # app description
    my $hbox_content = $factory->createHBox($layout);
    $factory->createLabel($hbox_content, $self->loc->N("Here you can set up your proxies configuration (eg: http://my_caching_server:8080)"));

    $hbox_content = $factory->createHBox($layout);

    my $vbox_labels_flags = $factory->createVBox($hbox_content);
    my $vbox_inputfields = $factory->createVBox($hbox_content);

    # http proxy section
    my $httpproxy_label = $factory->createLabel($vbox_labels_flags, $self->loc->N("HTTP proxy"));
    my $http_proxy = $factory->createInputField($factory->createHBox($vbox_inputfields),"",0);
    $http_proxy->setValue($proxy_curr_settings->{http_proxy}) if(defined($proxy_curr_settings->{http_proxy}));
    $http_proxy->setWeight($yui::YD_HORIZ, 30);

    # flag to setup the https proxy with the same value of the http proxy
    my $ckbHttpEqHttps = $factory->createCheckBox($vbox_labels_flags, $self->loc->N("Use HTTP proxy for HTTPS connections"),$httpsProxyEqualToHttpProxy);
    $ckbHttpEqHttps->setNotify(1);
    # add a spacing as we have 
    $factory->createLabel($factory->createHBox($vbox_inputfields)," ");

    # https proxy
    $factory->createLabel($vbox_labels_flags, $self->loc->N("HTTPS proxy"));
    my $https_proxy = $factory->createInputField($factory->createHBox($vbox_inputfields),"",0);
    $https_proxy->setValue($proxy_curr_settings->{https_proxy}) if(defined($proxy_curr_settings->{https_proxy}));
    $https_proxy->setWeight($yui::YD_HORIZ, 30);

    # ftp proxy
    $factory->createLabel($vbox_labels_flags, $self->loc->N("FTP proxy"));
    my $ftp_proxy = $factory->createInputField($factory->createHBox($vbox_inputfields),"",0);
    $ftp_proxy->setValue($proxy_curr_settings->{ftp_proxy}) if(defined($proxy_curr_settings->{ftp_proxy}));
    $ftp_proxy->setWeight($yui::YD_HORIZ, 30);

    # no-proxy list
    $factory->createLabel($vbox_labels_flags, $self->loc->N("No proxy for (comma separated list):"));
    my $no_proxy = $factory->createInputField($factory->createHBox($vbox_inputfields),"",0);
    $no_proxy->setValue($proxy_curr_settings->{no_proxy}) if(defined($proxy_curr_settings->{no_proxy}));
    $no_proxy->setWeight($yui::YD_HORIZ, 30);

    my $hbox_filler = $factory->createHBox($layout);
    $factory->createSpacing($hbox_filler,$yui::YD_VERT,2);

    my $hbox_foot = $factory->createHBox($layout);
    my $vbox_foot_left = $factory->createVBox($factory->createLeft($hbox_foot));
    my $vbox_foot_right = $factory->createVBox($factory->createRight($hbox_foot));
    my $aboutButton = $factory->createPushButton($vbox_foot_left,$self->loc->N("About"));
    my $cancelButton = $factory->createPushButton($vbox_foot_right,$self->loc->N("Cancel"));
    my $okButton = $factory->createPushButton($vbox_foot_right,$self->loc->N("OK"));

    # main loop
    while(1) {
        my $event     = $self->dialog->waitForEvent();
        my $eventType = $event->eventType();
        
        #event type checking
        if ($eventType == $yui::YEvent::CancelEvent) {
            last;
        }
        elsif ($eventType == $yui::YEvent::WidgetEvent) {
### Buttons and widgets ###
            my $widget = $event->widget();
            if ($widget == $cancelButton) {
                last;
            }elsif ($widget == $aboutButton) {
                $self->sh_gui->AboutDialog({
                    name => $appTitle,
                    version => $VERSION,
                    credits => "Copyright (c) 2013-2014 by Matteo Pasotti",
                    license => "GPLv2",
                    description => $self->loc->N("Graphical manager for proxies"),
                    authors => "Matteo Pasotti &lt;matteo.pasotti\@gmail.com&gt;"
                    }
                );
            }elsif ($widget == $okButton) {
                # setup proxy attribute
                my %_proxy = ( 
                    no_proxy    => $no_proxy->value(),
                    http_proxy  => $http_proxy->value(),
                    https_proxy => $https_proxy->value(),
                    ftp_proxy   => $ftp_proxy->value()
                );
                if($self->validate(\%_proxy)) {
                    # validation succeded
                    $self->proxy(\%_proxy);
                    # save changes
                    network::network::proxy_configure($self->proxy);
                    $self->ask_for_X_restart();
                    last;
                }
                # validation failed
                next;
            }elsif ($widget == $ckbHttpEqHttps){
                $https_proxy->setEnabled(!$ckbHttpEqHttps->isChecked());
            }
        }
    }

    $self->dialog->destroy() ;

    #restore old application title
    yui::YUI::app()->setApplicationTitle($appTitle);
}

1;
b">$@; if ($handle_die_and_cdie->()) { $handled = 1; 0; #- do not continue, transform cdie into die } else { !$o_in || $o_in->ask_okcancel('', formatError($err)); } }; }; if (my $err = $@) { if ($handled) { #- already handled in cdie handler above } elsif ($handle_die_and_cdie->()) { } elsif ($o_in && $o_in->ask_yesorno(N("Error"), N("I cannot read the partition table of device %s, it's too corrupted for me :( I can try to go on, erasing over bad partitions (ALL DATA will be lost!). The other solution is to not allow DrakX to modify the partition table. (the error is %s) Do you agree to lose all the partitions? ", $hd->{device}, formatError($err)))) { partition_table::raw::zero_MBR($hd); } else { #- using it readonly log::l("using /proc/partitions since diskdrake failed :("); fs::proc_partitions::use_($hd); } } $hd or next; member($_->{device}, @{$flags->{clear} || []}) and partition_table::remove($hd, $_) foreach partition_table::get_normal_parts($hd); } my @parts = partition_table::get_normal_parts($hd); # checking the magic of the filesystem, do not rely on pt_type foreach (@parts) { if (my $type = fs::type::type_subpart_from_magic($_)) { $type->{pt_type} = $_->{pt_type}; #- keep {pt_type} put_in_hash($_, $type); } else { $_->{bad_fs_type_magic} = 1; } } if ($hd->{usb_media_type}) { $hd->{is_removable} = 1; $_->{is_removable} = 1 foreach @parts; } push @hds, $hd; } #- detect raids before LVM allowing LVM on raid my $raids = raids(\@hds); my $all_hds = { %{ fs::get::empty_all_hds() }, hds => \@hds, raw_hds => \@raw_hds, lvms => [], raids => $raids }; $all_hds->{lvms} = [ lvms($all_hds) ]; fs::get_major_minor([ fs::get::fstab($all_hds) ]); # must be done after getting major/minor $all_hds->{dmcrypts} = [ dmcrypts($all_hds) ]; # allow lvm on dmcrypt $all_hds->{lvms} = [ lvms($all_hds) ]; $_->{faked_device} = 0 foreach fs::get::fstab($all_hds); $all_hds; } #- are_same_partitions() do not look at the device name since things may have changed sub are_same_partitions { my ($part1, $part2) = @_; foreach ('start', 'size', 'pt_type', 'fs_type', 'rootDevice') { $part1->{$_} eq $part2->{$_} or return 0; } 1; } sub is_one_big_fat_or_NT { my ($hds) = @_; @$hds == 1 or return 0; my @l = fs::get::hds_fstab(@$hds); @l == 1 && isFat_or_NTFS($l[0]) && fs::get::hds_free_space(@$hds) < MB(10); } sub computeSize { my ($part, $best, $all_hds, $suggestions) = @_; my $max = $part->{maxsize} || $part->{size}; return min($max, $best->{size}) unless $best->{ratio}; my %free_space; $free_space{$_->{rootDevice}} += $_->{size} foreach fs::get::holes($all_hds); my @l = my @L = grep { my @possible = $_->{hd} ? $_->{hd} : keys %free_space; my $size = $_->{size}; if (my $dev = find { $free_space{$_} >= $size } @possible) { $free_space{$dev} -= $size; 1; } else { 0 } } @$suggestions; my $free_space = $best->{hd} && $free_space{$best->{hd}} || sum(values %free_space); my $cylinder_size_maxsize_adjusted; my $tot_ratios = 0; while (1) { my $old_free_space = $free_space; my $old_tot_ratios = $tot_ratios; $tot_ratios = sum(map { $_->{ratio} } @l); last if $tot_ratios == $old_tot_ratios; @l = grep { if ($_->{ratio} && $_->{maxsize} && $tot_ratios && $_->{size} + $_->{ratio} / $tot_ratios * $old_free_space >= $_->{maxsize}) { return min($max, $best->{maxsize}) if $best->{mntpoint} eq $_->{mntpoint}; $free_space -= $_->{maxsize} - $_->{size}; if (!$cylinder_size_maxsize_adjusted++) { eval { $free_space += fs::get::part2hd($part, $all_hds)->cylinder_size - 1 }; } 0; } else { $_->{ratio}; } } @l; } my $size = int min($max, $best->{size} + $free_space * ($tot_ratios && $best->{ratio} / $tot_ratios)); #- verify other entry can fill the hole (any { $_->{size} <= $max - $size } @L) ? $size : $max; } sub suggest_part { my ($part, $all_hds, $o_suggestions) = @_; my $suggestions = $o_suggestions || $suggestions{server} || $suggestions{simple}; #- suggestions now use {fs_type}, but still keep compatibility foreach (@$suggestions) { fs::type::set_pt_type($_, $_->{pt_type}) if !exists $_->{fs_type}; } my $hd = fs::get::part2hd($part, $all_hds); my $hd_size = $hd && $hd->{totalsectors}; # nb: no $hd if $part is /dev/mdX my $has_swap = any { isSwap($_) } fs::get::fstab($all_hds); my @local_suggestions = grep { !$_->{mntpoint} && !$_->{VG_name} || !fs::get::has_mntpoint($_->{mntpoint}, $all_hds) || isSwap($_) && !$has_swap } grep { !$_->{min_hd_size} || !$hd_size || $_->{min_hd_size} <= $hd_size } grep { !$_->{hd} || $_->{hd} eq $part->{rootDevice} } @$suggestions; #- this allows specifying the size using a relative size. #- one should rather use {ratio} instead foreach (@local_suggestions) { if ($_->{percent_size} && $_->{percent_size} =~ /(.+?)%?$/) { $_->{size} = $1 / 100 * $hd_size; log::l("in suggestion, setting size=$_->{size} for percent_size=$_->{percent_size}"); } } my ($best) = grep { !$_->{maxsize} || $part->{size} <= $_->{maxsize} } grep { $_->{size} <= ($part->{maxsize} || $part->{size}) } grep { !$part->{fs_type} || $part->{fs_type} eq $_->{fs_type} || isTrueFS($part) && isTrueFS($_) } @local_suggestions; defined $best or return 0; #- sorry no suggestion :( $part->{mntpoint} = $best->{mntpoint}; fs::type::set_type_subpart($part, $best) if !isTrueFS($best) || !isTrueFS($part); $part->{size} = computeSize($part, $best, $all_hds, \@local_suggestions); foreach ('options', 'lv_name', 'encrypt_key', 'primaryOrExtended', 'device_LABEL', 'prefer_device_LABEL', 'device_UUID', 'prefer_device_UUID', 'prefer_device') { $part->{$_} = $best->{$_} if $best->{$_}; } $best; } sub suggestions_mntpoint { my ($all_hds) = @_; sort grep { !/swap/ && !fs::get::has_mntpoint($_, $all_hds) } (@suggestions_mntpoints, map { $_->{mntpoint} } @{$suggestions{server} || $suggestions{simple}}); } #- you can do this before modifying $part->{mntpoint} #- so $part->{mntpoint} should not be used here, use $mntpoint instead sub check_mntpoint { my ($mntpoint, $part, $all_hds) = @_; $mntpoint eq '' || isSwap($part) || isNonMountable($part) and return 0; $mntpoint =~ m|^/| or die N("Mount points must begin with a leading /"); $mntpoint =~ m|[\x7f-\xff]| and cdie N("Mount points should contain only alphanumerical characters"); fs::get::mntpoint2part($mntpoint, [ grep { $_ ne $part } fs::get::really_all_fstab($all_hds) ]) and die N("There is already a partition with mount point %s\n", $mntpoint); if ($mntpoint eq "/" && isRAID($part) && !fs::get::has_mntpoint("/boot", $all_hds)) { # lilo handles / on RAID1 if ($part->{level} ne '1') { cdie N("You've selected a software RAID partition as root (/). No bootloader is able to handle this without a /boot partition. Please be sure to add a separate /boot partition"); } else { # LILO only handles 0.90 metadata if ($part->{isFormatted} && $part->{metadata} && $part->{metadata} ne '0.90') { cdie N("Metadata version unsupported for a boot partition. Please be sure to add a separate /boot partition."); } else { $part->{metadata} = '0.90'; } } } if ($mntpoint eq "/boot" && isRAID($part)) { die N("You've selected a software RAID partition as /boot. No bootloader is able to handle this.") if $part->{level} ne '1'; # lilo handles /boot on RAID1 # LILO only handles 0.90 metadata if ($part->{isFormatted} && $part->{metadata} && $part->{metadata} ne '0.90') { die N("Metadata version unsupported for a boot partition."); } else { $part->{metadata} = '0.90'; } } if ($mntpoint eq "/" && (isLUKS($part) || isRawLUKS($part)) && !fs::get::has_mntpoint("/boot", $all_hds)) { cdie N("You've selected an encrypted partition as root (/). No bootloader is able to handle this without a /boot partition. Please be sure to add a separate /boot partition"); } if ($mntpoint eq "/boot" && (isLUKS($part) || isRawLUKS($part))) { die N("You cannot use an encrypted filesystem for mount point %s", "/boot"); } #- NB: if the LV doesn't exist, lv_nb_pvs returns 0 die N("You cannot use the LVM Logical Volume for mount point %s since it spans physical volumes", $mntpoint) if $mntpoint eq '/boot' && isLVM($part) && lvm::lv_nb_pvs($part) > 1; cdie N("You've selected the LVM Logical Volume as root (/). The bootloader is not able to handle this when the volume spans physical volumes. You should create a separate /boot partition first") if $mntpoint eq "/" && isLVM($part) && lvm::lv_nb_pvs($part) != 1 && !fs::get::has_mntpoint("/boot", $all_hds); cdie N("This directory should remain within the root filesystem") if member($mntpoint, qw(/root)); die N("This directory should remain within the root filesystem") if member($mntpoint, qw(/bin /dev /etc /lib /sbin /mnt /media)); die N("You need a true filesystem (ext2/3/4, reiserfs, xfs, or jfs) for this mount point\n") if !isTrueLocalFS($part) && $mntpoint eq '/'; die N("You need a true filesystem (ext2/3/4, reiserfs, xfs, or jfs) for this mount point\n") if $part->{fs_type} eq 'btrfs' && $mntpoint eq '/boot'; die N("You need a true filesystem (ext2/3/4, reiserfs, xfs, or jfs) for this mount point\n") if !isTrueFS($part) && member($mntpoint, '/home', fs::type::directories_needed_to_boot()); die N("You cannot use an encrypted filesystem for mount point %s", $mntpoint) if $part->{options} =~ /encrypted/ && member($mntpoint, qw(/ /usr /var /boot)); local $part->{mntpoint} = $mntpoint; fs::loopback::check_circular_mounts($part, $all_hds); } sub add { my ($hd, $part, $all_hds, $options) = @_; isSwap($part) ? ($part->{mntpoint} = 'swap') : $options->{force} || check_mntpoint($part->{mntpoint}, $part, $all_hds); delete $part->{maxsize}; if (isLVM($hd)) { lvm::lv_create($hd, $part); } else { partition_table::add($hd, $part, $options->{primaryOrExtended}); } fs::get_major_minor([ $part ]); } sub allocatePartitions { my ($all_hds, $to_add) = @_; my @to_add = @$to_add; foreach my $part_ (fs::get::holes($all_hds, 'non_readonly')) { my ($start, $size, $dev) = @$part_{"start", "size", "rootDevice"}; my ($part, $suggested); while ($suggested = suggest_part($part = { start => $start, size => 0, maxsize => $size, rootDevice => $dev }, $all_hds, \@to_add)) { my $hd = fs::get::part2hd($part, $all_hds); add($hd, $part, $all_hds, { primaryOrExtended => $part->{primaryOrExtended} }); $size -= $part->{size} + $part->{start} - $start; $start = $part->{start} + $part->{size}; @to_add = grep { $_ != $suggested } @to_add; } } } sub auto_allocate { my ($all_hds, $o_suggestions) = @_; my $before = listlength(fs::get::fstab($all_hds)); my $suggestions = $o_suggestions || $suggestions{simple}; allocatePartitions($all_hds, $suggestions); if ($o_suggestions) { auto_allocate_raids($all_hds, $suggestions); if (auto_allocate_vgs($all_hds, $suggestions)) { #- allocatePartitions needs to be called twice, once for allocating PVs, once for allocating LVs my @vgs = map { $_->{VG_name} } @{$all_hds->{lvms}}; my @suggested_lvs = grep { member($_->{hd}, @vgs) } @$suggestions; allocatePartitions($all_hds, \@suggested_lvs); } } partition_table::assign_device_numbers($_) foreach @{$all_hds->{hds}}; if ($before == listlength(fs::get::fstab($all_hds))) { # find out why auto_allocate failed if (any { !fs::get::has_mntpoint($_->{mntpoint}, $all_hds) } @$suggestions) { die N("Not enough free space for auto-allocating"); } else { die N("Nothing to do"); } } } sub auto_allocate_raids { my ($all_hds, $suggestions) = @_; my @raids = grep { isRawRAID($_) } fs::get::fstab($all_hds) or return; require raid; my @mds = grep { $_->{hd} =~ /md/ } @$suggestions; foreach my $md (@mds) { my @raids_ = grep { !$md->{parts} || $md->{parts} =~ /\Q$_->{mntpoint}/ } @raids; @raids = difference2(\@raids, \@raids_); my %h = %$md; delete @h{'hd', 'parts'}; # keeping mntpoint, level, chunk-size, fs_type/pt_type $h{disks} = \@raids_; my $part = raid::new($all_hds->{raids}, %h); raid::updateSize($part); push @raids, $part; #- we can build raid over raid } } sub auto_allocate_vgs { my ($all_hds, $suggestions) = @_; my @pvs = grep { isRawLVM($_) } fs::get::fstab($all_hds) or return 0; my @vgs = grep { $_->{VG_name} } @$suggestions or return 0; partition_table::write($_) foreach @{$all_hds->{hds}}; require lvm; foreach my $vg (@vgs) { my $lvm = new lvm($vg->{VG_name}); push @{$all_hds->{lvms}}, $lvm; my @pvs_ = grep { !$vg->{parts} || $vg->{parts} =~ /\Q$_->{mntpoint}/ } @pvs; @pvs = difference2(\@pvs, \@pvs_); foreach my $part (@pvs_) { raid::make($all_hds->{raids}, $part) if isRAID($part); $part->{lvm} = $lvm->{VG_name}; delete $part->{mntpoint}; lvm::vg_add($part); push @{$lvm->{disks}}, $part; } lvm::update_size($lvm); } 1; } sub change_type { my ($type, $hd, $part) = @_; $type->{pt_type} != $part->{pt_type} || $type->{fs_type} ne $part->{fs_type} or return; fs::type::check($type->{fs_type}, $hd, $part); delete $part->{device_UUID}; $hd->{isDirty} = 1; $part->{mntpoint} = '' if isSwap($part) && $part->{mntpoint} eq "swap"; $part->{mntpoint} = '' if fs::type::cannotBeMountable($part); set_isFormatted($part, 0); fs::type::set_type_subpart($part, $type); fs::mount_options::rationalize($part); 1; } sub partition_table_clear_and_initialize { my ($lvms, $hd, $o_in, $o_type, $b_warn) = @_; $hd->clear_existing; partition_table_initialize($lvms, $hd, $o_in, $o_type, $b_warn); } sub partition_table_initialize { my ($lvms, $hd, $o_in, $o_type, $b_warn) = @_; partition_table::initialize($hd, $o_type); if ($hd->isa('partition_table::lvm')) { if ($b_warn && $o_in) { $o_in->ask_okcancel_('', N("ALL existing partitions and their data will be lost on drive %s", partition_table::description($hd))) or return; } require lvm; lvm::check($o_in ? $o_in->do_pkgs : do_pkgs_standalone->new) if $::isStandalone; lvm::create_singleton_vg($lvms, fs::get::hds_fstab($hd)); } } 1;