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
|
# Copyright (C) 2005 Mandriva
# Olivier Blin <oblin@mandriva.com>
# Copyright (C) 2017-2018 Mageia
# Martin Whitaker <mageia@martin-whitaker.me.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA.
# SYNOPSIS
# --------
# This package provides miscellaneous helper functions.
package MGA::DrakISO::Utils;
use strict;
use MDK::Common;
use common;
use fs;
use IPC::Open3;
use IO::Select;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($sudo directory_usage glob__ run_ run_as_root run_in_root copy_or_link
mk_dev_null rm_dev_null mount mount_system_fs umount_all_in_root);
our $sudo = $> ? 'sudo' : '';
sub directory_usage {
my ($dir, $o_apparent) = @_;
my $apparent = $o_apparent && "--apparent-size";
first(split /\s/, `$sudo du -s -B 1 $apparent $dir`);
}
#- expand only if the pattern contains '*'
#- and matches dot characters (like shell dotglob)
sub glob__ {
my ($pattern) = @_;
$pattern =~ /\*/ ? glob_($pattern) : $pattern;
}
sub run_ {
my @cmd = @_;
print "@cmd\n" if $::verbose > 2;
system(@cmd) == 0;
}
sub run_as_root {
my @cmd = @_;
if (@cmd > 1) {
unshift @cmd, $sudo if $sudo;
} else {
$cmd[0] = join(' ', $sudo, @cmd[0]);
}
run_(@cmd);
}
sub run_in_root {
my ($root, $arch, @cmd) = @_;
if (@cmd > 1) {
unshift @cmd, ('chroot', $root);
unshift @cmd, ('setarch', $arch) if $arch;
unshift @cmd, $sudo if $sudo;
} else {
$cmd[0] = join(' ', $sudo, if_($arch, 'setarch', $arch), 'chroot', $root, @cmd[0]);
}
print "@cmd\n" if $::verbose > 2;
local %ENV = %ENV;
$ENV{LC_ALL} = 'C';
system(@cmd) == 0;
}
sub device_allocate_file {
my ($device, $size) = @_;
run_('dd', "of=$device", 'count=0', 'bs=1', "seek=" . removeXiBSuffix($size));
}
#- format $device as type $type
# FIXME: use fs::format
sub device_mkfs {
my ($device, $type, $o_label, $o_inode_size) = @_;
if ($type eq 'vfat') {
run_('mkfs.vfat', if_(defined $o_label, '-n', $o_label), $device);
} elsif (member($type, 'ext2', 'ext3', 'ext4')) {
run_("mkfs.$type", "-m", 0,
if_(defined $o_label, '-L', $o_label),
if_($o_inode_size, '-I', $o_inode_size),
if_(!-b $device, '-F'),
$device);
} elsif ($type eq 'swap') {
run_('mkswap', if_(defined $o_label, '-L', $o_label), $device);
} else {
die "ERROR: unable to mkfs for unsupported media type $type\n";
}
}
sub copy_or_link {
my ($src_file, $dst_file) = @_;
# TODO: support remote sources.
mkdir_p(dirname($dst_file));
symlinkf($src_file, $dst_file)
or die "ERROR: couldn't link $src_file to $dst_file\n";
}
sub mk_dev_null {
my ($root) = @_;
$root or die;
return if -e $root . '/dev/null';
run_as_root('mkdir', '-p', $root . '/dev');
run_as_root('cp', '-a', '/dev/null', $root . '/dev');
}
sub rm_dev_null {
my ($root) = @_;
$root or die;
run_as_root('rm', $root . '/dev/null') if -e $root . '/dev/null';
}
sub mount {
my ($dst, $src, @o_options) = @_;
mkdir_p($dst);
run_as_root('mount', @o_options, $src, $dst)
or die "ERROR: failed to mount $src on $dst\n";
}
sub mount_system_fs {
my ($root) = @_;
$root or die;
mount($root . '/dev', '/dev', '--bind', '-o', 'ro');
mount($root . '/proc', 'none', '-t', 'proc');
mount($root . '/sys', 'none', '-t', 'sysfs');
mount($root . '/run', 'none', '-t', 'tmpfs');
}
sub umount_all_in_root {
my ($root) = @_;
$root or die;
$root = Cwd::abs_path($root);
my @mounts = grep { $_ =~ $root } split("\n", cat_('/proc/mounts'));
foreach (reverse(@mounts)) {
my @field = split(' ' , $_);
run_as_root('umount', $field[1]);
}
}
1;
|