int writelabel(int f, char *boot, struct disklabel *lp) { #if NUMBOOT > 0 setbootflag(lp); #endif lp->d_magic = DISKMAGIC; lp->d_magic2 = DISKMAGIC; lp->d_checksum = 0; lp->d_checksum = dkcksum(lp); #if NUMBOOT > 0 if (installboot) { /* * First set the kernel disk label, * then write a label to the raw disk. * If the SDINFO ioctl fails because it is unimplemented, * keep going; otherwise, the kernel consistency checks * may prevent us from changing the current (in-core) * label. */ if (!donothing) { if (ioctl(f, DIOCSDINFO, lp) < 0 && errno != ENODEV && errno != ENOTTY) { l_perror("ioctl DIOCSDINFO"); return (1); } } if (!donothing) { if (lseek(f, 0, SEEK_SET) < 0) { perror("lseek"); return (1); } if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) { perror("write"); return (1); } } /* * Output the remainder of the disklabel */ if (!donothing && bootbuf && write(f, bootbuf, bootsize) != bootsize) { perror("write"); return(1); } } else #endif /* NUMBOOT > 0 */ if (!donothing) { if (ioctl(f, DIOCWDINFO, lp) < 0) { l_perror("ioctl DIOCWDINFO"); return (1); } } #ifdef __vax__ if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) { off_t alt; int i; alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors; for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) { (void)lseek(f, (alt + i) * lp->d_secsize, SEEK_SET); if (!donothing) if (write(f, boot, lp->d_secsize) != lp->d_secsize) warn("alternate label %d write", i/2); } } #endif /* Finally, write out any mount point information. */ if (!donothing) { /* First refresh our copy of the current label to get UID. */ if (ioctl(f, DIOCGDINFO, &lab) < 0) err(4, "ioctl DIOCGDINFO"); mpsave(lp); } return (0); }
int writelabel(int f, const char *boot, struct disklabel32 *lp) { const char *msg; int flag; int r; if (disable_write) { Warning("write to disk label suppressed - label was as follows:"); display(stdout, lp); return (0); } else { /* make sure we are not overwriting our boot code */ if (checkoldboot(f, boot)) errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!"); setbootflag(lp); lp->d_magic = DISKMAGIC32; lp->d_magic2 = DISKMAGIC32; lp->d_checksum = 0; lp->d_checksum = dkcksum32(lp); if (rflag) { /* * First set the kernel disk label, * then write a label to the raw disk. * If the SDINFO ioctl fails because it is unimplemented, * keep going; otherwise, the kernel consistency checks * may prevent us from changing the current (in-core) * label. */ if (ioctl(f, DIOCSDINFO32, lp) < 0 && errno != ENODEV && errno != ENOTTY) { l_perror("ioctl DIOCSDINFO32"); return (1); } lseek(f, (off_t)0, SEEK_SET); /* * write enable label sector before write * (if necessary), disable after writing. */ flag = 1; if (ioctl(f, DIOCWLABEL, &flag) < 0) warn("ioctl DIOCWLABEL"); msg = fixlabel(f, lp, 1); if (msg) { warn(msg); return (1); } r = write(f, boot, lp->d_bbsize); fixlabel(f, lp, 0); if (r != ((ssize_t)lp->d_bbsize)) { warn("write"); return (1); } #if NUMBOOT > 0 /* * Output the remainder of the disklabel */ if (bootbuf) { fixlabel(f, lp, 1); r = write(f, bootbuf, bootsize); fixlabel(f, lp, 0); if (r != bootsize) { warn("write"); return(1); } } #endif flag = 0; ioctl(f, DIOCWLABEL, &flag); } else if (ioctl(f, DIOCWDINFO32, lp) < 0) { l_perror("ioctl DIOCWDINFO32"); return (1); } } return (0); }