summaryrefslogtreecommitdiffstats
path: root/perl-install/fs/get.pm
blob: 46198443173ef3f685c07dce7ad7d9a298a1eaec (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
package fs::get; # $Id: get.pm 245972 2008-09-18 17:00:11Z pixel $

use diagnostics;
use strict;

use partition_table;
use fs::type;
use fs::loopback;
use fs::wild_device;
use fs;
use common;
use log;

sub empty_all_hds() {
    { hds => [], lvms => [], raids => [], dmcrypts => [], loopbacks => [], raw_hds => [], nfss => [], smbs => [], davs => [], special => [] };
}
sub fstab {
    my ($all_hds) = @_;
    my @parts = map { partition_table::get_normal_parts($_) } hds($all_hds);
    @parts, @{$all_hds->{raids}}, @{$all_hds->{dmcrypts}}, @{$all_hds->{loopbacks}};
}
sub really_all_fstab {
    my ($all_hds) = @_;
    my @l = fstab($all_hds);
    @l, @{$all_hds->{raw_hds}}, @{$all_hds->{nfss}}, @{$all_hds->{smbs}}, @{$all_hds->{davs}};
}

sub fstab_and_holes {
    my ($all_hds, $b_non_readonly) = @_;
    my @hds = grep { !($b_non_readonly && $_->{readonly}) } hds($all_hds);
    hds_fstab_and_holes(@hds), @{$all_hds->{raids}}, @{$all_hds->{dmcrypts}}, @{$all_hds->{loopbacks}};
}

sub holes {
    my ($all_hds, $b_non_readonly) = @_;
    grep { isEmpty($_) } fstab_and_holes($all_hds, $b_non_readonly);
}
sub hds_holes {
    grep { isEmpty($_) } hds_fstab_and_holes(@_);
}
sub free_space {
    my ($all_hds) = @_;
    sum map { $_->{size} } holes($all_hds);
}
sub hds_free_space {
    sum map { $_->{size} } hds_holes(@_);
}

sub hds {
    my ($all_hds) = @_;
    (@{$all_hds->{hds}}, @{$all_hds->{lvms}});
}

#- get all normal partition including special ones as found on sparc.
sub hds_fstab {
    map { partition_table::get_normal_parts($_) } @_;
}

sub vg_free_space {
    my ($hd) = @_;
    my @parts = partition_table::get_normal_parts($hd);
    $hd->{totalsectors} - sum map { $_->{size} } @parts;
}

sub hds_fstab_and_holes {
    map {
	if (isLVM($_)) {
	    my @parts = partition_table::get_normal_parts($_);
	    my $free = vg_free_space($_);
	    my $free_part = { start => 0, size => $free, pt_type => 0, rootDevice => $_->{VG_name} };
	    @parts, if_($free >= $_->cylinder_size, $free_part);
	} else {
	    partition_table::get_normal_parts_and_holes($_);
	}
    } @_;
}


sub device2part {
    my ($dev, $fstab) = @_;
    my $subpart = fs::wild_device::to_subpart($dev);
    my $part = find { is_same_hd($subpart, $_) } @$fstab;
    log::l("fs::get::device2part: unknown device <<$dev>>") if !$part;
    $part;
}

sub part2hd {
    my ($part, $all_hds) = @_;
    my $hd = find { $part->{rootDevice} eq ($_->{device} || $_->{VG_name}) } hds($all_hds);
    $hd;
}

sub file2part {
    my ($fstab, $file, $b_keep_simple_symlinks) = @_;    
    my $part;

    $file = $b_keep_simple_symlinks ? common::expand_symlinks_but_simple("$::prefix$file") : expand_symlinks("$::prefix$file");
    unless ($file =~ s/^$::prefix//) {
	my $part = find { fs::type::carry_root_loopback($_) } @$fstab or die;
	log::l("found $part->{mntpoint}");
	$file =~ s|/initrd/loopfs|$part->{mntpoint}|;
    }
    foreach (@$fstab) {
	my $m = $_->{mntpoint};
	$part = $_ if 
	  $file =~ /^\Q$m/ && 
	    (!$part || length $part->{mntpoint} < length $m);
    }
    $part or die "file2part: not found $file";
    $file =~ s|$part->{mntpoint}/?|/|;
    ($part, $file);
}

sub mntpoint2part {
    my ($mntpoint, $fstab) = @_;
    find { $mntpoint eq $_->{mntpoint} } @$fstab;
}
sub has_mntpoint {
    my ($mntpoint, $all_hds) = @_;
    mntpoint2part($mntpoint, [ really_all_fstab($all_hds) ]);
}
sub root_ {
    my ($fstab, $o_boot) = @_;
    $o_boot && mntpoint2part("/boot", $fstab) || mntpoint2part("/", $fstab);
}
sub root { &root_ || {} }

sub up_mount_point {
    my ($mntpoint, $fstab) = @_;
    while (1) {
	$mntpoint = dirname($mntpoint);
	$mntpoint ne "." or return;
	$_->{mntpoint} eq $mntpoint and return $_ foreach @$fstab;
    }
}

sub is_same_hd {
    my ($hd1, $hd2) = @_;
    if ($hd1->{major} && $hd2->{major}) {
	$hd1->{major} == $hd2->{major} && $hd1->{minor} == $hd2->{minor};
    } elsif (my ($s1) = $hd1->{device} =~ m|https?://(.+?)/*$|) {
	my ($s2) = $hd2->{device} =~ m|https?://(.+?)/*$|;
	$s1 eq $s2;
    } else {
	$hd1->{device_LABEL} && $hd2->{device_LABEL} && $hd1->{device_LABEL} eq $hd2->{device_LABEL}
	  || $hd1->{device_UUID} && $hd2->{device_UUID} && $hd1->{device_UUID} eq $hd2->{device_UUID}
	  || $hd1->{device} && $hd2->{device} && $hd1->{device} eq $hd2->{device}
	  || $hd1->{device} && $hd2->{device_alias} && $hd1->{device} eq $hd2->{device_alias}
	  || $hd1->{device_alias} && $hd2->{device} && $hd1->{device_alias} eq $hd2->{device}
	  || $hd1->{device_alias} && $hd2->{device_alias} && $hd1->{device_alias} eq $hd2->{device_alias};
    }
}

sub mntpoint_prefixed {
    my ($part) = @_;
    $::prefix . $part->{mntpoint};
}

1;
med bootparamd ntpd xntpd chronyd postfix sendmail # imap imaps ipop2 ipop3 pop3s routed yppasswdd ypserv ldap dhcpd dhcrelay # hylafax innd identd rstatd rusersd rwalld rwhod gated # kadmin kprop krb524 krb5kdc krb5server hldsld bayonne sockd dhsd gnu-pop3d # gdips pptpd.conf vrrpd crossfire bnetd pvmd ircd sympa finger ntalk talk) ], N("Database Server") => [ qw(mysql postgresql) ], ); my %services_root; foreach my $root (keys %root_services) { $services_root{$_} = $root foreach @{$root_services{$root}}; } my ($l, $on_services) = services(); my %services; $services{$_} = 0 foreach @{$l || []}; $services{$_} = 1 foreach @{$on_services || []}; $in->ask_browse_tree_info('drakxservices', N("Choose which services should be automatically started at boot time"), { node_state => sub { $services{$_[0]} ? 'selected' : 'unselected' }, build_tree => sub { my ($add_node, $flat) = @_; $add_node->($_, !$flat && ($services_root{$_} || N("Other"))) foreach sort keys %services; }, grep_unselected => sub { grep { !$services{$_} } @_ }, toggle_nodes => sub { my ($set_state, @nodes) = @_; my $new_state = !$services{$nodes[0]}; foreach (@nodes) { $set_state->($_, $new_state ? 'selected' : 'unselected'); $services{$_} = $new_state; } }, get_status => sub { N("Services: %d activated for %d registered", scalar(grep { $_ } values %services), scalar(values %services)); }, get_info => sub { formatLines(description($_[0])) }, interactive_help_id => 'configureServices', }) or return $l, $on_services; #- no change on cancel. [ grep { $services{$_} } @$l ]; } sub ask_standalone_gtk { my ($_in) = @_; my ($l, $on_services) = services(); my @xinetd_services = map { $_->[0] } @{(services_raw())[1]}; require ugtk2; ugtk2->import(qw(:wrappers :create)); my $W = ugtk2->new(N("Services")); my ($x, $y, $w_popup); my $nopop = sub { $w_popup and $w_popup->destroy; undef $w_popup }; my $display = sub { my ($text) = @_; $nopop->(); gtkshow(gtkadd($w_popup = Gtk2::Window->new('popup'), gtksignal_connect(gtkadd(Gtk2::EventBox->new, gtkadd(gtkset_shadow_type(Gtk2::Frame->new, 'etched_out'), gtkset_justify(Gtk2::Label->new($text), 'left'))), button_press_event => sub { $nopop->() } )))->move($x, $y) if $text; }; my $update_service = sub { my ($service, $label) = @_; my $started = -e "/var/lock/subsys/$service"; $label->set_label($started ? N("running") : N("stopped")); }; my $b = Gtk2::EventBox->new; $b->set_events('pointer_motion_mask'); gtkadd($W->{window}, gtkadd($b, gtkpack_($W->create_box_with_title(N("Services and daemons")), 1, gtkset_size_request(create_scrolled_window(create_packtable({ col_spacings => 10, row_spacings => 3 }, map { my $service = $_; my $is_xinetd_service = member($service, @xinetd_services); my $infos = warp_text(description($_), 40); $infos ||= N("No additional information\nabout this service, sorry."); my $label = gtkset_justify(Gtk2::Label->new, 'left'); $update_service->($service, $label) if !$is_xinetd_service; [ gtkpack__(Gtk2::HBox->new(0,0), $_), gtkpack__(Gtk2::HBox->new(0,0), $label), gtkpack__(Gtk2::HBox->new(0,0), gtksignal_connect(Gtk2::Button->new(N("Info")), clicked => sub { $display->($infos) })), gtkpack__(Gtk2::HBox->new(0,0), gtkset_active(gtksignal_connect( Gtk2::CheckButton->new($is_xinetd_service ? N("Start when requested") : N("On boot")), clicked => sub { if ($_[0]->get_active) { push @$on_services, $service if !member($service, @$on_services); } else { @$on_services = grep { $_ ne $service } @$on_services; } }), member($service, @$on_services))), map { my $a = $_; gtkpack__(Gtk2::HBox->new(0,0), gtksignal_connect(Gtk2::Button->new(translate($a)), clicked => sub { my $action = $a eq "Start" ? 'restart' : 'stop'; # as we need the output in UTF-8, force it local $_ = `GP_LANG="UTF-8" service $service $action 2>&1`; s/\033\[[^mG]*[mG]//g; c::set_tagged_utf8($_); $update_service->($service, $label); $display->($_); })) if !$is_xinetd_service; } (N_("Start"), N_("Stop")) ]; } @$l), [ $::isEmbedded ? 'automatic' : 'never', 'automatic' ]), -1, $::isEmbedded ? -1 : 400), 0, gtkpack(gtkset_border_width(Gtk2::HBox->new(0,0),5), $W->create_okcancel) )) ); $b->signal_connect(motion_notify_event => sub { my ($w, $e) = @_; my ($ox, $oy) = $w->window->get_origin; $x = $e->x+$ox; $y = $e->y+$oy }); $b->signal_connect(button_press_event => sub { $nopop->() }); $::isEmbedded and gtkflush(); $W->main or return; $on_services; } sub ask { my ($in) = @_; !$::isInstall && $in->isa('interactive::gtk') ? &ask_standalone_gtk : &ask_install; } sub doit { my ($in, $on_services) = @_; my ($l, $was_on_services) = services(); foreach (@$l) { my $before = member($_, @$was_on_services); my $after = member($_, @$on_services); if ($before != $after) { my $script = "/etc/rc.d/init.d/$_"; run_program::rooted($::prefix, "chkconfig", $after ? "--add" : "--del", $_); if ($after && cat_("$::prefix$script") =~ /^#\s+chkconfig:\s+-/m) { run_program::rooted($::prefix, "chkconfig", "--level", "35", $_, "on"); } if (!$after && !$::isInstall && !$in->isa('interactive::gtk')) { #- only done after install AND when not using the gtk frontend (since it allows one to start/stop services) #- this allows to skip stopping service "dm" run_program::rooted($::prefix, $script, "stop"); } } } } sub services_raw() {