| 1 | #!@PERL@ |
|---|
| 2 | use strict; |
|---|
| 3 | |
|---|
| 4 | # should be running as root |
|---|
| 5 | if($> != 0) |
|---|
| 6 | { |
|---|
| 7 | printf "\nWARNING: this should be run as root\n\n" |
|---|
| 8 | } |
|---|
| 9 | |
|---|
| 10 | # check and get command line parameters |
|---|
| 11 | if($#ARGV != 4 && $#ARGV != 2) |
|---|
| 12 | { |
|---|
| 13 | print <<__E; |
|---|
| 14 | |
|---|
| 15 | Setup raidfile config utility. |
|---|
| 16 | |
|---|
| 17 | Bad command line parameters. |
|---|
| 18 | Usage: |
|---|
| 19 | raidfile-config config-dir block-size dir0 [dir1 dir2] |
|---|
| 20 | |
|---|
| 21 | Parameters: |
|---|
| 22 | config-dir is usually @sysconfdir_expanded@/boxbackup |
|---|
| 23 | block-size must be a power of two, and usually the block or |
|---|
| 24 | fragment size of your file system |
|---|
| 25 | dir0, dir1, dir2 are the directories used as the root of the raid |
|---|
| 26 | file system |
|---|
| 27 | |
|---|
| 28 | If only one directory is specified, then userland RAID is disabled. |
|---|
| 29 | Specifying three directories enables it. |
|---|
| 30 | |
|---|
| 31 | __E |
|---|
| 32 | exit(1); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | my ($config_dir,$block_size,@dirs) = @ARGV; |
|---|
| 36 | |
|---|
| 37 | my $conf = $config_dir . '/raidfile.conf'; |
|---|
| 38 | |
|---|
| 39 | # check dirs are unique, and exist |
|---|
| 40 | my %d; |
|---|
| 41 | for(@dirs) |
|---|
| 42 | { |
|---|
| 43 | die "$_ is used twice" if exists $d{$_}; |
|---|
| 44 | die "$_ is not a directory" unless -d $_; |
|---|
| 45 | die "$_ should be an absolute path" unless m/\A\//; |
|---|
| 46 | $d{$_} = 1; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | # check block size is OK |
|---|
| 50 | $block_size = int($block_size); |
|---|
| 51 | die "Bad block size" if $block_size <= 0; |
|---|
| 52 | my $c = 1; |
|---|
| 53 | while(1) |
|---|
| 54 | { |
|---|
| 55 | last if $c == $block_size; |
|---|
| 56 | die "Block size $block_size is not a power of two" if $c > $block_size; |
|---|
| 57 | $c = $c * 2; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | # check that it doesn't already exist |
|---|
| 61 | if(-f $conf) |
|---|
| 62 | { |
|---|
| 63 | die "$conf already exists. Delete and try again"; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | # create directory |
|---|
| 67 | if(!-d $config_dir) |
|---|
| 68 | { |
|---|
| 69 | print "Creating $config_dir...\n"; |
|---|
| 70 | mkdir $config_dir,0755 or die "Can't create $config_dir"; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | # adjust if userland RAID is disabled |
|---|
| 74 | if($#dirs == 0) |
|---|
| 75 | { |
|---|
| 76 | $dirs[1] = $dirs[0]; |
|---|
| 77 | $dirs[2] = $dirs[0]; |
|---|
| 78 | print "WARNING: userland RAID is disabled.\n"; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | # write the file |
|---|
| 82 | open CONFIG,">$conf" or die "Can't open $conf for writing"; |
|---|
| 83 | |
|---|
| 84 | print CONFIG <<__E; |
|---|
| 85 | |
|---|
| 86 | disc0 |
|---|
| 87 | { |
|---|
| 88 | SetNumber = 0 |
|---|
| 89 | BlockSize = $block_size |
|---|
| 90 | Dir0 = $dirs[0] |
|---|
| 91 | Dir1 = $dirs[1] |
|---|
| 92 | Dir2 = $dirs[2] |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | __E |
|---|
| 96 | |
|---|
| 97 | close CONFIG; |
|---|
| 98 | |
|---|
| 99 | print "Config file written.\n"; |
|---|
| 100 | |
|---|