This file is indexed.

/usr/sbin/debian-edu-fsautoresize is in debian-edu-config 1.702.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/perl
#
# Author:  Petter Reinholdtsen
# Date:    2006-09-19
# License: GNU General Public License
#
# This is the fsautoresize system.  It checks file systems, and
# automatically extend the full ones based on the provided
# instructions.


use strict;
use warnings;

use Getopt::Std;
use Sys::Syslog qw(openlog syslog closelog LOG_NOTICE);

# Using this module (instead of Filesys::DiskSpace) to get a version
# providing the device size, and not only free and used.
use Filesys::Df; # from debian package libfilesys-df-perl

# Config

# for each mount point in the list, extend it when it become too full
# (based on real size in KiB/MiB/GiB or percent), and increase it with
# either a static size or a percentage, until it reaches the upper
# limit (in real size or fraction of volume group).  Should also
# support scripting to decide if the partition should be extended or
# not, to handle more advanced logic like 100 MiB per user with home
# directories on /home/.

# read config
#
#  Format:
#    [regex]  minsizefree/min%free  maxsize/max%size (of vg)  size/% incrememt
# The last matching regex take effect.  Example:
#  .+ 1% 20g 10%
#  /usr 10% 10g 1g

my @conffiles = qw(/usr/share/debian-edu-config/fsautoresizetab
                   /site/etc/fsautoresizetab
                   /etc/fsautoresizetab);

my %opts;
getopts("dnv", \%opts) || usage();

# handle signals (for reload and shutdown)

# Check if all the listed mount points support online extending

# loop

#   check full file systems (libfilesys-df-perl, libfilesys-diskfree-perl,
#                            libfilesys-diskspace-perl,libfilesys-statvfs-perl)
#   resize if available space in volume group
#   send email if resize succeeded


$ENV{PATH} = "/sbin:/usr/sbin:/bin:/usr/bin";

my %fsops =
(
    'ext3' => {
        'online_supported' => \&ext3_online_supported,
        'online_resize' => \&ext3_online_resize,
    },
);

my %devopts =
(
    'lvm' => {
        'resize' => \&lvm_resize,
    }
);

sub usage {
    print <<EOF;
Usage: $0 [-dnv]

  Resize full partitions based on configuration templates.

   -d     run in the background and resize when partitions are full (experimental)
   -v     verbose output
   -n     disable dryrun, do the resizing

EOF

    print "The configuration is read from\n";
    print "  ", join("\n  ", @conffiles), "\n";
    exit 1;
}

sub run {
    my @cmd = @_;
    print STDERR "exec: ", join(" ", @cmd, $opts{n} ? "" : " (dryrun, add -n to activate)", "\n");
    if ($opts{n}) {
        system(@cmd);
        return 0 == $?;
    } else {
        return 1;
    }
    return undef;
}

sub ext3_online_supported {
    my %minfo = @_;
    my $device = $minfo{device};
    my $supported = 0;
    open(TUNE2FS, "tune2fs -l $device 2>/dev/null |") || die "Unable to check $device";
    while (<TUNE2FS>) {
        chomp;
        $supported = 1 if (m/Filesystem features: .*resize_inode /);
    }
    close(TUNE2FS);
    return $supported;
}

sub ext3_online_resize {
    my (%minfo) = @_;
    my $device = $minfo{device};

    my $device_resize = $devopts{$minfo{devicetype}}->{resize};
    my $retval;
    if (&$device_resize($minfo{device}, $minfo{newsize})) {
        $retval = run "resize2fs", "$device";
        if (!$retval) {
            # Perhaps resize2fs is too old.  Try ext2online instead.
            my $retval = run "ext2online", "$device";
        }
    } else {
        print STDERR "error: unable to resize $device\n";
    }
    # ext2online ?

    # fsck -f, lvresize, resize2fs
    return $retval;
}

