SUMMARY: How can I setup diskquota from ascript

From: Pulak Rakshit <pulakr_at_cableol.net>
Date: Tue, 6 Feb 1996 09:48:27 GMT

I had two responses,

        Eric Bennett <bennett_at_hpel.cees.edu>, wrote:

I have had some success on other systems with the atrocious hack of
setting EDITOR to equal the script itself, then calling edquota.
The script's second invocation edits the tmp file as needed, then exits.




*********************************************************
    Richard Owain Hughes <s138_at_cpcroh.cpc.uea.ac.uk>, wrote:

If you're interested, I have written a C program to do just this. I
did write a version as a script but it was fairly unpleasant!
I've included the C code below, if it will help. You may have to tweak
it for your local setup but it runs okay under our OSF/1 3.0 system.

The program has two modes. One is to show the current quota, the other is
to set it. To set the quota, call the program with

setquota -s username path quota

Cheers,
Richard

Content-Type: text/plain; charset="us-ascii"
Content-ID: <27741.823286534.2_at_cpcroh.cpc.uea.ac.uk>
Content-Description: setquota.c

#include <stdio.h>
#include <ufs/quota.h>
#include <errno.h>
#include <pwd.h>

#define LEN 1024

int quotactl(char *path, int cmd, int id, char *addr);
char *status(int s, int t);
int setq(int uid, char *p, char *h, char *uname, int quota);

main (int argc, char *argv[])
{
        struct dqblk *disk;
        struct passwd *pw;
        char path[LEN], host[LEN];
        int cmd, id, quota;

        disk = (struct dqblk *) malloc (sizeof (struct dqblk));
        pw = (struct passwd *) malloc (sizeof (struct passwd));
        gethostname(host, LEN);


        sync (argv[3]);
        switch (argc) {
            case 4:
                if (strcmp (argv[1], "-p") == 0) {
                    if ((pw = getpwnam(argv[2])) == NULL) {
                        fprintf (stderr, "Username %s does not exist.\n",
argv[2]);
                        exit (1);
                    }
                    exit (printq(pw->pw_uid, argv[3], host, argv[2]));
                 }
                 else {
                    fprintf (stderr, "Useage: %s [-s] [-p] username path
[quota]\n", argv[0]);
                    exit (1);
                 }
                 break;
            case 5:
                 if (strcmp (argv[1], "-s") == 0) {
                     if ((pw = getpwnam(argv[2])) == NULL) {
                        fprintf (stderr, "Username %s does not exist.\n", argv[2
]);
                        exit (1);
                    }
                    exit (setq(pw->pw_uid, argv[3], host, argv[2],
(atoi(argv[4])*2)));
                 }
                 else {
                    fprintf (stderr, "Useage: %s [-s] [-p] username path [quota]
\n", argv[0]);
                    exit (1);
                 }
                 break;
            default:
                fprintf (stderr, "Useage: %s [-s] [-p] username path
[quota]\n", argv[0]);
                exit(1);
                break;
             }
}
int sync(char *p)
{
    int cmd=QCMD(Q_SYNC, USRQUOTA);

    return quotactl(p, cmd, 0, NULL);
}

int printq(int uid, char *p, char *h, char *uname)
{
    int cmd=QCMD(Q_GETQUOTA, USRQUOTA), quota, stat;
    struct dqblk *disk;
    disk = (struct dqblk *) malloc (sizeof (struct dqblk));

    if ((stat = quotactl(p, cmd, uid, disk)) != 0) {
        fprintf (stderr, "Can't get quota for userid %d.\n", uid);
        perror (strerror(errno));
        fprintf (stderr, "%d %s \n", errno, p);
        return stat;
    }
    else {
        printf ("User:%s\t\t\tHost:%s\t\tFilesys:%s\n\n", uname, h, p);
        printf ("Quota:%d Kilobytes\t\tUseage:%d Kilobytes\t\tStatus:%s\n",
disk->dqb_bsoftlimit/2, disk->dqb_curblocks/2, status (disk->dqb_bsoftlimit,
disk->dqb_curblocks));
        return 0;
     }
}
int setq(int uid, char *p, char *h, char *uname, int quota)
{
    int cmd=QCMD(Q_SETQUOTA, USRQUOTA), stat;
    struct dqblk *disk;
    disk = (struct dqblk *) malloc (sizeof (struct dqblk));
    if ((stat = quotactl(p, QCMD(Q_GETQUOTA, USRQUOTA), uid, disk)) != 0) {
        fprintf (stderr, "Can't get quota for userid %d.\n", uid);
        return stat;
    }
    else {
        disk->dqb_bsoftlimit = quota;
        disk->dqb_bhardlimit = quota + (quota /2);
        disk->dqb_isoftlimit = quota / 2;
        disk->dqb_ihardlimit = disk->dqb_isoftlimit + (disk->dqb_isoftlimit /2);
        if ((stat = quotactl(p, cmd, uid, disk)) != 0) {
            fprintf (stderr, "Can't set quota for userid %d.\n", uid);
            return stat;
        }
    }
    return 0;
}

char *status(int s, int t)
{
    if (s > t)
        return "IN CREDIT";
    else
        return "OVERDRAWN";
}
Received on Tue Feb 06 1996 - 11:29:27 NZDT

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:46 NZDT