[net.sources] CHSIZE -- Change Size of Filesystem

cliff@hou2d.UUCP (#C.SHEDD) (01/02/85)

#!/bin/sh-----cut here-----cut here-----cut here-----cut here-----
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	chsize.c
#	chsize.1m
cat - << \SHAR_EOF > chsize.1m
.UC
.TH CHSIZE 1 "HO Local"
.deTH
.PD
.nrIN \\n()Mu
.ift .ds ]H \\$1\^(\^\\$2\^)
.ifn .ds ]H \\$1(\\$2)
.if\\n()s .ds ]D
.if\\n()t .ds ]D UNIX System V
.ifn .ds ]D UNIX System V
.ds]L
.if!\\$3 .ds ]L (\^\\$3\^)
.if!\\$4 .ds ]D \\$4
.wh0 }H
.wh-\\n(:mu }F
.em}M
.if\\n(nl .bp
.nr)I \\n()Mu
.nr)R 0
.}E
.DT
.ifn \{.na
.nh\}
.ift \{.bd S 3 3
.hy14 \}
..
.tr ~"
.if t .ds i \(fm\(fm
.if n .ds i ""
.UC
.SH NAME
chsize \- change size of file system
.SH SYNOPSIS
.B chsize
[
.B \-f
] /dev/rdsknnn new-size
.SH DESCRIPTION
The
.I chsize
utility allows super-users to change the number of blocks allocated
to a file system.
.P
The command line must contain the file system's character (a.k.a. "raw")
i/o device name.
.P
The arguement
.B new-size
must also be present.  It is the number of 512-byte blocks to be allocated
to the file system.  
.P
If the 
.B new-size
arguement is less than the current size, the
.B \-f
(force change) option must be present.
.P
To affect the change in the file system size, 
.I fsck
must be invoked to rebuild the free list.
.SH EXAMPLE
The file system "/dev/rdsk005" currently has 24500 blocks.  The user wishes
to add another 500 blocks.
.ti +.5i
chsize /dev/rdsk005 25000
fsck -f -s -y /dev/rdsk005
.P
The file system "/dev/rdsk999" currently has 30000 blocks.  The user wishes
to reduce its size by 15000 blocks.
.ti +.5i
chsize /dev/rdsk999 15000
fsck -f -s -y /dev/rdsk999
.SH "SEE ALSO"
fsck( 1m)
.SH "DON'T BOTHER"
dcopy( 1m), volcopy( 1m)
.SH AUTHOR
C.J. Shedd of the HOCC UNIX Support Staff came up with this facility.
He can
be contacted at HO 3D-219, extension 3856, or through electronic mail to
houxm!cliff. 
.SH SUPPORT
The author is the sole source of support for this facility.
SHAR_EOF
cat - << \SHAR_EOF > chsize.c
/*	c h s i z e
	change size of file system

	this program allows the super user to change the number of blocks
	allocated to a file system.

        @ (INSLOC) etc 700 root root

*/

#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/filsys.h>

	static char *Sccsid	= "@(#)chsize.c	v1.0	autumn 1984 c.j. shedd";
	static char *Sccsid1	= "@(#)	original release";

struct { 
	char	fill1[BSIZE];
	union { 
		char	fill2[BSIZE];
		struct filsys fs;
	} f;
} super;

main( argc, argv)
int	argc; 
char	*argv[];

{ 
	int	fsi, fso, i, new_size, old_size, okay_to_use_force;
	char	*myname, *fsarg, *nsize;

	myname = argv[0];

	if ((strcmp(argv[1], "-f") == 0)) {
		okay_to_use_force++;
		fsarg = argv[2];
		nsize = argv[3];
	} else {
		okay_to_use_force = 0;
		fsarg = argv[1];
		nsize = argv[2];
	}
	if ((argc - okay_to_use_force) != 3) {
		fprintf(stderr, "usage: %s [-f] /dev/rdsknnn new-size \n", myname);
		exit(2);
	}
	if ((fsi = open( fsarg, 0)) < 1) {
		fprintf(stderr, "%s: failed to open device %s \n", myname, fsarg);
		exit(2);
	}
	if ((i = read(fsi, &super, sizeof(super))) != sizeof( super)) {
		fprintf( stderr, "%s: cannot read superblock of %s \n", myname, fsarg);
		exit(2);
	}
	new_size = atoi(nsize);

#define S super.f.fs

	if (S.s_magic == FsMAGIC && S.s_type == Fs2b)
		old_size = S.s_fsize * 2;
	else
		old_size = S.s_fsize;

	if ( new_size < old_size && !okay_to_use_force) {
		fprintf(stderr, "%s: no permission to use force -- aborting \n", myname);
		exit(2);
	}

	printf("%s: file system %.6s of pack %.6s currently has %ld 512-byte blocks \n", 
	    myname, S.s_fname, S.s_fpack, old_size);

	printf("%s: in ten seconds it will be changed to %ld 512-byte blocks \n",
	    myname, new_size);
	printf("%s: use INTR or QUIT to abort request \n", myname);

	sleep( 10);

	if (S.s_magic == FsMAGIC && S.s_type == Fs2b)
		S.s_fsize = new_size / 2;
	else
		S.s_fsize = new_size;

	fso = open(fsarg, 1);

	if (write(fso, &super, sizeof(super))  < 0) {
		fprintf( stderr, "%s: cannot update superblock \n", myname);
		exit(2);
	}

	printf( "%s: immediately invoke fsck to affect change \07\07\n", myname);
	exit(0);
}


SHAR_EOF