sub lvm_resize {
    my ($device, $newsize) = @_;

    $device = map_dev_to_lvmdev($device);

    # Using lvextend and not lvresize, to make sure we do not try to
    # shrink the file system.
    return run("lvextend","-L${newsize}k", "$device");
}

sub map_dev_to_lvmdev {
    my $device = shift;
    my ($vg, $lv) = $device =~ m%/dev/mapper/([^-]+)-(.+)$%;
    if ($vg) { # Remap if using stupid new linux kernel and/or tools
        $device = "/dev/$vg/$lv";
    }
    return $device
}

sub guess_devicetype {
    my $device = shift;

    # Try to find the real device, to handle /dev/vg/lv -> /dev/mapper/vg-lv
    $device = readlink $device if ( -l $device );

    return "lvm" if ($device =~ m%^/dev/mapper/.+-.+$% || $device =~ m%^../dm-.+$%);
    return undef;
}

sub get_volumecapasity {
    my $device = shift;
    return 1000; # FIXME Placeholder
}

sub get_lvextents {
    my $device = shift;
    $device = map_dev_to_lvmdev($device);

    open(my $fh, "lvdisplay -c $device 2>/dev/null |") or
        die "Unable to extract lvm lv extent size for $device";
    my @f = split(/:/, <$fh>);
    close($fh);

    return @f[6,7];
}

sub supported_mountpoints {
    my %mountpoints;
    my %devices;
    open(M, "/proc/mounts") || die "Unable to open /proc/mounts";
    while (<M>) {
        chomp;
        my @f = split(/\s+/);
        my $device = $f[0];
        my $mountpoint = $f[1];
        my $typename = $f[2];
        next unless (exists $fsops{$typename});
        my $devicetype = guess_devicetype($device),
        my $extents;

        # Only devices mounted several times once
        next if $devices{$device};
        $devices{$device} = 1;

        print STDERR "Checking $mountpoint [$device]\n" if $opts{v};
        if ( -d $mountpoint && -e $device) { # df only work if the directory is available
            my $ref = df($mountpoint);
            my ($size, $used, $avail) =
                ($ref->{blocks}, $ref->{used}, $ref->{bavail});
#            my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) =
#                df $mountpoint;

            if (defined $devicetype && "lvm" eq $devicetype) {
                my ($volsizeblocks, $extents) = get_lvextents($device);
                # Convert from 512 byte blocks to kilobytes
                $size = $volsizeblocks/2;
            }

            my $fracavail = 100 * $avail / $size;
            print STDERR "  A: $size $used $avail ($fracavail%)\n" if $opts{v};
            my %minfo =
                (
                 mountpoint => $mountpoint,
                 device     => $device,
                 devicetype => $devicetype,
                 fstype     => $typename,
                 extents    => $extents,
                 # These three are in kilobytes
                 used       => $used,
                 available  => $avail,
                 size       => $size,
                 volsize    => get_volumecapasity($device),
                 );
            next unless (defined $minfo{'devicetype'} && exists
                         $devopts{$minfo{'devicetype'}});

            $mountpoints{$mountpoint} = \%minfo;
        }

    }
    close(M);
    return %mountpoints;
}

