Edit File: fixquotas
#!/usr/local/cpanel/3rdparty/bin/perl # cpanel - fixquotas Copyright 2016 cPanel, Inc. # 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 IPC::Open2 (); use IO::Handle (); use Cpanel::FindBin (); use Cpanel::Filesys::Info (); use Cpanel::SafeRun::Errors (); use Cpanel::GenSysInfo (); use Cpanel::SafeFile (); use Cpanel::Filesys::Root (); use Cpanel::LoadFile (); 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"; my $slash_is_xfs = 0; 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(); install_default_quota_databases(); 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 zinstall { my ( $file, $dest ) = @_; open( RFILE, $file ); binmode RFILE; my $pid = IPC::Open2::open2( \*GZIPR, \*GZIPW, "gzip", "-df" ); binmode GZIPW; binmode GZIPR; while (<RFILE>) { print GZIPW $_; } close(RFILE); close(GZIPW); open( DEST, ">", $dest ); binmode DEST; while (<GZIPR>) { print DEST $_; } close(DEST); waitpid $pid, 0; print "..${dest}.."; } 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::Info::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 install_default_quota_databases { # the loop below that reads /etc/fstab may take a while during each line read, thus we copy the file # # so that we can safely read the file without someone writing to the file while we're reading it # system 'cp -f /etc/fstab /etc/fstab.quotas'; $| = 1; print "Installing Default Quota Databases..."; if ( open FTQ, '/etc/fstab.quotas' ) { while (<FTQ>) { my $line = $_; next if $line =~ m{^#}; # skip comments if ( $line =~ /ext[234]|reiserfs/ ) { if ( $line =~ /usrquota/ ) { if ( $line =~ /^\S+\s*(\S+)/ ) { my $mntpoint = $1; $mntpoint =~ s/\/$//g; unlink "${mntpoint}/aquota.user.new"; unlink "${mntpoint}/quota.user.new"; zinstall( "/usr/local/cpanel/scripts/aquota.user_emptyfs.gz", "$mntpoint/aquota.user" ); zinstall( "/usr/local/cpanel/scripts/quota.user_emptyfs.gz", "$mntpoint/quota.user" ); } } } elsif ( has_quota_support_for_xfs() && $line =~ qr{\bxfs\b} ) { $slash_is_xfs = 1 if $line =~ m{\s/\s}; } } close FTQ; print "...Done\n"; } else { print "\nThe scan was unable to determine which filesystems have quotas enabled.\n"; return 1; } return; } 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'; 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"; my $fh = IO::Handle->new(); my $lock = Cpanel::SafeFile::safeopen( $fh, '+<', $grub2_cfg ); die "Could not open '$grub2_cfg' file: $!" unless $lock; 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]+ (/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 ); } Cpanel::SafeFile::safeclose( $fh, $lock ); } # 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;