1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * Just in case we're not in a build environment, make sure that 31*0Sstevel@tonic-gate * TEXT_DOMAIN gets set to something. 32*0Sstevel@tonic-gate */ 33*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 34*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 35*0Sstevel@tonic-gate #endif 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate /* 38*0Sstevel@tonic-gate * patch /etc/vfstab file 39*0Sstevel@tonic-gate */ 40*0Sstevel@tonic-gate #include <meta.h> 41*0Sstevel@tonic-gate #include <string.h> 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate /* 44*0Sstevel@tonic-gate * patch filesystem lines into vfstab file, return tempfilename 45*0Sstevel@tonic-gate */ 46*0Sstevel@tonic-gate int 47*0Sstevel@tonic-gate meta_patch_vfstab( 48*0Sstevel@tonic-gate char *cmpname, /* filesystem mount point or */ 49*0Sstevel@tonic-gate /* "swap" if updating swap partition */ 50*0Sstevel@tonic-gate mdname_t *fsnp, /* filesystem device name */ 51*0Sstevel@tonic-gate char *vname, /* vfstab file name */ 52*0Sstevel@tonic-gate char *old_bdevname, /* old name of block device, needed */ 53*0Sstevel@tonic-gate /* for deciding which of multiple */ 54*0Sstevel@tonic-gate /* swap file entries to change */ 55*0Sstevel@tonic-gate /* if NULL then not changing swap */ 56*0Sstevel@tonic-gate int doit, /* really patch file */ 57*0Sstevel@tonic-gate int verbose, /* show what we're doing */ 58*0Sstevel@tonic-gate char **tname, /* returned temp file name */ 59*0Sstevel@tonic-gate md_error_t *ep /* returned error */ 60*0Sstevel@tonic-gate ) 61*0Sstevel@tonic-gate { 62*0Sstevel@tonic-gate char *chrname = fsnp->rname; 63*0Sstevel@tonic-gate char *blkname = fsnp->bname; 64*0Sstevel@tonic-gate FILE *fp = NULL; 65*0Sstevel@tonic-gate FILE *tfp = NULL; 66*0Sstevel@tonic-gate struct stat sbuf; 67*0Sstevel@tonic-gate char buf[512]; 68*0Sstevel@tonic-gate char cdev[512]; 69*0Sstevel@tonic-gate char bdev[512]; 70*0Sstevel@tonic-gate char mntpt[512]; 71*0Sstevel@tonic-gate char fstype[512]; 72*0Sstevel@tonic-gate char fsckpass[512]; 73*0Sstevel@tonic-gate char mntboot[512]; 74*0Sstevel@tonic-gate char mntopt[512]; 75*0Sstevel@tonic-gate int gotfs = 0; 76*0Sstevel@tonic-gate char *cmpstr = &mntpt[0]; /* compare against mntpnt if fs, */ 77*0Sstevel@tonic-gate /* or fstype if swap */ 78*0Sstevel@tonic-gate char *char_device = chrname; 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate /* check names */ 81*0Sstevel@tonic-gate assert(vname != NULL); 82*0Sstevel@tonic-gate assert(tname != NULL); 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate /* get temp names */ 85*0Sstevel@tonic-gate *tname = NULL; 86*0Sstevel@tonic-gate *tname = Malloc(strlen(vname) + strlen(".tmp") + 1); 87*0Sstevel@tonic-gate (void) strcpy(*tname, vname); 88*0Sstevel@tonic-gate (void) strcat(*tname, ".tmp"); 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate /* check if going to update swap entry in file */ 91*0Sstevel@tonic-gate /* if so then compare against file system type */ 92*0Sstevel@tonic-gate if ((old_bdevname != NULL) && (strcmp("swap", cmpname) == 0)) { 93*0Sstevel@tonic-gate cmpstr = &fstype[0]; 94*0Sstevel@tonic-gate char_device = &cdev[0]; 95*0Sstevel@tonic-gate } 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate /* copy vfstab file, replace filesystem line */ 98*0Sstevel@tonic-gate if ((fp = fopen(vname, "r")) == NULL) { 99*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname); 100*0Sstevel@tonic-gate goto out; 101*0Sstevel@tonic-gate } 102*0Sstevel@tonic-gate if (fstat(fileno(fp), &sbuf) != 0) { 103*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname); 104*0Sstevel@tonic-gate goto out; 105*0Sstevel@tonic-gate } 106*0Sstevel@tonic-gate if (doit) { 107*0Sstevel@tonic-gate if ((tfp = fopen(*tname, "w")) == NULL) { 108*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname); 109*0Sstevel@tonic-gate goto out; 110*0Sstevel@tonic-gate } 111*0Sstevel@tonic-gate if (fchmod(fileno(tfp), (sbuf.st_mode & 0777)) != 0) { 112*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname); 113*0Sstevel@tonic-gate goto out; 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate if (fchown(fileno(tfp), sbuf.st_uid, sbuf.st_gid) != 0) { 116*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname); 117*0Sstevel@tonic-gate goto out; 118*0Sstevel@tonic-gate } 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) { 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate /* check that have all required params from vfstab file */ 123*0Sstevel@tonic-gate /* or that the line isnt a comment */ 124*0Sstevel@tonic-gate /* or that the fstype/mntpoint match what was passed in */ 125*0Sstevel@tonic-gate /* or that the block device matches if changing swap */ 126*0Sstevel@tonic-gate /* the last check is needed since there may be multiple */ 127*0Sstevel@tonic-gate /* entries of swap in the file, and so the fstype is not */ 128*0Sstevel@tonic-gate /* a sufficient check */ 129*0Sstevel@tonic-gate if ((sscanf(buf, "%512s %512s %512s %512s %512s %512s %512s", 130*0Sstevel@tonic-gate bdev, cdev, mntpt, fstype, fsckpass, 131*0Sstevel@tonic-gate mntboot, mntopt) != 7) || 132*0Sstevel@tonic-gate (bdev[0] == '#') || (strcmp(cmpstr, cmpname) != 0) || 133*0Sstevel@tonic-gate ((old_bdevname != NULL) && 134*0Sstevel@tonic-gate (strstr(bdev, old_bdevname) == NULL))) { 135*0Sstevel@tonic-gate if (doit) { 136*0Sstevel@tonic-gate if (fputs(buf, tfp) == EOF) { 137*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname); 138*0Sstevel@tonic-gate goto out; 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate } 141*0Sstevel@tonic-gate continue; 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate if (verbose) { 145*0Sstevel@tonic-gate (void) printf(dgettext(TEXT_DOMAIN, 146*0Sstevel@tonic-gate "Delete the following line from %s:\n\n"), 147*0Sstevel@tonic-gate vname); 148*0Sstevel@tonic-gate (void) printf("%s\n", buf); 149*0Sstevel@tonic-gate (void) printf( 150*0Sstevel@tonic-gate dgettext(TEXT_DOMAIN, 151*0Sstevel@tonic-gate "Add the following line to %s:\n\n"), 152*0Sstevel@tonic-gate vname); 153*0Sstevel@tonic-gate (void) printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n\n", 154*0Sstevel@tonic-gate blkname, char_device, mntpt, fstype, fsckpass, 155*0Sstevel@tonic-gate mntboot, mntopt); 156*0Sstevel@tonic-gate } 157*0Sstevel@tonic-gate if (doit) { 158*0Sstevel@tonic-gate if (fprintf(tfp, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", 159*0Sstevel@tonic-gate blkname, char_device, mntpt, fstype, fsckpass, 160*0Sstevel@tonic-gate mntboot, mntopt) == EOF) { 161*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname); 162*0Sstevel@tonic-gate goto out; 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate } 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate gotfs = 1; 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate if (! feof(fp)) { 170*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname); 171*0Sstevel@tonic-gate goto out; 172*0Sstevel@tonic-gate } 173*0Sstevel@tonic-gate if (! gotfs) { 174*0Sstevel@tonic-gate (void) mderror(ep, MDE_VFSTAB_FILE, vname); 175*0Sstevel@tonic-gate goto out; 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate if (fclose(fp) != 0) { 178*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname); 179*0Sstevel@tonic-gate goto out; 180*0Sstevel@tonic-gate } 181*0Sstevel@tonic-gate fp = NULL; 182*0Sstevel@tonic-gate if (doit) { 183*0Sstevel@tonic-gate if ((fflush(tfp) != 0) || 184*0Sstevel@tonic-gate (fsync(fileno(tfp)) != 0) || 185*0Sstevel@tonic-gate (fclose(tfp) != 0)) { 186*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname); 187*0Sstevel@tonic-gate goto out; 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate tfp = NULL; 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate /* return success */ 193*0Sstevel@tonic-gate return (0); 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate /* cleanup, return error */ 196*0Sstevel@tonic-gate out: 197*0Sstevel@tonic-gate if (fp != NULL) 198*0Sstevel@tonic-gate (void) fclose(fp); 199*0Sstevel@tonic-gate if (tfp != NULL) 200*0Sstevel@tonic-gate (void) fclose(tfp); 201*0Sstevel@tonic-gate if (*tname != NULL) { 202*0Sstevel@tonic-gate (void) unlink(*tname); 203*0Sstevel@tonic-gate Free(*tname); 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate return (-1); 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate /* 210*0Sstevel@tonic-gate * set filesystem device name in vfstab 211*0Sstevel@tonic-gate */ 212*0Sstevel@tonic-gate int 213*0Sstevel@tonic-gate meta_patch_fsdev( 214*0Sstevel@tonic-gate char *fsname, /* filesystem mount point */ 215*0Sstevel@tonic-gate mdname_t *fsnp, /* filesystem device */ 216*0Sstevel@tonic-gate char *vname, /* vfstab file name */ 217*0Sstevel@tonic-gate md_error_t *ep /* returned error */ 218*0Sstevel@tonic-gate ) 219*0Sstevel@tonic-gate { 220*0Sstevel@tonic-gate int doit = 1; 221*0Sstevel@tonic-gate int verbose = 0; 222*0Sstevel@tonic-gate char *tvname = NULL; 223*0Sstevel@tonic-gate int rval = -1; 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate /* check names */ 226*0Sstevel@tonic-gate assert(fsname != NULL); 227*0Sstevel@tonic-gate if (vname == NULL) 228*0Sstevel@tonic-gate vname = "/etc/vfstab"; 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate /* replace lines in vfstab */ 231*0Sstevel@tonic-gate if (meta_patch_vfstab(fsname, fsnp, vname, NULL, doit, verbose, &tvname, 232*0Sstevel@tonic-gate ep) != 0) { 233*0Sstevel@tonic-gate goto out; 234*0Sstevel@tonic-gate } 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate /* rename temp file on top of real one */ 237*0Sstevel@tonic-gate if (rename(tvname, vname) != 0) { 238*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname); 239*0Sstevel@tonic-gate goto out; 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate Free(tvname); 242*0Sstevel@tonic-gate tvname = NULL; 243*0Sstevel@tonic-gate rval = 0; 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate /* cleanup, return error */ 246*0Sstevel@tonic-gate out: 247*0Sstevel@tonic-gate if (tvname != NULL) { 248*0Sstevel@tonic-gate if (doit) 249*0Sstevel@tonic-gate (void) unlink(tvname); 250*0Sstevel@tonic-gate Free(tvname); 251*0Sstevel@tonic-gate } 252*0Sstevel@tonic-gate return (rval); 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate /* 257*0Sstevel@tonic-gate * set filesystem device name in vfstab 258*0Sstevel@tonic-gate */ 259*0Sstevel@tonic-gate int 260*0Sstevel@tonic-gate meta_patch_swapdev( 261*0Sstevel@tonic-gate mdname_t *fsnp, /* filesystem device */ 262*0Sstevel@tonic-gate char *vname, /* vfstab file name */ 263*0Sstevel@tonic-gate char *old_bdevname, /* block device name to change */ 264*0Sstevel@tonic-gate md_error_t *ep /* returned error */ 265*0Sstevel@tonic-gate ) 266*0Sstevel@tonic-gate { 267*0Sstevel@tonic-gate int doit = 1; 268*0Sstevel@tonic-gate int verbose = 0; 269*0Sstevel@tonic-gate char *tvname = NULL; 270*0Sstevel@tonic-gate int rval = -1; 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate /* check names */ 273*0Sstevel@tonic-gate if (vname == NULL) 274*0Sstevel@tonic-gate vname = "/etc/vfstab"; 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate /* replace lines in vfstab */ 277*0Sstevel@tonic-gate if (meta_patch_vfstab("swap", fsnp, vname, old_bdevname, doit, 278*0Sstevel@tonic-gate verbose, &tvname, ep) != 0) { 279*0Sstevel@tonic-gate goto out; 280*0Sstevel@tonic-gate } 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate /* rename temp file on top of real one */ 283*0Sstevel@tonic-gate if (rename(tvname, vname) != 0) { 284*0Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname); 285*0Sstevel@tonic-gate goto out; 286*0Sstevel@tonic-gate } 287*0Sstevel@tonic-gate Free(tvname); 288*0Sstevel@tonic-gate tvname = NULL; 289*0Sstevel@tonic-gate rval = 0; 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate /* cleanup, return error */ 292*0Sstevel@tonic-gate out: 293*0Sstevel@tonic-gate if (tvname != NULL) { 294*0Sstevel@tonic-gate if (doit) 295*0Sstevel@tonic-gate (void) unlink(tvname); 296*0Sstevel@tonic-gate Free(tvname); 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate return (rval); 299*0Sstevel@tonic-gate } 300