sub calculate_resize {
    my ($minforef, $configref) = @_;
    my $mountpoint = $minforef->{mountpoint};
    my $lastconfig;
    for my $config (@$configref) {
        my $regex = ${$config}{'regex'};
        if ($mountpoint =~ m/^$regex$/) {
#            print STDERR "Matching '$regex'\n" if $opts{v};
            $lastconfig = $config;
        }
    }

    if ($lastconfig) {
        my $minfree   = $lastconfig->{minfree};
        my $maxsize   = $lastconfig->{maxsize};
        my $increment = $lastconfig->{increment};
        print(STDERR "info: $mountpoint matched regex ", $lastconfig->{regex},
              " from ", $lastconfig->{sourcefile}, "\n") if $opts{v};

        print STDERR "  $minfree $maxsize\n" if $opts{v};
        if ($minfree =~ m/(\d+)%$/) {
            $minfree = int($minforef->{size} * $1 / 100);
        }
        if ($maxsize =~ m/(\d+)%$/) {
            $maxsize = int($minforef->{volsize} * $1 / 100);
        }
        if ($increment =~ m/(\d+)%$/) {
            $increment = int($minforef->{size} * $1 / 100);
        }
        if (defined $lastconfig->{fstype} && "lvm" eq $lastconfig->{fstype}) {
            my $extents   = $lastconfig->{extents};
            my $extentsize= $lastconfig->{size} / $extents;
            if ($increment < $extentsize) {
                # Need to increase by at least one extent
                $increment = $extentsize;
            }
        }
        my $available = $minforef->{available};
        print STDERR "  $minfree>?$available $maxsize $increment\n" if $opts{v};
        if ($minfree > $available) {
            my $size = $minforef->{size};
            my $newsize = $size + $increment;
            $newsize = $maxsize if ($newsize > $maxsize);
            if ($newsize > $size) {
                $minforef->{newsize} = $newsize;
                print STDERR "  Need more than $available available, resizing to $newsize\n" if $opts{v};
            } else {
                # Upper limit is below the wanted size.  Not resizing
                $minforef->{newsize} = $minforef->{size};
            }
        } else {
            $minforef->{newsize} = $minforef->{size};
        }
    } else {
        print STDERR "info: unable to match $mountpoint in config file\n";
        $minforef->{newsize} = $minforef->{size};
    }
}

sub as_kilobyte {
    my $val = shift;
    $val = $1 * 1024 * 1024 * 1024 if ($val =~ m/^(\d+)t$/i);
    $val = $1 * 1024 * 1024 if ($val =~ m/^(\d+)g$/i);
    $val = $1 * 1024 if ($val =~ m/^(\d+)m$/i);
    return $val;
}

sub load_config {
    my @conffiles = @_;
    my @config;

    for my $file (@conffiles) {
        open(F, "<", $file) || next;
        while (<F>) {
            chomp;
            s/\#.*//;
            my ($regex, $minfree, $maxsize, $increment) = split(/\s+/);
            next unless $regex;
            $increment = "10%" if $increment eq "defaults";
            $minfree = as_kilobyte($minfree);
            $maxsize = as_kilobyte($maxsize);
            $increment = as_kilobyte($increment);
            my %fields =
                (
                 regex     => $regex,
                 minfree   => $minfree,
                 maxsize   => $maxsize,
                 increment => $increment,
                 sourcefile=> $file,
                 );
            push(@config, \%fields);
        }
        close F;
    }
    return @config;
}

sub fs_resize {
    my @config = @_;

    my %mountpoints = supported_mountpoints();

    for my $mountpoint (sort keys %mountpoints) {
        my %minfo = %{$mountpoints{$mountpoint}};

        calculate_resize(\%minfo, \@config);
        my $online_supported =
            $fsops{$minfo{fstype}}->{'online_supported'};
#        print(STDERR "S: $mountpoint ", $minfo{size}, " ",
#              $minfo{newsize}, "\n") if $opts{v};
        if ($minfo{size} != $minfo{newsize}) {
            print STDERR "info: trying to resize $mountpoint\n"
                if $opts{v};
            logmsg("overfull $mountpoint, resizing to $minfo{newsize}") if $opts{n};
            if (&$online_supported(%minfo)) {
                $fsops{$minfo{fstype}}->{'online_resize'}(%minfo);
            } else {
                print STDERR "warning: unable to resize $mountpoint, online resizing support is not detected\n";
            }
        }
    }
}

sub logmsg {
    my $msg = shift;
    openlog("debian-edu-fsautoresize", undef, 'user');
    syslog(LOG_NOTICE, "%s", $msg);
    closelog;
}

if ($opts{d}) { # Deamon mode, run in the background
    while (1) {
        my @config = load_config(@conffiles);
        fs_resize(@config);
        sleep 300;
    }
} else {
    my @config = load_config(@conffiles);
    fs_resize(@config);
}
exit 0;