⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fdiskbsdlabel.c

📁 Util-linux 软件包包含许多工具。其中比较重要的是加载、卸载、格式化、分区和管理硬盘驱动器
💻 C
📖 第 1 页 / 共 2 页
字号:
/*   NetBSD disklabel editor for Linux fdisk   Written by Bernhard Fastenrath (fasten@informatik.uni-bonn.de)   with code from the NetBSD disklabel command:     Copyright (c) 1987, 1988 Regents of the University of California.   All rights reserved.     Redistribution and use in source and binary forms, with or without   modification, are permitted provided that the following conditions   are met:   1. Redistributions of source code must retain the above copyright      notice, this list of conditions and the following disclaimer.   2. Redistributions in binary form must reproduce the above copyright      notice, this list of conditions and the following disclaimer in the      documentation and/or other materials provided with the distribution.   3. All advertising materials mentioning features or use of this software      must display the following acknowledgement:  	This product includes software developed by the University of  	California, Berkeley and its contributors.   4. Neither the name of the University nor the names of its contributors      may be used to endorse or promote products derived from this software      without specific prior written permission.     THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE   ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF   SUCH DAMAGE.   Changes:   19990319 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> - i18n/nls   20000101 - David Huggins-Daines <dhuggins@linuxcare.com> - Better   support for OSF/1 disklabels on Alpha.   Also fixed unaligned accesses in alpha_bootblock_checksum()*/#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <ctype.h>#include <setjmp.h>#include <errno.h>#include "nls.h"#include <sys/ioctl.h>#include <sys/param.h>#include "common.h"#include "fdisk.h"#define FREEBSD_PARTITION	0xa5#define NETBSD_PARTITION	0xa9#define DKTYPENAMES#include "fdiskbsdlabel.h"static void xbsd_delete_part (void);static void xbsd_new_part (void);static void xbsd_write_disklabel (void);static int xbsd_create_disklabel (void);static void xbsd_edit_disklabel (void);static void xbsd_write_bootstrap (void);static void xbsd_change_fstype (void);static int xbsd_get_part_index (int max);static int xbsd_check_new_partition (int *i);static void xbsd_list_types (void);static u_short xbsd_dkcksum (struct xbsd_disklabel *lp);static int xbsd_initlabel  (struct partition *p, struct xbsd_disklabel *d,			    int pindex);static int xbsd_readlabel  (struct partition *p, struct xbsd_disklabel *d);static int xbsd_writelabel (struct partition *p, struct xbsd_disklabel *d);static void sync_disks (void);#if defined (__alpha__)void alpha_bootblock_checksum (char *boot);#endif#if !defined (__alpha__)static int xbsd_translate_fstype (int linux_type);static void xbsd_link_part (void);static struct partition *xbsd_part;static int xbsd_part_index;#endif#if defined (__alpha__)/* We access this through a u_int64_t * when checksumming */static char disklabelbuffer[BSD_BBSIZE] __attribute__((aligned(8)));#elsestatic char disklabelbuffer[BSD_BBSIZE];#endifstatic struct xbsd_disklabel xbsd_dlabel;#define bsd_cround(n) \	(display_in_cyl_units ? ((n)/xbsd_dlabel.d_secpercyl) + 1 : (n))/* * Test whether the whole disk has BSD disk label magic. * * Note: often reformatting with DOS-type label leaves the BSD magic, * so this does not mean that there is a BSD disk label. */intcheck_osf_label(void) {	if (xbsd_readlabel (NULL, &xbsd_dlabel) == 0)		return 0;	return 1;}intbtrydev (char * dev) {	if (xbsd_readlabel (NULL, &xbsd_dlabel) == 0)		return -1;	printf(_("\nBSD label for device: %s\n"), dev);	xbsd_print_disklabel (0);	return 0;}static voidbmenu (void) {  puts (_("Command action"));  puts (_("   d   delete a BSD partition"));  puts (_("   e   edit drive data"));  puts (_("   i   install bootstrap"));  puts (_("   l   list known filesystem types"));  puts (_("   m   print this menu"));  puts (_("   n   add a new BSD partition"));  puts (_("   p   print BSD partition table"));  puts (_("   q   quit without saving changes"));  puts (_("   r   return to main menu"));  puts (_("   s   show complete disklabel"));  puts (_("   t   change a partition's filesystem id"));  puts (_("   u   change units (cylinders/sectors)"));  puts (_("   w   write disklabel to disk"));#if !defined (__alpha__)  puts (_("   x   link BSD partition to non-BSD partition"));#endif}#if !defined (__alpha__)static inthidden(int type) {	return type ^ 0x10;}static intis_bsd_partition_type(int type) {	return (type == FREEBSD_PARTITION ||		type == hidden(FREEBSD_PARTITION) ||		type == NETBSD_PARTITION ||		type == hidden(NETBSD_PARTITION));}#endifvoidbselect (void) {#if !defined (__alpha__)  int t, ss;  struct partition *p;  for (t=0; t<4; t++) {    p = get_part_table(t);    if (p && is_bsd_partition_type(p->sys_ind)) {      xbsd_part = p;      xbsd_part_index = t;      ss = get_start_sect(xbsd_part);      if (ss == 0) {	fprintf (stderr, _("Partition %s has invalid starting sector 0.\n"),		 partname(disk_device, t+1, 0));	return;      }      printf (_("Reading disklabel of %s at sector %d.\n"),	      partname(disk_device, t+1, 0), ss + BSD_LABELSECTOR);      if (xbsd_readlabel (xbsd_part, &xbsd_dlabel) == 0)	if (xbsd_create_disklabel () == 0)	  return;      break;    }  }  if (t == 4) {    printf (_("There is no *BSD partition on %s.\n"), disk_device);    return;  }#elif defined (__alpha__)  if (xbsd_readlabel (NULL, &xbsd_dlabel) == 0)    if (xbsd_create_disklabel () == 0)      exit ( EXIT_SUCCESS );#endif  while (1) {    putchar ('\n');    switch (tolower (read_char (_("BSD disklabel command (m for help): ")))) {      case 'd':	xbsd_delete_part ();	break;      case 'e':	xbsd_edit_disklabel ();	break;      case 'i':	xbsd_write_bootstrap ();	break;      case 'l':	xbsd_list_types ();	break;      case 'n':	xbsd_new_part ();	break;      case 'p':	xbsd_print_disklabel (0);	break;      case 'q':	close (fd);	exit ( EXIT_SUCCESS );      case 'r':	return;      case 's':	xbsd_print_disklabel (1);	break;      case 't':	xbsd_change_fstype ();	break;      case 'u':	change_units();	break;      case 'w':	xbsd_write_disklabel ();	break;#if !defined (__alpha__)      case 'x':	xbsd_link_part ();	break;#endif      default:	bmenu ();	break;    }  }}static voidxbsd_delete_part (void){  int i;  i = xbsd_get_part_index (xbsd_dlabel.d_npartitions);  xbsd_dlabel.d_partitions[i].p_size   = 0;  xbsd_dlabel.d_partitions[i].p_offset = 0;  xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;  if (xbsd_dlabel.d_npartitions == i + 1)    while (xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size == 0)      xbsd_dlabel.d_npartitions--;}static voidxbsd_new_part (void){  unsigned int begin, end;  char mesg[256];  int i;  if (!xbsd_check_new_partition (&i))    return;#if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__)  begin = get_start_sect(xbsd_part);  end = begin + get_nr_sects(xbsd_part) - 1;#else  begin = 0;  end = xbsd_dlabel.d_secperunit - 1;#endif  snprintf (mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));  begin = read_int (bsd_cround (begin), bsd_cround (begin), bsd_cround (end),		    0, mesg);  if (display_in_cyl_units)    begin = (begin - 1) * xbsd_dlabel.d_secpercyl;  snprintf (mesg, sizeof(mesg), _("Last %s or +size or +sizeM or +sizeK"),	   str_units(SINGULAR));  end = read_int (bsd_cround (begin), bsd_cround (end), bsd_cround (end),		  bsd_cround (begin), mesg);  if (display_in_cyl_units)    end = end * xbsd_dlabel.d_secpercyl - 1;  xbsd_dlabel.d_partitions[i].p_size   = end - begin + 1;  xbsd_dlabel.d_partitions[i].p_offset = begin;  xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;}voidxbsd_print_disklabel (int show_all) {  struct xbsd_disklabel *lp = &xbsd_dlabel;  struct xbsd_partition *pp;  FILE *f = stdout;  int i, j;  if (show_all) {#if defined (__alpha__)    fprintf(f, "# %s:\n", disk_device);#else    fprintf(f, "# %s:\n", partname(disk_device, xbsd_part_index+1, 0));#endif    if ((unsigned) lp->d_type < BSD_DKMAXTYPES)      fprintf(f, _("type: %s\n"), xbsd_dktypenames[lp->d_type]);    else      fprintf(f, _("type: %d\n"), lp->d_type);    fprintf(f, _("disk: %.*s\n"), (int) sizeof(lp->d_typename), lp->d_typename);    fprintf(f, _("label: %.*s\n"), (int) sizeof(lp->d_packname), lp->d_packname);    fprintf(f, _("flags:"));    if (lp->d_flags & BSD_D_REMOVABLE)      fprintf(f, _(" removable"));    if (lp->d_flags & BSD_D_ECC)      fprintf(f, _(" ecc"));    if (lp->d_flags & BSD_D_BADSECT)      fprintf(f, _(" badsect"));    fprintf(f, "\n");    /* On various machines the fields of *lp are short/int/long */    /* In order to avoid problems, we cast them all to long. */    fprintf(f, _("bytes/sector: %ld\n"), (long) lp->d_secsize);    fprintf(f, _("sectors/track: %ld\n"), (long) lp->d_nsectors);    fprintf(f, _("tracks/cylinder: %ld\n"), (long) lp->d_ntracks);    fprintf(f, _("sectors/cylinder: %ld\n"), (long) lp->d_secpercyl);    fprintf(f, _("cylinders: %ld\n"), (long) lp->d_ncylinders);    fprintf(f, _("rpm: %d\n"), lp->d_rpm);    fprintf(f, _("interleave: %d\n"), lp->d_interleave);    fprintf(f, _("trackskew: %d\n"), lp->d_trackskew);    fprintf(f, _("cylinderskew: %d\n"), lp->d_cylskew);    fprintf(f, _("headswitch: %ld\t\t# milliseconds\n"),	    (long) lp->d_headswitch);    fprintf(f, _("track-to-track seek: %ld\t# milliseconds\n"),	    (long) lp->d_trkseek);    fprintf(f, _("drivedata: "));    for (i = NDDATA - 1; i >= 0; i--)      if (lp->d_drivedata[i])	break;    if (i < 0)      i = 0;    for (j = 0; j <= i; j++)      fprintf(f, "%ld ", (long) lp->d_drivedata[j]);  }  fprintf (f, _("\n%d partitions:\n"), lp->d_npartitions);  fprintf (f, _("#       start       end      size     fstype   [fsize bsize   cpg]\n"));  pp = lp->d_partitions;  for (i = 0; i < lp->d_npartitions; i++, pp++) {    if (pp->p_size) {      if (display_in_cyl_units && lp->d_secpercyl) {	fprintf(f, "  %c: %8ld%c %8ld%c %8ld%c  ",		'a' + i,		(long) pp->p_offset / lp->d_secpercyl + 1,		(pp->p_offset % lp->d_secpercyl) ? '*' : ' ',		(long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1)			/ lp->d_secpercyl,		((pp->p_offset + pp->p_size) % lp->d_secpercyl) ? '*' : ' ',		(long) pp->p_size / lp->d_secpercyl,		(pp->p_size % lp->d_secpercyl) ? '*' : ' ');      } else {	fprintf(f, "  %c: %8ld  %8ld  %8ld   ",		'a' + i,		(long) pp->p_offset,		(long) pp->p_offset + pp->p_size - 1,		(long) pp->p_size);      }      if ((unsigned) pp->p_fstype < BSD_FSMAXTYPES)	fprintf(f, "%8.8s", xbsd_fstypes[pp->p_fstype].name);      else	fprintf(f, "%8x", pp->p_fstype);      switch (pp->p_fstype) {	case BSD_FS_UNUSED:	  fprintf(f, "    %5ld %5ld %5.5s ",		  (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, "");	  break;	  	case BSD_FS_BSDFFS:	  fprintf(f, "    %5ld %5ld %5d ",		  (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag,		  pp->p_cpg);	  break;	  	default:	  fprintf(f, "%22.22s", "");	  break;      }      fprintf(f, "\n");    }  }}static voidxbsd_write_disklabel (void) {#if defined (__alpha__)	printf (_("Writing disklabel to %s.\n"), disk_device);	xbsd_writelabel (NULL, &xbsd_dlabel);#else	printf (_("Writing disklabel to %s.\n"),		partname(disk_device, xbsd_part_index+1, 0));	xbsd_writelabel (xbsd_part, &xbsd_dlabel);#endif	reread_partition_table(0);	/* no exit yet */}static intxbsd_create_disklabel (void) {	char c;#if defined (__alpha__)	fprintf (stderr, _("%s contains no disklabel.\n"), disk_device);#else	fprintf (stderr, _("%s contains no disklabel.\n"),		 partname(disk_device, xbsd_part_index+1, 0));#endif	while (1) {		c = read_char (_("Do you want to create a disklabel? (y/n) "));		if (tolower(c) == 'y') {			if (xbsd_initlabel (#if defined (__alpha__) || defined (__powerpc__) || defined (__hppa__) || \    defined (__s390__) || defined (__s390x__)				NULL, &xbsd_dlabel, 0#else				xbsd_part, &xbsd_dlabel, xbsd_part_index

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -