#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - scripts/fixquotas Copyright 2019 cPanel, L.L.C.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
package Scripts::FixQuotas;
use strict;
use Cwd ();
use IO::File ();
use Cpanel::FindBin ();
use Cpanel::Filesys::FindParse ();
use Cpanel::Filesys::Info ();
use Cpanel::SafeRun::Errors ();
use Cpanel::GenSysInfo ();
use Cpanel::Filesys::Root ();
use Cpanel::LoadFile ();
use Cpanel::Fcntl::Constants ();
use Cpanel::Transaction::File::Raw ();
use Cpanel::SafeDir::MK ();
# For testing purposes
our $PROC_MOUNTS = Cwd::realpath('/proc/mounts');
our $UDEV_RULES_DIR = "/etc/udev/rules.d";
our $UDEV_LINK_RULES_FILE = "$UDEV_RULES_DIR/99-root-link.rules";
our %cmd = (
'quotaon' => undef,
'quotaoff' => undef,
);
exit run(@ARGV) unless caller();
sub run {
return 0 if !verify_quota_binaries();
Cpanel::SafeRun::Errors::saferunnoerror( $cmd{'quotaoff'}, '-a' );
fix_broken_dev_root_links();
initialize_quotas();
Cpanel::SafeRun::Errors::saferunnoerror( $cmd{'quotaon'}, '-a' );
if ( grep { $_ eq '--onboot' } @_ ) {
require Cpanel::Notify;
Cpanel::Notify::notification_class( 'class' => 'Quota::SetupComplete', 'application' => 'Quota::SetupComplete', 'constructor_args' => [] );
}
return (0);
}
sub has_quota_support_for_xfs {
# currently only enable xfs quota support on CentOS 7
return Cpanel::GenSysInfo::get_rpm_distro_version() == 7;
}
sub get_xfs_mount_points_without_quota {
my @lines = grep { /\bxfs\b.*\bnoquota\b/ } Cpanel::SafeRun::Errors::saferunnoerror('/bin/mount');
my @partitions;
foreach my $line (@lines) {
push @partitions, $1 if $line =~ /^\S+\s+on\s+(\S+)/;
}
return @partitions;
}
# Find the XFS partitions with uquota (but not noquota) set in fstab.
sub get_xfs_mount_points_fstab_quota {
my @xfs = grep { $_->{'fstype'} eq 'xfs' } Cpanel::Filesys::FindParse::parse_fstab();
return map { $_->{mountpoint} } grep {
my $obj = $_;
my $opts = { map { $_ => 1 } @{ $obj->{'options'} } };
$opts->{'uquota'} && !$opts->{'noquota'};
} @xfs;
}
sub fix_broken_dev_root_links {
if ( !-e $PROC_MOUNTS || !-r $PROC_MOUNTS ) {
return 0;
}
my @lines = split qq{\n}, Cpanel::LoadFile::load($PROC_MOUNTS) or die "Unable to open $PROC_MOUNTS: $!";
#
# Danger: This code is targeted to fix systems that have
# /dev/root (AKA Cpanel::Filesys::Root::DEV_ROOT) in /proc/mounts
#
# If it is refactored to fix other system additional coverage will
# be needed esp for handling roots like /dev/mapper/Vol....
#
foreach my $line (@lines) {
my ( $device, $mount, $type, undef ) = split( /\s+/, $line, 4 );
if ( defined $device && $device eq $Cpanel::Filesys::Root::DEV_ROOT && !-e $Cpanel::Filesys::Root::DEV_ROOT ) {
my $actual_root_device_path = Cpanel::Filesys::Root::get_root_device_path();
#If $DEV_ROOT is a symlink that doesn’t resolve,
#whether it’s a dangling symlink,
#a symlink to a dangling symlink, or part of a symlink loop,
#then get rid of it.
if ( -l $Cpanel::Filesys::Root::DEV_ROOT && !-e $Cpanel::Filesys::Root::DEV_ROOT ) {
unlink $Cpanel::Filesys::Root::DEV_ROOT or warn "unlink($Cpanel::Filesys::Root::DEV_ROOT): $!";
}
symlink( $actual_root_device_path, $Cpanel::Filesys::Root::DEV_ROOT ) or warn "symlink($actual_root_device_path, $Cpanel::Filesys::Root::DEV_ROOT): $!";
Cpanel::SafeDir::MK::safemkdir( $UDEV_RULES_DIR, 0755 );
my ($device_name) = $actual_root_device_path =~ m{^/[^/]+/(.*)$}; # /dev/(XXXXX......)
my $trans_obj = Cpanel::Transaction::File::Raw->new( path => $UDEV_LINK_RULES_FILE, 'permissions' => 0644 );
my $contents_ref = $trans_obj->get_data();
my $new_line = qq{KERNEL == "$device_name", SUBSYSTEM == "block", SYMLINK += "root"};
# In the event /dev/root was a symlink to /dev/root we need to make sure
# we remove any circular lines
my $circular_bad_line = qq{echo ' KERNEL == "root", SUBSYSTEM == "block", SYMLINK += "root"'};
my $circular_bad_line_without_space = qq{echo 'KERNEL == "root", SUBSYSTEM == "block", SYMLINK += "root"'};
my @lines = grep {
$_ ne $new_line # Trying to be narrow to remove only things we have added
&& $_ ne $circular_bad_line # Trying to be narrow to remove only things we have added
&& $_ ne $circular_bad_line_without_space # Trying to be narrow to remove only things we have added
} split( m{\n}, $$contents_ref );
push @lines, $new_line;
my $new_contents = join( "\n", @lines ) . "\n";
$trans_obj->set_data( \$new_contents );
$trans_obj->save_and_close_or_die();
last;
}
}
return 1;
}
sub initialize_quotas { ##no critic (Subroutines::ProhibitExcessComplexity)
# need to init quotas before a boot on xfs, to avoid to reboot for each partitions
system '/usr/local/cpanel/scripts/initquotas';
my $filesys_ref = Cpanel::Filesys::Info::_all_filesystem_info();
my $slash_is_xfs = index( $filesys_ref->{'/'}{'fstype'}, 'xfs' ) > -1 ? 1 : 0;
if ( has_quota_support_for_xfs() ) {
my %xfs_without_quota = map { $_ => 1 } get_xfs_mount_points_without_quota();
my $xfs_partition_without_quota = %xfs_without_quota ? 1 : 0;
# do kernel mod? #
if ( $slash_is_xfs && $xfs_partition_without_quota && ( -f '/etc/grub2.cfg' || -f '/etc/grub2-efi.cfg' ) ) {
# at least one file system is XFS, so we'll need to enable quotas on the root file system before it's remounted by initrd and have the #
# user reboot to activate this change. it's not enough to remount the root filesystem, or any other, it must be completely remounted! #
my $grub_conf;
{
local $/ = undef;
open my $grub_fh, '<', '/etc/default/grub'
or die "The system failed to open the /etc/default/grub file: $!";
$grub_conf = <$grub_fh>;
close $grub_fh;
}
my $grub_conf_has_quota = $grub_conf =~ m/GRUB_CMDLINE_LINUX.+?rootflags.+?u(sr)?quota/ ? 1 : 0;
my $is_cloudlinux = Cpanel::GenSysInfo::get_rpm_distro() eq 'cloudlinux';
my $grub_cloudlinux_has_quota = 1;
my $grub2_cfg = find_grub2_cfg_file();
# running /usr/sbin/grub2-mkconfig to update /boot/grub2/grub.cfg
# will not preserve the linux kernel, let's patch it manually
# we need to adjust the flags each time cloudlinux update the menuentry
if ($is_cloudlinux) {
print "CloudLinux system detected: adding/checking 'rootflags=uquota' to $grub2_cfg\n";
# Don't use safelock or transactions here, as those use link(2),
# and we may be on a vfat filesystem (for EFI) which doesn't
# support link(2).
my $fh = IO::File->new( $grub2_cfg, '+<' );
die "Could not open '$grub2_cfg' file: $!" unless flock( $fh, $Cpanel::Fcntl::Constants::LOCK_EX );
my @lines;
my $in_cl_entry;
# add rootflags=uquota to cloudlinux entries
while ( my $line = readline $fh ) {
# begin of cloudlinux menuentry
if ( !$in_cl_entry && $line =~ qr{^\s*menuentry 'CloudLinux\b}i ) {
$in_cl_entry = 1;
}
# end of cloudlinux menuentry
if ( $in_cl_entry && $line !~ qr{^#} && $line =~ qr/}/ ) {
$in_cl_entry = 0;
}
# manually add the rootflags to the cloudlinux entries
# we can consider to also add them to other entries
if ( $in_cl_entry
&& $line =~ qr{^\s*linux([0-9]+|efi) (/boot)?/vmlinuz}i
&& $line !~ qr{rootflags.+?u(sr)?quota}i ) {
chomp $line;
$line .= qq{ rootflags=uquota\n};
$grub_cloudlinux_has_quota = 0;
}
push @lines, $line;
}
if ( $grub_cloudlinux_has_quota == 0 ) {
seek $fh, 0, 0;
print {$fh} join( '', @lines );
truncate( $fh, tell $fh );
}
flock( $fh, $Cpanel::Fcntl::Constants::LOCK_UN );
undef $fh;
}
# only need to adjust the grub2 configuration file if / is an xfs partition
# we need to reboot in all cases when enabling quota on xfs
if ( !$grub_conf_has_quota || ( $is_cloudlinux && !$grub_cloudlinux_has_quota ) ) {
if ( !$grub_conf_has_quota ) {
# we need to modify /etc/default/grub to add user quotas, then re-generate the grub.cfg in /boot and finally reboot the system #
print qq{Modifying the /etc/default/grub file to enable user quotas...\n};
$grub_conf =~ s/GRUB_CMDLINE_LINUX="(.+?)"/GRUB_CMDLINE_LINUX="$1 rootflags=uquota"/m;
die qq{You must manually add or update "rootflags=uquota" to "GRUB_CMDLINE_LINUX" in the /etc/default/grub file, and re-run this tool.\n}
if $grub_conf !~ m/GRUB_CMDLINE_LINUX.+?rootflags.+?u(sr)?quota/;
open my $grubw_fh, '>', '/etc/default/grub'
or die "failed to open /etc/default/grub for writing: $!";
print {$grubw_fh} $grub_conf;
close $grubw_fh;
if ( !$is_cloudlinux ) {
print qq{Running the "grub2-mkconfig" command to regenerate the system's boot configuration...\n};
die "/boot is not mounted. Mount /boot and then re-run this tool.\n"
if !-f $grub2_cfg;
system qw{ /usr/sbin/grub2-mkconfig -o }, $grub2_cfg;
die qq{The system failed to run the "/usr/sbin/grub2-mkconfig -o $grub2_cfg" command. Correct any issues, run the command manually, and then re-run this tool.\n}
if $?;
}
}
system( '/bin/touch', '/var/cpanel/reboot_required_for_quota' );
require Cpanel::Notify;
require Cpanel::ServerTasks;
Cpanel::Notify::notification_class(
'class' => 'Quota::RebootRequired',
'application' => 'Quota::RebootRequired',
'constructor_args' => []
);
Cpanel::ServerTasks::schedule_task( ['SystemTasks'], 5, "recache_system_reboot_data" );
# the script must quit at this time and ask the user to reboot to enable quotas #
die "\nThe '/' partition uses the XFS® filesystem. You must reboot the server to enable quotas.\n";
}
}
# If we have entries with quota enabled in fstab that aren't currently using
# quota, then we need to reboot.
if ( grep { $xfs_without_quota{$_} } get_xfs_mount_points_fstab_quota() ) {
system( '/bin/touch', '/var/cpanel/reboot_required_for_quota' );
require Cpanel::Notify;
require Cpanel::ServerTasks;
Cpanel::Notify::notification_class(
'class' => 'Quota::RebootRequired',
'application' => 'Quota::RebootRequired',
'constructor_args' => []
);
Cpanel::ServerTasks::schedule_task( ['SystemTasks'], 5, "recache_system_reboot_data" );
die "\nYou must reboot the server to enable XFS® filesystem quotas.\n";
}
}
system '/usr/local/cpanel/scripts/resetquotas';
return;
}
sub verify_quota_binaries {
my @missing_cmds;
foreach my $cmd_name ( keys %cmd ) {
$cmd{$cmd_name} = Cpanel::FindBin::findbin($cmd_name);
if ( !defined $cmd{$cmd_name} || !-e $cmd{$cmd_name} || !-x $cmd{$cmd_name} ) {
push @missing_cmds, $cmd_name;
}
}
if ( scalar @missing_cmds ) {
print "Incomplete quota kit: unable to fix quotas.\n";
print 'Missing commands: ', join( ', ', @missing_cmds ), "\n";
return 0;
}
return 1;
}
sub find_grub2_cfg_file {
my @files = qw{
/boot/efi/EFI/centos/grub.cfg
/boot/grub2/grub.cfg
};
foreach my $file (@files) {
return $file if -f $file;
}
return $files[-1];
}
1;
Copyright 2K16 - 2K18 Indonesian Hacker Rulez