summaryrefslogtreecommitdiffstats
path: root/lib/MGA/DrakISO/Utils.pm
blob: 40495e64db8de46e8b0c1a06276248095b593505 (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
package MGA::DrakISO::Utils;

use strict;

use MDK::Common;
use common;
use fs;
use run_program;
use IPC::Open3;
use IO::Select;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(directory_usage run_ mount_system_fs umount_external_fs);

sub directory_usage {
    my ($dir, $o_apparent) = @_;
    my $apparent = $o_apparent && "--apparent-size";
    first(split /\s/, `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 $options = ref $_[0] eq 'HASH' ? shift @_ : {};
    my @cmd = @_;
    $options->{timeout} ||= 'never';
    if (arch() !~ /^arm/) {
        my $targetarch = delete $options->{targetarch};
        unshift @cmd, 'setarch', $targetarch if $targetarch;
    }
    print STDERR "running " . (exists $options->{root} && "(in chroot) ") . join(' ', @cmd) . "\n";
    run_program::raw($options, @cmd);
}

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 "unable to mkfs for unsupported media type $type\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');
}

sub umount_external_fs {
    my ($build) = @_;
    my $system_root = Cwd::abs_path($build->get_system_root);
    my @mounts = grep { $_ =~ $system_root } split("\n", cat_('/proc/mounts'));
    foreach (reverse(@mounts)) {
        my @field = split(' ' , $_);
        fs::mount::umount($field[1]);
    }
}

1;