summaryrefslogtreecommitdiffstats
path: root/lib/MGA/DrakISO/Utils.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/MGA/DrakISO/Utils.pm')
-rw-r--r--lib/MGA/DrakISO/Utils.pm60
1 files changed, 49 insertions, 11 deletions
diff --git a/lib/MGA/DrakISO/Utils.pm b/lib/MGA/DrakISO/Utils.pm
index 85a9892..4e85207 100644
--- a/lib/MGA/DrakISO/Utils.pm
+++ b/lib/MGA/DrakISO/Utils.pm
@@ -34,12 +34,14 @@ use IO::Select;
use Exporter;
our @ISA = qw(Exporter);
-our @EXPORT = qw(directory_usage run_ copy_or_link mount_system_fs umount_all_in_chroot);
+our @EXPORT = qw($sudo directory_usage glob__ run_ run_as_root run_in_root copy_or_link 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/, `du -s -B 1 $apparent $dir`);
+ first(split /\s/, `$sudo du -s -B 1 $apparent $dir`);
}
#- expand only if the pattern contains '*'
@@ -61,6 +63,34 @@ sub run_ {
run_program::raw($options, @cmd);
}
+sub run_as_root {
+ my (@cmd) = @_;
+
+ if (@cmd > 1) {
+ unshift @cmd, $sudo if $sudo;
+ } else {
+ @cmd[0] = join(' ', $sudo, @cmd[0]);
+ }
+ print "@cmd\n" if $::verbose > 2;
+ system(@cmd) == 0;
+}
+
+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));
@@ -93,20 +123,28 @@ sub copy_or_link {
or die "ERROR: couldn't link $src_file to $dst_file\n";
}
+sub mount {
+ my ($dst, $src, $o_options) = @_;
+ mkdir_p($dst);
+ system(join(' ', 'sudo mount', $o_options, $src, $dst)) == 0
+ or die "ERROR: failed to mount $src on $dst\n";
+}
+
sub mount_system_fs {
- my ($build) = @_;
- run_('mount', '-t', 'devtmpfs', '/dev', $build->get_system_root . '/dev');
- run_('mount', '-t', 'proc', '/proc', $build->get_system_root . '/proc');
- run_('mount', '-t', 'sysfs', '/sys', $build->get_system_root . '/sys');
+ my ($root) = @_;
+ 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_chroot {
- my ($build) = @_;
- my $system_root = Cwd::abs_path($build->get_system_root);
- my @mounts = grep { $_ =~ $system_root } split("\n", cat_('/proc/mounts'));
+sub umount_all_in_root {
+ my ($root) = @_;
+ $root = Cwd::abs_path($root);
+ my @mounts = grep { $_ =~ $root } split("\n", cat_('/proc/mounts'));
foreach (reverse(@mounts)) {
my @field = split(' ' , $_);
- fs::mount::umount($field[1]);
+ system(join(' ', 'sudo umount', $field[1]));
}
}