admin scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.8 KiB

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. # Script for adding fast storage for a user
  5. # Last modified 9 Jan 2007, werdz@redbrick.dcu.ie
  6. #############################
  7. # Default settings
  8. #############################
  9. my $softlimit = 300000;
  10. my $hardlimit = 330000;
  11. my $softblocks = 800000;
  12. my $hardblocks = 1000000;
  13. #############################
  14. # General configuration
  15. my $faststorage_location = "/fast-storage/users";
  16. my $faststorage_device = "/dev/sda7";
  17. # Check for command line arguments
  18. usage() unless $ARGV[0];
  19. my $username = $ARGV[0];
  20. if($ARGV[1]) {
  21. $softlimit = $ARGV[1];
  22. $hardlimit = int($softlimit * 1.10);
  23. }
  24. if($ARGV[2]) {
  25. $softblocks = $ARGV[2];
  26. $hardblocks = int($softblocks * 1.25);
  27. }
  28. # Determine any extra information we'll need
  29. my $user_letter = lc(substr($username,0,1));
  30. my $groups_output = `id $username`;
  31. #print $groups_output;
  32. $groups_output =~ m/uid=\d*\(.*?\) gid=\d*\((.*?)\).*$/;
  33. my $usergroup = $1;
  34. # Check if the user already has fast storage space
  35. if( -e $faststorage_location . "/" . $user_letter . "/" . $username) {
  36. print "User " . $username . " already appears have a fast storage account at:\n";
  37. print $faststorage_location . "/" . $user_letter . "/" . $username . "\n\n";
  38. exit(1); # Unsuccessful error code
  39. }
  40. # Build commands
  41. my $mkdir_cmd = "mkdir -p " . $faststorage_location . "/" . $user_letter . "/" . $username;
  42. my $chmod_cmd = "chmod 700 " . $faststorage_location . "/" . $user_letter . "/" . $username;
  43. my $chown_cmd = "chown $username:$usergroup $faststorage_location/$user_letter/$username";
  44. my $quota_cmd = "setquota -u " . $username . " " . $softlimit . " " . $hardlimit . " " . $softblocks . " " . $hardblocks . " " . $faststorage_device;
  45. # Print out a summary of what will be done.
  46. print "User does not appear to have fast storage space.\n";
  47. print "Summary of what will be done:\n\n";
  48. print $mkdir_cmd . "\n" . $chmod_cmd . "\n" . $chown_cmd . "\n" . $quota_cmd . "\n\n";
  49. # Ask the user if this is acceptable.
  50. my $valid_answer = 0;
  51. my $user_answer;
  52. while(!$valid_answer) {
  53. print "Is this alright? (Y/N): ";
  54. $user_answer = lc(<STDIN>);
  55. chomp $user_answer;
  56. if($user_answer eq 'y' || $user_answer eq 'n') {
  57. $valid_answer = 1;
  58. }
  59. }
  60. # User says no.
  61. if($user_answer eq 'n') {
  62. print "Aborted by user.\n";
  63. exit(1);
  64. }
  65. # Approved by user.. continue.
  66. print "Creating directory...\n";
  67. `$mkdir_cmd`;
  68. print "Setting permissions...\n";
  69. `$chmod_cmd`;
  70. print "Setting ownership...\n";
  71. `$chown_cmd`;
  72. print "Setting quota...\n";
  73. `$quota_cmd`;
  74. print "Done.\n";
  75. exit(0);
  76. #####################################################
  77. #####################################################
  78. sub usage {
  79. my $usage = "Fast storage script\n";
  80. $usage .= "Usage: faststorage_add.pl username [custom quota size] [custom block limit]\n";
  81. print $usage;
  82. exit(1); # Unsuccessful error code
  83. }