/[42-fs]/42.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /42.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations)
Sun Jul 19 11:55:06 2009 UTC (10 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 2581 byte(s)
rewrite to use parted and extended partition to store files

1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use File::Slurp;
7 use File::Path;
8 use Getopt::Long;
9 use Data::Dump qw/dump/;
10
11 my $dev = '/dev/sdb';
12 my $mnt = '/mnt/42';
13
14 my ( $init, $debug );
15
16 GetOptions(
17 'init!' => \$init,
18 'debug!' => \$debug,
19 ) or die "unknown options: $!";
20
21
22 my @part;
23
24 sub partitions {
25 open(my $parted, '-|', "parted -s $dev unit mb print free") || die "parted: $!";
26 while(<$parted>) {
27 warn "## $_" if $debug;
28 if ( m{^\s+([\d\.]+)MB\s+([\d\.]+)MB\s+([\d\.]+)MB\s+Free Space} ) {
29 $part[0] = [ $1, $2, $3, 'free' ];
30 }
31 next unless m{^\s+\d+\s+};
32 s{^\s+}{};
33 s{([\d\.]+)MB}{$1}g;
34 my ( $nr, $start, $end, $size, undef ) = split(/\s+/, $_, 5);
35 $part[$nr] = [ $start, $end, $size ];
36 }
37 warn "## part = ",dump( @part ) if $debug;
38 return @part;
39 }
40
41 sub parted {
42 my $command = shift;
43 warn "+ $command\n";
44 system("parted -s $dev unit mb $command") == 0 or die "parted: $!";
45 }
46
47
48 if ( $init ) {
49 system("umount $mnt");
50 partitions();
51 parted("rm $_") foreach grep { defined $part[$_] } ( 1 .. 4 );
52 parted("mkpartfs primary ext2 0 42MB");
53 partitions();
54 parted("mkpart extended 42MB " . $part[0]->[1] . 'MB');
55 system "sync;sync";
56 partitions();
57 system("mount ${dev}1 $mnt") == 0 or die "can't mount: $!";
58 my $cmd = read_file($0);
59 write_file( "$mnt/42.pl", $cmd );
60 chmod 0755, "$mnt/42.pl";
61 exit;
62 }
63
64 map { mkdir $_ unless -e $_ } map { "$mnt/$_" } ( 'log', 'fs', 'stat' );
65
66 my $path = shift @ARGV || die "usage: $0 /path/to/file\n";
67
68 my ($dir,$file) = ($1,$2) if $path =~ m{^(?:(.+)/)?([^/]+)};
69
70 my $fs = "$mnt/fs/$dir";
71 mkpath $fs unless -e $fs;
72
73 $fs .= '/' . $file;
74 die "$fs exists!" if -e $fs;
75
76 my @stat = stat($path);
77 die "can't stat $path: $!" unless @stat;
78 warn "# $path ",$stat[7],$/;
79
80 partitions();
81
82 my $size_mb = int( $stat[7] / 1000 / 1000 ) + 1;
83 my ( $free_start, undef, $free_size ) = @{$part[0]};
84
85 my $part_end = $free_start + $size_mb;
86
87 my $last_part = $#part;
88
89 system("umount $mnt");
90 parted("mkpart logical ${free_start}MB ${part_end}MB");
91 partitions();
92 system("mount ${dev}1 $mnt") == 0 or die "can't mount $mnt: $!";
93
94 my $part_size = $part[$#part]->[2] || die "can't get size of new partition";
95
96 die "not enough space on $dev $size_mb > $part_size" if $size_mb > $part_size;
97
98 my $part_nr = $#part;
99
100 die "can't create partition $last_part == $part_nr" if $last_part == $part_nr;
101
102 write_file( "$mnt/stat/$part_nr", join("\n",@stat) );
103
104 my $log = "$mnt/log/$part_nr";
105
106 my $to_dev = $dev . $part_nr;
107
108 symlink( $to_dev ,$fs) || die "can't create $fs: $!";
109
110 my $cmd = "dd_rescue -w -l $log $path $to_dev";
111 print "+ $cmd\n";
112 exec $cmd;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26