4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2012 Milan Jurik. All rights reserved.
24 * Copyright 2014 Toomas Soome <tsoome@me.com>
25 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
30 * This file contains functions that implement the command menu commands.
36 #include <sys/resource.h>
45 #endif /* defined(sparc) */
50 #include "menu_command.h"
51 #include "menu_defect.h"
52 #include "menu_partition.h"
57 #include "partition.h"
61 #include "ctlr_scsi.h"
62 #include "auto_sense.h"
63 #include "modify_partition.h"
66 extern struct menu_item menu_partition
[];
67 extern struct menu_item menu_analyze
[];
68 extern struct menu_item menu_defect
[];
71 * Choices for the p_tag vtoc field
73 slist_t ptag_choices
[] = {
74 { "unassigned", "", V_UNASSIGNED
},
75 { "boot", "", V_BOOT
},
76 { "root", "", V_ROOT
},
77 { "swap", "", V_SWAP
},
79 { "backup", "", V_BACKUP
},
80 { "stand", "", V_STAND
},
82 { "home", "", V_HOME
},
83 { "alternates", "", V_ALTSCTR
},
84 { "reserved", "", V_RESERVED
},
85 { "system", "", V_SYSTEM
},
86 { "BIOS_boot", "", V_BIOS_BOOT
},
92 * Choices for the p_flag vtoc field
94 slist_t pflag_choices
[] = {
95 { "wm", "read-write, mountable", 0 },
96 { "wu", "read-write, unmountable", V_UNMNT
},
97 { "rm", "read-only, mountable", V_RONLY
},
98 { "ru", "read-only, unmountable", V_RONLY
|V_UNMNT
},
104 * This routine implements the 'disk' command. It allows the user to
105 * select a disk to be current. The list of choices is the list of
106 * disks that were found at startup time.
111 struct disk_info
*disk
;
115 int blind_select
= 0;
118 int *defltptr
= NULL
;
125 * This buffer holds the check() prompt that verifies we've got the right
126 * disk when performing a blind selection. The size should be sufficient
127 * to hold the prompt string, plus 256 characters for the disk name -
128 * way more than should ever be necessary. See the #define in misc.h.
130 char chk_buf
[BLIND_SELECT_VER_PROMPT
];
132 if (istokenpresent()) {
134 * disk number to be selected is already in the
137 TOKEN token
, cleantoken
;
140 * Get the disk number the user has given.
143 for (disk
= disk_list
; disk
!= NULL
; disk
= disk
->disk_next
) {
147 ioparam
.io_bounds
.lower
= 0;
148 ioparam
.io_bounds
.upper
= i
- 1;
149 (void) gettoken(token
);
150 clean_token(cleantoken
, token
);
153 * Convert the token into an integer.
155 if (geti(cleantoken
, &index
, (int *)NULL
))
159 * Check to be sure it is within the legal bounds.
161 if ((index
< 0) || (index
>= i
)) {
162 err_print("`%d' is out of range.\n", index
);
168 fmt_print("\n\nAVAILABLE DISK SELECTIONS:\n");
171 if ((option_f
== (char *)NULL
) && isatty(0) == 1 && isatty(1) == 1) {
173 * We have a real terminal for std input and output, enable
174 * more style of output for disk selection list.
177 tty_lines
= get_tty_lines();
185 * Loop through the list of found disks.
187 for (disk
= disk_list
; disk
!= NULL
; disk
= disk
->disk_next
) {
189 * If using more output, account 2 lines for each disk.
191 if (more
&& !more_quit
&& i
&& (one_line
||
192 ((2 * i
+ 1) % (tty_lines
- 2) <= 1))) {
196 * Get the next character.
198 fmt_print("- hit space for more or s to select - ");
203 * Handle display one line command
209 /* Handle Quit command */
215 /* Handle ^D command */
218 /* or get on with the show */
219 if (c
== 's' || c
== 'S') {
220 fmt_print("%80s\n", " ");
225 * If this is the current disk, mark it as
228 if (cur_disk
== disk
) {
232 if (!more
|| !more_quit
)
233 pr_diskline(disk
, i
);
244 * Determine total number of disks, and ask the user which disk he
245 * would like to make current.
248 for (disk
= disk_list
; disk
!= NULL
; disk
= disk
->disk_next
) {
252 ioparam
.io_bounds
.lower
= 0;
253 ioparam
.io_bounds
.upper
= ndisks
- 1;
254 index
= input(FIO_INT
, "Specify disk (enter its number)", ':',
255 &ioparam
, defltptr
, DATA_INPUT
);
262 * Find the disk chosen. Search through controllers/disks
263 * in the same original order, so we match what the user
268 for (disk
= disk_list
; disk
!= NULL
; disk
= disk
->disk_next
) {
274 * Should never happen.
276 impossible("no disk found");
280 (void) snprintf(chk_buf
, sizeof (chk_buf
),
281 "Disk %s selected - is this the desired disk? ", disk
->disk_name
);
282 if (check(chk_buf
)) {
288 * Update the state. We lock out interrupts so the state can't
297 * If type unknown and interactive, ask user to specify type.
298 * Also, set partition table (best guess) too.
300 if (!option_f
&& ncyl
== 0 && nhead
== 0 && nsect
== 0 &&
301 (disk
->label_type
!= L_TYPE_EFI
)) {
306 * Get the Solaris Fdisk Partition information
308 if (nhead
!= 0 && nsect
!= 0)
309 (void) copy_solaris_part(&cur_disk
->fdisk_part
);
311 if ((cur_disk
->label_type
== L_TYPE_EFI
) &&
312 (cur_disk
->disk_parts
->etoc
->efi_flags
&
313 EFI_GPT_PRIMARY_CORRUPT
)) {
314 err_print("Reading the primary EFI GPT label ");
315 err_print("failed. Using backup label.\n");
316 err_print("Use the 'backup' command to restore ");
317 err_print("the primary label.\n");
320 #if defined(_SUNOS_VTOC_16)
322 * If there is no fdisk solaris partition.
324 if (cur_disk
->fdisk_part
.numsect
== 0) {
325 err_print("No Solaris fdisk partition found.\n");
328 #endif /* defined(_SUNOS_VTOC_16) */
331 * If the label of the disk is marked dirty,
332 * see if they'd like to label the disk now.
334 if (cur_disk
->disk_flags
& DSK_LABEL_DIRTY
) {
335 if (check("Disk not labeled. Label it now") == 0) {
337 err_print("Write label failed\n");
339 cur_disk
->disk_flags
&= ~DSK_LABEL_DIRTY
;
348 * This routine implements the 'type' command. It allows the user to
349 * specify the type of the current disk. It should be necessary only
350 * if the disk was not labelled or was somehow labelled incorrectly.
351 * The list of legal types for the disk comes from information that was
357 struct disk_type
*type
, *tptr
, *oldtype
;
359 int i
, index
, deflt
, *defltptr
= NULL
;
360 struct disk_type disk_type
;
361 struct disk_type
*d
= &disk_type
;
363 int auto_conf_choice
;
365 struct dk_label label
;
366 struct efi_info efi_info
;
368 char volname
[LEN_DKL_VVOL
];
372 * There must be a current disk.
374 if (cur_disk
== NULL
) {
375 err_print("Current Disk is not set.\n");
378 oldtype
= cur_disk
->disk_type
;
379 type
= cur_ctype
->ctype_dlist
;
381 * Print out the list of choices.
383 fmt_print("\n\nAVAILABLE DRIVE TYPES:\n");
385 if (cur_ctype
->ctype_ctype
== DKC_SCSI_CCS
) {
386 auto_conf_choice
= 0;
387 fmt_print(" %d. Auto configure\n", first_disk
++);
389 auto_conf_choice
= -1;
393 for (tptr
= type
; tptr
!= NULL
; tptr
= tptr
->dtype_next
) {
395 * If we pass the current type, mark it to be the default.
397 if (cur_dtype
== tptr
) {
401 if (cur_disk
->label_type
== L_TYPE_EFI
) {
404 if (tptr
->dtype_asciilabel
)
405 fmt_print(" %d. %s\n", i
++,
406 tptr
->dtype_asciilabel
);
409 fmt_print(" %d. other\n", i
);
410 ioparam
.io_bounds
.lower
= 0;
411 ioparam
.io_bounds
.upper
= i
;
413 * Ask the user which type the disk is.
415 index
= input(FIO_INT
, "Specify disk type (enter its number)", ':',
416 &ioparam
, defltptr
, DATA_INPUT
);
418 * Find the type s/he chose.
420 if (index
== auto_conf_choice
) {
426 * User chose "auto configure".
428 (void) strcpy(x86_devname
, cur_disk
->disk_name
);
429 switch (cur_disk
->label_type
) {
431 if ((tptr
= auto_sense(cur_file
, 1, &label
)) == NULL
) {
432 err_print("Auto configure failed\n");
435 fmt_print("%s: configured with capacity of ",
436 cur_disk
->disk_name
);
437 nblks
= (diskaddr_t
)tptr
->dtype_ncyl
*
438 tptr
->dtype_nhead
* tptr
->dtype_nsect
;
439 scaled
= bn2mb(nblks
);
440 if (scaled
> 1024.0) {
441 fmt_print("%1.2fGB\n", scaled
/1024.0);
443 fmt_print("%1.2fMB\n", scaled
);
445 fmt_print("<%s cyl %d alt %d hd %d sec %d>\n",
446 tptr
->dtype_asciilabel
, tptr
->dtype_ncyl
,
447 tptr
->dtype_acyl
, tptr
->dtype_nhead
,
451 if ((tptr
= auto_efi_sense(cur_file
, &efi_info
))
453 err_print("Auto configure failed\n");
456 fmt_print("%s: configured with capacity of ",
457 cur_disk
->disk_name
);
458 scaled
= bn2mb(efi_info
.capacity
);
459 if (scaled
> 1024.0) {
460 fmt_print("%1.2fGB\n", scaled
/1024.0);
462 fmt_print("%1.2fMB\n", scaled
);
464 cur_blksz
= efi_info
.e_parts
->efi_lbasize
;
465 print_efi_string(efi_info
.vendor
, efi_info
.product
,
466 efi_info
.revision
, efi_info
.capacity
);
468 for (nparts
= 0; nparts
< cur_parts
->etoc
->efi_nparts
;
470 if (cur_parts
->etoc
->efi_parts
[nparts
].p_tag
==
472 if (cur_parts
->etoc
->efi_parts
[nparts
].
474 (void) strcpy(volname
,
475 cur_parts
->etoc
->efi_parts
483 if (delete_disk_type(cur_disk
->disk_type
) != 0) {
484 fmt_print("Autoconfiguration failed.\n");
487 cur_disk
->disk_type
= tptr
;
488 cur_disk
->disk_parts
= tptr
->dtype_plist
;
489 init_globals(cur_disk
);
492 for (nparts
= 0; nparts
<
493 cur_parts
->etoc
->efi_nparts
; nparts
++) {
494 if (cur_parts
->etoc
->efi_parts
[nparts
].p_tag
==
497 cur_parts
->etoc
->efi_parts
[nparts
].p_name
,
499 (void) strlcpy(cur_disk
->v_volume
, volname
,
507 /* Should never happen */
510 } else if ((index
== other_choice
) && (cur_label
== L_TYPE_SOLARIS
)) {
512 * User chose "other".
513 * Get the standard information on the new type.
514 * Put all information in a tmp structure, in
517 bzero((char *)d
, sizeof (struct disk_type
));
519 d
->dtype_ncyl
= get_ncyl();
520 d
->dtype_acyl
= get_acyl(d
->dtype_ncyl
);
521 d
->dtype_pcyl
= get_pcyl(d
->dtype_ncyl
, d
->dtype_acyl
);
522 d
->dtype_nhead
= get_nhead();
523 d
->dtype_phead
= get_phead(d
->dtype_nhead
, &d
->dtype_options
);
524 d
->dtype_nsect
= get_nsect();
525 d
->dtype_psect
= get_psect(&d
->dtype_options
);
526 d
->dtype_bpt
= get_bpt(d
->dtype_nsect
, &d
->dtype_options
);
527 d
->dtype_rpm
= get_rpm();
528 d
->dtype_fmt_time
= get_fmt_time(&d
->dtype_options
);
529 d
->dtype_cyl_skew
= get_cyl_skew(&d
->dtype_options
);
530 d
->dtype_trk_skew
= get_trk_skew(&d
->dtype_options
);
531 d
->dtype_trks_zone
= get_trks_zone(&d
->dtype_options
);
532 d
->dtype_atrks
= get_atrks(&d
->dtype_options
);
533 d
->dtype_asect
= get_asect(&d
->dtype_options
);
534 d
->dtype_cache
= get_cache(&d
->dtype_options
);
535 d
->dtype_threshold
= get_threshold(&d
->dtype_options
);
536 d
->dtype_prefetch_min
= get_min_prefetch(&d
->dtype_options
);
537 d
->dtype_prefetch_max
= get_max_prefetch(d
->dtype_prefetch_min
,
539 d
->dtype_bps
= get_bps();
541 d
->dtype_dr_type
= 0;
542 #endif /* defined(sparc) */
544 d
->dtype_asciilabel
= get_asciilabel();
546 * Add the new type to the list of possible types for
547 * this controller. We lock out interrupts so the lists
548 * can't get munged. We put off actually allocating the
549 * structure till here in case the user wanted to
550 * interrupt while still inputting information.
553 tptr
= (struct disk_type
*)zalloc(sizeof (struct disk_type
));
555 cur_ctype
->ctype_dlist
= tptr
;
557 while (type
->dtype_next
!= NULL
)
558 type
= type
->dtype_next
;
559 type
->dtype_next
= tptr
;
561 bcopy((char *)d
, (char *)tptr
, sizeof (disk_type
));
562 tptr
->dtype_next
= NULL
;
564 * the new disk type does not have any defined
565 * partition table . Hence copy the current partition
566 * table if possible else create a default
569 new_partitiontable(tptr
, oldtype
);
570 } else if ((index
== other_choice
) && (cur_label
== L_TYPE_EFI
)) {
572 cur_parts
->etoc
->efi_last_lba
= maxLBA
;
573 cur_parts
->etoc
->efi_last_u_lba
= maxLBA
- 34;
574 for (i
= 0; i
< cur_parts
->etoc
->efi_nparts
; i
++) {
575 cur_parts
->etoc
->efi_parts
[i
].p_start
= 0;
576 cur_parts
->etoc
->efi_parts
[i
].p_size
= 0;
577 cur_parts
->etoc
->efi_parts
[i
].p_tag
= V_UNASSIGNED
;
579 cur_parts
->etoc
->efi_parts
[8].p_start
=
580 maxLBA
- 34 - (1024 * 16);
581 cur_parts
->etoc
->efi_parts
[8].p_size
= (1024 * 16);
582 cur_parts
->etoc
->efi_parts
[8].p_tag
= V_RESERVED
;
584 err_print("Write label failed\n");
586 cur_disk
->disk_flags
&= ~DSK_LABEL_DIRTY
;
591 * User picked an existing disk type.
596 if (tptr
->dtype_asciilabel
) {
599 tptr
= tptr
->dtype_next
;
601 if ((tptr
->dtype_asciilabel
== NULL
) &&
602 (tptr
->dtype_next
!= NULL
)) {
603 while (tptr
->dtype_asciilabel
== NULL
) {
604 tptr
= tptr
->dtype_next
;
609 * Check for mounted file systems in the format zone.
610 * One potential problem with this would be that check()
611 * always returns 'yes' when running out of a file. However,
612 * it is actually ok because we don't let the program get
613 * started if there are mounted file systems and we are
614 * running from a file.
616 if ((tptr
!= oldtype
) &&
617 checkmount((diskaddr_t
)-1, (diskaddr_t
)-1)) {
619 "Cannot set disk type while it has mounted "
624 * check for partitions being used for swapping in format zone
626 if ((tptr
!= oldtype
) &&
627 checkswap((diskaddr_t
)-1, (diskaddr_t
)-1)) {
628 err_print("Cannot set disk type while its partition are "
629 "currently being used for swapping.\n");
634 * Check for partitions being used in SVM, VxVM or LU devices
637 if ((tptr
!= oldtype
) &&
638 checkdevinuse(cur_disk
->disk_name
, (diskaddr_t
)-1,
639 (diskaddr_t
)-1, 0, 0)) {
640 err_print("Cannot set disk type while its "
641 "partitions are currently in use.\n");
645 * If the type selected is different from the previous type,
646 * mark the disk as not labelled and reload the current
647 * partition info. This is not essential but probably the
648 * right thing to do, since the size of the disk has probably
652 if (tptr
!= oldtype
) {
653 cur_disk
->disk_type
= tptr
;
654 cur_disk
->disk_parts
= NULL
;
655 cur_disk
->disk_flags
&= ~DSK_LABEL
;
658 * Initialize the state of the current disk.
660 init_globals(cur_disk
);
661 (void) get_partition();
665 * If the label of the disk is marked dirty,
666 * see if they'd like to label the disk now.
668 if (cur_disk
->disk_flags
& DSK_LABEL_DIRTY
) {
669 if (check("Disk not labeled. Label it now") == 0) {
671 err_print("Write label failed\n");
673 cur_disk
->disk_flags
&= ~DSK_LABEL_DIRTY
;
682 * This routine implements the 'partition' command. It simply runs
683 * the partition menu.
690 * There must be a current disk type and a current disk
692 if (cur_dtype
== NULL
) {
693 err_print("Current Disk Type is not set.\n");
697 * Check for a valid fdisk table entry for Solaris
704 last_menu
= cur_menu
;
708 * If there is no current partition table, make one. This is
709 * so the commands within the menu never have to check for
710 * a non-existent table.
712 if (cur_parts
== NULL
)
713 err_print("making partition.\n");
720 run_menu(menu_partition
, "PARTITION", "partition", 0);
726 * This routine implements the 'current' command. It describes the
734 * If there is no current disk, say so. Note that this is
735 * not an error since it is a legitimate response to the inquiry.
737 if (cur_disk
== NULL
) {
738 fmt_print("No Current Disk.\n");
742 * Print out the info we have on the current disk.
744 fmt_print("Current Disk = %s", cur_disk
->disk_name
);
745 if (chk_volname(cur_disk
)) {
747 print_volname(cur_disk
);
750 if (cur_disk
->devfs_name
!= NULL
) {
751 if (cur_dtype
== NULL
) {
752 fmt_print("<type unknown>\n");
753 } else if (cur_label
== L_TYPE_SOLARIS
) {
754 fmt_print("<%s cyl %d alt %d hd %d sec %d>\n",
755 cur_dtype
->dtype_asciilabel
, ncyl
,
757 } else if (cur_label
== L_TYPE_EFI
) {
758 print_efi_string(cur_dtype
->vendor
,
759 cur_dtype
->product
, cur_dtype
->revision
,
760 cur_dtype
->capacity
);
763 fmt_print("%s\n", cur_disk
->devfs_name
);
765 fmt_print("%s%d: <", cur_ctlr
->ctlr_dname
,
766 cur_disk
->disk_dkinfo
.dki_unit
);
767 if (cur_dtype
== NULL
) {
768 fmt_print("type unknown");
769 } else if (cur_label
== L_TYPE_SOLARIS
) {
770 fmt_print("%s cyl %d alt %d hd %d sec %d",
771 cur_dtype
->dtype_asciilabel
, ncyl
,
773 } else if (cur_label
== L_TYPE_EFI
) {
774 print_efi_string(cur_dtype
->vendor
,
775 cur_dtype
->product
, cur_dtype
->revision
,
776 cur_dtype
->capacity
);
785 * This routine implements the 'format' command. It allows the user
786 * to format and verify any portion of the disk.
791 diskaddr_t start
, end
;
793 int format_time
, format_tracks
, format_cyls
;
798 struct scsi_inquiry
*inq
;
799 char rawbuf
[MAX_MODE_SENSE_SIZE
];
800 struct scsi_capacity_16 capacity
;
801 struct vpd_hdr
*vpdhdr
;
806 uint8_t prot_flag
[NUM_PROT_TYPE
] = {1, 0, 0, 0};
808 char *prot_descriptor
[NUM_PROT_TYPE
] = {
809 "Protection Information is disabled.",
810 "Protection Information is enabled.",
811 "Protection Information is enabled.",
812 "Protection Information is enabled.", };
815 * There must be a current disk type and a current disk
817 if (cur_dtype
== NULL
) {
818 err_print("Current Disk Type is not set.\n");
823 * There must be a format routine in cur_ops structure to have
826 if (cur_ops
->op_format
== NULL
) {
828 "Cannot format this drive. Please use your Manufacturer supplied formatting "
834 * There must be a current defect list. Except for
835 * unformatted SCSI disks. For them the defect list
836 * can only be retrieved after formatting the disk.
838 if ((cur_ctype
->ctype_flags
& CF_SCSI
) && !EMBEDDED_SCSI
&&
839 (cur_ctype
->ctype_flags
& CF_DEFECTS
) &&
840 ! (cur_flags
& DISK_FORMATTED
)) {
841 cur_list
.flags
|= LIST_RELOAD
;
843 } else if (cur_list
.list
== NULL
&& !EMBEDDED_SCSI
) {
844 err_print("Current Defect List must be initialized.\n");
848 * Ask for the bounds of the format. We always use the whole
849 * disk as the default, since that is the most likely case.
850 * Note, for disks which must be formatted accross the whole disk,
851 * don't bother the user.
853 ioparam
.io_bounds
.lower
= start
= 0;
854 if (cur_label
== L_TYPE_SOLARIS
) {
855 if (cur_ctype
->ctype_flags
& CF_SCSI
) {
856 ioparam
.io_bounds
.upper
= end
= datasects() - 1;
858 ioparam
.io_bounds
.upper
= end
= physsects() - 1;
861 ioparam
.io_bounds
.upper
= end
= cur_parts
->etoc
->efi_last_lba
;
864 if (! (cur_ctlr
->ctlr_flags
& DKI_FMTVOL
)) {
865 deflt
= ioparam
.io_bounds
.lower
;
866 start
= input(FIO_BN
,
867 "Enter starting block number", ':',
868 &ioparam
, (int *)&deflt
, DATA_INPUT
);
869 ioparam
.io_bounds
.lower
= start
;
870 deflt
= ioparam
.io_bounds
.upper
;
872 "Enter ending block number", ':',
873 &ioparam
, (int *)&deflt
, DATA_INPUT
);
876 * Some disks can format tracks. Make sure the whole track is
877 * specified for them.
879 if (cur_ctlr
->ctlr_flags
& DKI_FMTTRK
) {
880 if (bn2s(start
) != 0 ||
881 bn2s(end
) != sectors(bn2h(end
)) - 1) {
882 err_print("Controller requires formatting of ");
883 err_print("entire tracks.\n");
888 * Check for mounted file systems in the format zone, and if we
889 * find any, make sure they are really serious. One potential
890 * problem with this would be that check() always returns 'yes'
891 * when running out of a file. However, it is actually ok
892 * because we don't let the program get started if there are
893 * mounted file systems and we are running from a file.
895 if (checkmount(start
, end
)) {
897 "Cannot format disk while it has mounted partitions.\n\n");
901 * check for partitions being used for swapping in format zone
903 if (checkswap(start
, end
)) {
904 err_print("Cannot format disk while its partition are \
905 currently being used for swapping.\n");
909 * Check for partitions being used in SVM, VxVM or LU devices
910 * in this format zone
912 if (checkdevinuse(cur_disk
->disk_name
, start
, end
, 0, 0)) {
913 err_print("Cannot format disk while its partitions "
914 "are currently in use.\n");
918 if (cur_disk
->disk_lbasize
!= DEV_BSIZE
) {
919 fmt_print("Current disk sector size is %d Byte, format\n"
920 "will change the sector size to 512 Byte. ",
921 cur_disk
->disk_lbasize
);
922 if (check("Continue")) {
928 * set the default protection type
930 prot_type
= PROT_TYPE_0
;
933 * Check if the protect information of this disk is enabled
935 if (uscsi_inquiry(cur_file
, rawbuf
, sizeof (rawbuf
))) {
936 err_print("Inquiry failed\n");
939 inq
= (struct scsi_inquiry
*)rawbuf
;
940 protect
= inq
->inq_protect
;
942 fmt_print("The protection information is not enabled\n");
944 "The disk will be formatted with protection type 0\n");
946 (void) memset(rawbuf
, 0, MAX_MODE_SENSE_SIZE
);
947 if (uscsi_inquiry_page_86h(cur_file
, rawbuf
, sizeof (rawbuf
))) {
948 err_print("Inquiry with page 86h failed\n");
951 vpdhdr
= (struct vpd_hdr
*)rawbuf
;
952 pagecode
= vpdhdr
->page_code
;
953 if (pagecode
!= 0x86) {
954 err_print("Inquiry with page 86h failed\n");
957 spt
= (rawbuf
[4] << 2) >> 5;
958 fmt_print("This disk can support protection types:\n");
989 "Invalid supported protection types\n");
992 for (i
= 0; i
< NUM_PROT_TYPE
; i
++) {
993 if (prot_flag
[i
] == 1) {
994 fmt_print("[%d] TYPE_%d : ", i
, i
);
995 fmt_print("%s\n", prot_descriptor
[i
]);
1000 * Get the current protection type
1002 if (uscsi_read_capacity_16(cur_file
, &capacity
)) {
1003 err_print("Read capacity_16 failed\n");
1006 p_type
= get_cur_protection_type(&capacity
);
1007 fmt_print("\nThe disk is currently formatted with TYPE_%d.\n",
1011 * Ask user what protection type to use
1013 ioparam
.io_bounds
.lower
= PROT_TYPE_0
;
1014 ioparam
.io_bounds
.upper
= PROT_TYPE_3
;
1015 prot_type
= input(FIO_INT
, "Specify the New Protection Type",
1016 ':', &ioparam
, NULL
, DATA_INPUT
);
1018 * if get a unsupported protection type, then use the
1019 * current type: p_type.
1021 if (prot_flag
[prot_type
] == 0) {
1022 fmt_print("Unsupported protection type.\n");
1025 fmt_print("The disk will be formatted to type %d\n", prot_type
);
1028 if (SCSI
&& (format_time
= scsi_format_time()) > 0) {
1030 "\nReady to format. Formatting cannot be interrupted\n"
1031 "and takes %d minutes (estimated). ", format_time
);
1033 } else if (cur_dtype
->dtype_options
& SUP_FMTTIME
) {
1035 * Formatting time is (2 * time of 1 spin * number of
1036 * tracks) + (step rate * number of cylinders) rounded
1037 * up to the nearest minute. Note, a 10% fudge factor
1038 * is thrown in for insurance.
1040 if (cur_dtype
->dtype_fmt_time
== 0)
1041 cur_dtype
->dtype_fmt_time
= 2;
1043 format_tracks
= ((end
-start
) / cur_dtype
->dtype_nsect
) + 1;
1044 format_cyls
= format_tracks
/ cur_dtype
->dtype_nhead
;
1045 format_tracks
= format_tracks
* cur_dtype
->dtype_fmt_time
;
1050 format_time
= ((60000 / cur_dtype
->dtype_rpm
) +1) *
1051 format_tracks
+ format_cyls
* 7;
1053 * 20% done tick (sec)
1055 format_interval
= format_time
/ 5000;
1059 format_time
= (format_time
+ 59999) / 60000;
1062 * Check format time values and make adjustments
1063 * to prevent sleeping too long (forever?) or
1066 if (format_time
<= 1) {
1068 * Format time is less than 1 min..
1073 if (format_interval
< 11) {
1074 /* Format time is less than 1 minute. */
1075 if (format_interval
< 2)
1076 format_interval
= 2; /* failsafe */
1077 format_interval
= 10;
1079 /* Format time is greater than 1 minute. */
1080 format_interval
-= 10;
1084 "Ready to format. Formatting cannot be interrupted\n"
1085 "and takes %d minutes (estimated). ", format_time
);
1088 "Ready to format. Formatting cannot be interrupted.\n");
1090 if (check("Continue")) {
1095 * Print the time so that the user will know when format started.
1096 * Lock out interrupts. This could be a problem, since it could
1097 * cause the user to sit for quite awhile with no control, but we
1098 * don't have any other good way of keeping his gun from going off.
1100 clock
= time((time_t *)0);
1101 fmt_print("Beginning format. The current time is %s\n",
1105 * Mark the defect list dirty so it will be rewritten when we are
1106 * done. It is possible to qualify this so it doesn't always
1107 * get rewritten, but it's not worth the trouble.
1108 * Note: no defect lists for embedded scsi drives.
1110 if (!EMBEDDED_SCSI
) {
1111 cur_list
.flags
|= LIST_DIRTY
;
1114 * If we are formatting over any of the labels, mark the label
1115 * dirty so it will be rewritten.
1117 if (cur_disk
->label_type
== L_TYPE_SOLARIS
) {
1118 if (start
< totalsects() && end
>= datasects()) {
1119 if (cur_disk
->disk_flags
& DSK_LABEL
)
1120 cur_flags
|= LABEL_DIRTY
;
1122 } else if (cur_disk
->label_type
== L_TYPE_EFI
) {
1124 if (cur_disk
->disk_flags
& DSK_LABEL
)
1125 cur_flags
|= LABEL_DIRTY
;
1129 cur_flags
|= LABEL_DIRTY
;
1132 * Do the format. bugid 1009138 removed the use of fork to
1133 * background the format and print a tick.
1136 status
= (*cur_ops
->op_format
)(start
, end
, &cur_list
);
1139 err_print("failed\n");
1142 fmt_print("done\n");
1143 if (option_msg
&& diag_msg
) {
1144 clock
= time((time_t *)0);
1145 fmt_print("The current time is %s\n", ctime(&clock
));
1147 cur_flags
|= DISK_FORMATTED
;
1149 * If the defect list or label is dirty, write them out again.
1150 * Note, for SCSI we have to wait til now to load defect list
1151 * since we can't access it until after formatting a virgin disk.
1153 /* enter_critical(); */
1154 if (cur_list
.flags
& LIST_RELOAD
) {
1155 assert(!EMBEDDED_SCSI
);
1156 if (*cur_ops
->op_ex_man
== NULL
||
1157 (*cur_ops
->op_ex_man
)(&cur_list
)) {
1158 err_print("Warning: unable to reload defect list\n");
1159 cur_list
.flags
&= ~LIST_DIRTY
;
1162 cur_list
.flags
|= LIST_DIRTY
;
1165 if (cur_list
.flags
& LIST_DIRTY
) {
1166 assert(!EMBEDDED_SCSI
);
1167 write_deflist(&cur_list
);
1170 if (cur_flags
& LABEL_DIRTY
) {
1171 (void) write_label();
1172 cur_flags
&= ~LABEL_DIRTY
;
1175 * Come up for air, since the verify step does not need to
1176 * be atomic (it does it's own lockouts when necessary).
1180 * If we are supposed to verify, we do the 'write' test over
1181 * the format zone. The rest of the analysis parameters are
1182 * left the way they were.
1188 fmt_print("\nVerifying media...");
1189 status
= do_scan(SCAN_PATTERN
, F_SILENT
);
1192 * If the defect list or label is dirty, write them out again.
1194 if (cur_list
.flags
& LIST_DIRTY
) {
1195 assert(!EMBEDDED_SCSI
);
1197 write_deflist(&cur_list
);
1199 if (cur_flags
& LABEL_DIRTY
) {
1200 cur_flags
&= ~LABEL_DIRTY
;
1201 (void) write_label();
1207 * This routine implements the 'repair' command. It allows the user
1208 * to reallocate sectors on the disk that have gone bad.
1215 u_ioparam_t ioparam
;
1218 int block_has_error
;
1222 * There must be a current disk type (and therefore a current disk).
1224 if (cur_dtype
== NULL
) {
1225 err_print("Current Disk Type is not set.\n");
1229 * The current disk must be formatted for repair to work.
1231 if (!(cur_flags
& DISK_FORMATTED
)) {
1232 err_print("Current Disk is unformatted.\n");
1236 * Check for a valid fdisk table entry for Solaris
1238 if (!good_fdisk()) {
1242 * Repair is an optional command for controllers, so it may
1245 if (cur_ops
->op_repair
== NULL
) {
1246 err_print("Controller does not support repairing.\n");
1247 err_print("or disk supports automatic defect management.\n");
1251 * There must be a defect list for non-embedded scsi devices,
1252 * since we will add to it.
1254 if (!EMBEDDED_SCSI
&& cur_list
.list
== NULL
) {
1255 err_print("Current Defect List must be initialized.\n");
1259 * Ask the user which sector has gone bad.
1261 ioparam
.io_bounds
.lower
= 0;
1262 if (cur_disk
->label_type
== L_TYPE_SOLARIS
) {
1263 ioparam
.io_bounds
.upper
= physsects() - 1;
1265 ioparam
.io_bounds
.upper
= cur_parts
->etoc
->efi_last_lba
;
1268 "Enter absolute block number of defect", ':',
1269 &ioparam
, (int *)NULL
, DATA_INPUT
);
1271 * Check to see if there is a mounted file system over the
1272 * specified sector. If there is, make sure the user is
1275 if (checkmount(bn
, bn
)) {
1276 if (check("Repair is in a mounted partition, continue"))
1280 * check for partitions being used for swapping in format zone
1282 if (checkswap(bn
, bn
)) {
1283 if (check("Repair is in a partition which is currently \
1284 being used for swapping.\ncontinue"))
1288 if (checkdevinuse(cur_disk
->disk_name
, bn
, bn
, 0, 0)) {
1289 if (check("Repair is in a partition which is currently "
1290 "in use.\ncontinue"))
1294 buf
= zalloc((cur_disk
->disk_lbasize
== 0) ?
1295 SECSIZE
: cur_disk
->disk_lbasize
);
1298 * Try to read the sector before repairing it. If we can
1299 * get good data out of it, we can write that data back
1300 * after the repair. If the sector looks ok, ask the
1301 * user to confirm the repair, since it doesn't appear
1302 * necessary. Try reading the block several times to
1303 * see if we can read it consistently.
1305 * First, let's see if the block appears to have problems...
1307 block_has_error
= 1;
1308 for (i
= 0; i
< 5; i
++) {
1309 status
= (*cur_ops
->op_rdwr
)(DIR_READ
, cur_file
, bn
,
1310 1, buf
, (F_SILENT
| F_ALLERRS
), NULL
);
1312 break; /* one of the tries failed */
1315 block_has_error
= 0;
1317 This block doesn't appear to be bad. Repair it anyway")) {
1325 if (check("Ready to repair defect, continue")) {
1330 * We're committed to repairing it. Try to get any good
1331 * data out of the block if possible. Note that we do
1332 * not set the F_ALLERRS flag.
1335 for (i
= 0; i
< 5; i
++) {
1336 status
= (*cur_ops
->op_rdwr
)(DIR_READ
, cur_file
, bn
,
1337 1, buf
, F_SILENT
, NULL
);
1344 * Lock out interrupts so the disk can't get out of sync with
1349 fmt_print("Repairing ");
1350 if (block_has_error
) {
1351 fmt_print("%s error on ", buf_is_good
? "soft" : "hard");
1353 fmt_print("block %llu (", bn
);
1354 pr_dblock(fmt_print
, bn
);
1359 status
= (*cur_ops
->op_repair
)(bn
, F_NORMAL
);
1361 fmt_print("failed.\n\n");
1364 * The repair worked. Write the old data to the new
1365 * block if we were able to read it, otherwise
1366 * zero out the new block. If it looks like the
1367 * new block is bad, let the user know that, too.
1368 * Should we attempt auto-repair in this case?
1372 bzero(buf
, cur_disk
->disk_lbasize
);
1374 status
= (*cur_ops
->op_rdwr
)(DIR_WRITE
, cur_file
, bn
,
1375 1, buf
, (F_SILENT
| F_ALLERRS
), NULL
);
1377 status
= (*cur_ops
->op_rdwr
)(DIR_READ
, cur_file
,
1378 bn
, 1, buf
, (F_SILENT
| F_ALLERRS
), NULL
);
1381 fmt_print("The new block %llu (", bn
);
1382 pr_dblock(fmt_print
, bn
);
1383 fmt_print(") also appears defective.\n");
1387 * Add the bad sector to the defect list, write out
1388 * the defect list, and kill off the working list so
1389 * it will get synced up with the current defect list
1390 * next time we need it.
1392 * For embedded scsi, we don't require a defect list.
1393 * However, if we have one, add the defect if the
1394 * list includes the grown list. If not, kill it
1395 * to force a resync if we need the list later.
1397 if (EMBEDDED_SCSI
) {
1398 if (cur_list
.list
!= NULL
) {
1399 if (cur_list
.flags
& LIST_PGLIST
) {
1400 add_ldef(bn
, &cur_list
);
1402 kill_deflist(&cur_list
);
1405 } else if (cur_ctype
->ctype_flags
& CF_WLIST
) {
1406 kill_deflist(&cur_list
);
1407 if (*cur_ops
->op_ex_cur
!= NULL
) {
1408 (*cur_ops
->op_ex_cur
)(&cur_list
);
1409 fmt_print("Current list updated\n");
1412 add_ldef(bn
, &cur_list
);
1413 write_deflist(&cur_list
);
1415 kill_deflist(&work_list
);
1427 * This routine implements the 'show' command. It translates a disk
1428 * block given in any format into decimal, hexadecimal, and
1429 * cylinder/head/sector format.
1434 u_ioparam_t ioparam
;
1438 * There must be a current disk type, so we will know the geometry.
1440 if (cur_dtype
== NULL
) {
1441 err_print("Current Disk Type is not set.\n");
1445 * Ask the user for a disk block.
1447 ioparam
.io_bounds
.lower
= 0;
1448 if (cur_disk
->label_type
== L_TYPE_SOLARIS
) {
1449 ioparam
.io_bounds
.upper
= physsects() - 1;
1451 ioparam
.io_bounds
.upper
= cur_parts
->etoc
->efi_last_lba
;
1453 bn
= input(FIO_BN
, "Enter a disk block", ':',
1454 &ioparam
, (int *)NULL
, DATA_INPUT
);
1458 fmt_print("Disk block = %lld = 0x%llx = (", bn
, bn
);
1459 pr_dblock(fmt_print
, bn
);
1465 * This routine implements the 'label' command. It writes the
1466 * primary and backup labels onto the current disk.
1472 int deflt
, *defltptr
= NULL
;
1475 * There must be a current disk type (and therefore a current disk).
1477 if (cur_dtype
== NULL
) {
1478 err_print("Current Disk Type is not set.\n");
1482 * The current disk must be formatted to label it.
1484 if (!(cur_flags
& DISK_FORMATTED
)) {
1485 err_print("Current Disk is unformatted.\n");
1489 * Check for a valid fdisk table entry for Solaris
1491 if (!good_fdisk()) {
1495 * Check to see if there are any mounted file systems anywhere
1496 * on the current disk. If so, refuse to label the disk, but
1497 * only if the partitions would change for the mounted partitions.
1500 if (checkmount((diskaddr_t
)-1, (diskaddr_t
)-1)) {
1501 /* Bleagh, too descriptive */
1502 if (check_label_with_mount()) {
1503 err_print("Cannot label disk while it has "
1504 "mounted partitions.\n\n");
1510 * check to see if there any partitions being used for swapping
1511 * on the current disk. If so, refuse to label the disk, but
1512 * only if the partitions would change for the mounted partitions.
1514 if (checkswap((diskaddr_t
)-1, (diskaddr_t
)-1)) {
1515 if (check_label_with_swap()) {
1516 err_print("Cannot label disk while its "
1517 "partitions are currently being used for "
1524 * Check to see if any partitions used for svm, vxvm or live upgrade
1525 * are on the disk. If so, refuse to label the disk, but only
1526 * if we are trying to shrink a partition in use.
1528 if (checkdevinuse(cur_disk
->disk_name
, (diskaddr_t
)-1,
1529 (diskaddr_t
)-1, 0, 1)) {
1530 err_print("Cannot label disk when "
1531 "partitions are in use as described.\n");
1536 * If there is not a current partition map, warn the user we
1537 * are going to use the default. The default is the first
1538 * partition map we encountered in the data file. If there is
1539 * no default we give up.
1541 if (cur_parts
== NULL
) {
1542 fmt_print("Current Partition Table is not set, "
1543 "using default.\n");
1544 cur_disk
->disk_parts
= cur_parts
= cur_dtype
->dtype_plist
;
1545 if (cur_parts
== NULL
) {
1546 err_print("No default available, cannot label.\n");
1551 * If expert (-e) mode, then ask user if they wish
1552 * to change the current solaris label into an EFI one
1555 #if defined(_SUNOS_VTOC_8)
1559 u_ioparam_t ioparam
;
1560 struct extvtoc vtoc
;
1561 struct dk_label label
;
1562 struct dk_gpt
*vtoc64
;
1563 struct efi_info efinfo
;
1564 struct disk_type
*dptr
;
1566 /* Ask user what label to use */
1567 fmt_print("[0] SMI Label\n");
1568 fmt_print("[1] EFI Label\n");
1569 ioparam
.io_bounds
.lower
= 0;
1570 ioparam
.io_bounds
.upper
= 1;
1571 if ((cur_label
== L_TYPE_SOLARIS
) &&
1572 (cur_disk
->fdisk_part
.systid
!= EFI_PMBR
))
1573 deflt
= L_TYPE_SOLARIS
;
1577 choice
= input(FIO_INT
, "Specify Label type", ':',
1578 &ioparam
, defltptr
, DATA_INPUT
);
1579 if ((choice
== L_TYPE_SOLARIS
) &&
1580 (cur_label
== L_TYPE_SOLARIS
) &&
1581 (cur_disk
->fdisk_part
.systid
!= EFI_PMBR
)) {
1583 } else if ((choice
== L_TYPE_EFI
) &&
1584 (cur_label
== L_TYPE_EFI
)) {
1588 case L_TYPE_SOLARIS
:
1590 * EFI label to SMI label
1592 if (cur_dtype
->capacity
> INFINITY
) {
1593 fmt_print("Warning: SMI labels only support up to "
1597 if (cur_disk
->fdisk_part
.systid
== EFI_PMBR
) {
1598 fmt_print("Warning: This disk has an EFI label. "
1599 "Changing to SMI label will erase all\n"
1600 "current partitions.\n");
1601 if (check("Continue"))
1603 #if defined(_FIRMWARE_NEEDS_FDISK)
1604 fmt_print("You must use fdisk to delete the current "
1605 "EFI partition and create a new\n"
1606 "Solaris partition before you can convert the "
1612 #if defined(_FIRMWARE_NEEDS_FDISK)
1613 if (!(((cur_disk
->fdisk_part
.systid
!= SUNIXOS
) ||
1614 (cur_disk
->fdisk_part
.systid
!= SUNIXOS2
)) &&
1615 (cur_disk
->fdisk_part
.numsect
> 0))) {
1616 fmt_print("You must use fdisk to create a Solaris "
1617 "partition before you can convert the label.\n");
1622 (void) memset((char *)&label
, 0, sizeof (struct dk_label
));
1624 (void) strcpy(x86_devname
, cur_disk
->disk_name
);
1625 if (cur_ctype
->ctype_ctype
== DKC_DIRECT
||
1626 cur_ctype
->ctype_ctype
== DKC_BLKDEV
)
1627 dptr
= auto_direct_get_geom_label(cur_file
, &label
);
1629 dptr
= auto_sense(cur_file
, 1, &label
);
1631 fmt_print("Autoconfiguration failed.\n");
1635 pcyl
= label
.dkl_pcyl
;
1636 ncyl
= label
.dkl_ncyl
;
1637 acyl
= label
.dkl_acyl
;
1638 nhead
= label
.dkl_nhead
;
1639 nsect
= label
.dkl_nsect
;
1641 if (delete_disk_type(cur_disk
->disk_type
) == 0) {
1642 cur_label
= L_TYPE_SOLARIS
;
1643 cur_disk
->label_type
= L_TYPE_SOLARIS
;
1644 cur_disk
->disk_type
= dptr
;
1645 cur_disk
->disk_parts
= dptr
->dtype_plist
;
1647 cur_parts
= dptr
->dtype_plist
;
1649 if (status
= write_label())
1650 err_print("Label failed.\n");
1652 cur_disk
->disk_flags
&= ~DSK_LABEL_DIRTY
;
1656 err_print("Label failed.\n");
1663 * SMI label to EFI label
1666 if ((cur_disk
->fdisk_part
.systid
== SUNIXOS
) ||
1667 (cur_disk
->fdisk_part
.systid
== SUNIXOS2
)) {
1668 fmt_print("Warning: This disk has an SMI label. "
1669 "Changing to EFI label will erase all\ncurrent "
1671 if (check("Continue")) {
1676 if (get_disk_info(cur_file
, &efinfo
, cur_disk
) != 0) {
1679 (void) memset((char *)&label
, 0, sizeof (struct dk_label
));
1680 label
.dkl_pcyl
= pcyl
;
1681 label
.dkl_ncyl
= ncyl
;
1682 label
.dkl_acyl
= acyl
;
1683 #if defined(_SUNOS_VTOC_16)
1684 label
.dkl_bcyl
= bcyl
;
1685 #endif /* defined(_SUNOC_VTOC_16) */
1686 label
.dkl_nhead
= nhead
;
1687 label
.dkl_nsect
= nsect
;
1688 #if defined(_SUNOS_VTOC_8)
1689 for (i
= 0; i
< NDKMAP
; i
++) {
1690 label
.dkl_map
[i
] = cur_parts
->pinfo_map
[i
];
1692 #endif /* defined(_SUNOS_VTOC_8) */
1693 label
.dkl_magic
= DKL_MAGIC
;
1694 label
.dkl_vtoc
= cur_parts
->vtoc
;
1695 if (label_to_vtoc(&vtoc
, &label
) == -1) {
1698 if (SMI_vtoc_to_EFI(cur_file
, &vtoc64
) == -1) {
1701 if (efi_write(cur_file
, vtoc64
) != 0) {
1703 err_print("Warning: error writing EFI.\n");
1706 cur_disk
->disk_flags
&= ~DSK_LABEL_DIRTY
;
1709 * copy over the EFI vtoc onto the SMI vtoc and return
1712 dptr
= auto_efi_sense(cur_file
, &efinfo
);
1714 fmt_print("Autoconfiguration failed.\n");
1718 cur_label
= L_TYPE_EFI
;
1719 cur_disk
->label_type
= L_TYPE_EFI
;
1720 cur_disk
->disk_type
= dptr
;
1721 cur_disk
->disk_parts
= dptr
->dtype_plist
;
1723 cur_parts
= dptr
->dtype_plist
;
1724 cur_parts
->etoc
= vtoc64
;
1726 ncyl
= pcyl
= nsect
= psect
= acyl
= phead
= 0;
1729 * Get the Solais Fdisk Partition information.
1731 (void) copy_solaris_part(&cur_disk
->fdisk_part
);
1739 * Make sure the user is serious.
1741 if (check("Ready to label disk, continue")) {
1745 * Write the labels out (this will also notify unix) and
1749 if (status
= write_label())
1750 err_print("Label failed.\n");
1755 * This routine implements the 'analyze' command. It simply runs
1763 * There must be a current disk type (and therefor a current disk).
1765 if (cur_dtype
== NULL
) {
1766 err_print("Current Disk Type is not set.\n");
1770 last_menu
= cur_menu
;
1775 run_menu(menu_analyze
, "ANALYZE", "analyze", 0);
1781 * This routine implements the 'defect' command. It simply runs
1790 * There must be a current disk type (and therefor a current disk).
1792 if (cur_dtype
== NULL
) {
1793 err_print("Current Disk Type is not set.\n");
1798 * Check for the defect management and list management ops and
1799 * display appropriate message.
1801 if ((cur_ops
->op_ex_man
== NULL
) && (cur_ops
->op_ex_cur
== NULL
) &&
1802 (cur_ops
->op_create
== NULL
) && (cur_ops
->op_wr_cur
== NULL
)) {
1803 err_print("Controller does not support defect management\n");
1804 err_print("or disk supports automatic defect management.\n");
1808 last_menu
= cur_menu
;
1811 * Lock out interrupt while we manipulate the defect lists.
1815 * If the working list is null but there is a current list,
1816 * update the working list to be a copy of the current list.
1818 if ((work_list
.list
== NULL
) && (cur_list
.list
!= NULL
)) {
1819 work_list
.header
= cur_list
.header
;
1820 work_list
.list
= (struct defect_entry
*)zalloc(
1821 deflist_size(cur_blksz
, work_list
.header
.count
) *
1823 for (i
= 0; i
< work_list
.header
.count
; i
++)
1824 *(work_list
.list
+ i
) = *(cur_list
.list
+ i
);
1825 work_list
.flags
= cur_list
.flags
& LIST_PGLIST
;
1831 run_menu(menu_defect
, "DEFECT", "defect", 0);
1835 * If the user has modified the working list but not committed
1836 * it, warn him that he is probably making a mistake.
1838 if (work_list
.flags
& LIST_DIRTY
) {
1839 if (!EMBEDDED_SCSI
) {
1841 "Warning: working defect list modified; but not committed.\n");
1843 "Do you wish to commit changes to current defect list"))
1851 * This routine implements the 'backup' command. It allows the user
1852 * to search for backup labels on the current disk. This is useful
1853 * if the primary label was lost and the user wishes to recover the
1854 * partition information for the disk. The disk is relabeled and
1855 * the current defect list is written out if a backup label is found.
1860 struct dk_label label
;
1861 struct disk_type
*dtype
;
1862 struct partition_info
*parts
, *plist
;
1868 * There must be a current disk type (and therefore a current disk).
1870 if (cur_dtype
== NULL
) {
1871 err_print("Current Disk Type is not set.\n");
1875 * The disk must be formatted to read backup labels.
1877 if (!(cur_flags
& DISK_FORMATTED
)) {
1878 err_print("Current Disk is unformatted.\n");
1882 * Check for a valid fdisk table entry for Solaris
1884 if (!good_fdisk()) {
1888 * If we found a primary label on this disk, make sure
1889 * the user is serious.
1891 if (cur_disk
->label_type
== L_TYPE_EFI
) {
1892 if (((cur_disk
->disk_parts
->etoc
->efi_flags
&
1893 EFI_GPT_PRIMARY_CORRUPT
) == 0) &&
1894 check("Disk has a primary label, still continue"))
1896 fmt_print("Restoring primary label.\n");
1897 if (write_label()) {
1898 err_print("Failed\n");
1902 } else if (((cur_disk
->disk_flags
& (DSK_LABEL
| DSK_LABEL_DIRTY
)) ==
1904 (check("Disk has a primary label, still continue"))) {
1908 buf
= zalloc(cur_blksz
);
1909 fmt_print("Searching for backup labels...");
1910 (void) fflush(stdout
);
1913 * Some disks have the backup labels in a strange place.
1915 if (cur_ctype
->ctype_flags
& CF_BLABEL
)
1920 * Loop through each copy of the backup label.
1922 for (sec
= 1; ((sec
< BAD_LISTCNT
* 2 + 1) && (sec
< nsect
));
1924 bn
= chs2bn(ncyl
+ acyl
- 1, head
, sec
) + solaris_offset
;
1926 * Attempt to read it.
1928 if ((*cur_ops
->op_rdwr
)(DIR_READ
, cur_file
, bn
,
1929 1, buf
, F_NORMAL
, NULL
)) {
1933 (void *) memcpy((char *)&label
, buf
, sizeof (struct dk_label
));
1936 * Verify that it is a reasonable label.
1938 if (!checklabel(&label
))
1940 if (trim_id(label
.dkl_asciilabel
))
1943 * Lock out interrupts while we manipulate lists.
1946 fmt_print("found.\n");
1948 * Find out which disk type the backup label claims.
1950 for (dtype
= cur_ctype
->ctype_dlist
; dtype
!= NULL
;
1951 dtype
= dtype
->dtype_next
)
1952 if (dtype_match(&label
, dtype
))
1955 * If it disagrees with our current type, something
1956 * real bad is happening.
1958 if (dtype
!= cur_dtype
) {
1959 if (dtype
== NULL
) {
1961 Unknown disk type in backup label\n");
1966 fmt_print("Backup label claims different type:\n");
1967 fmt_print(" <%s cyl %d alt %d hd %d sec %d>\n",
1968 label
.dkl_asciilabel
, label
.dkl_ncyl
,
1969 label
.dkl_acyl
, label
.dkl_nhead
,
1971 if (check("Continue")) {
1979 * Try to match the partition map with a known map.
1981 for (parts
= dtype
->dtype_plist
; parts
!= NULL
;
1982 parts
= parts
->pinfo_next
)
1983 if (parts_match(&label
, parts
))
1986 * If we couldn't match it, allocate space for a new one,
1987 * fill in the info, and add it to the list. The name
1988 * for the new map is derived from the disk name.
1990 if (parts
== NULL
) {
1991 parts
= (struct partition_info
*)
1992 zalloc(sizeof (struct partition_info
));
1993 plist
= dtype
->dtype_plist
;
1995 dtype
->dtype_plist
= parts
;
1997 while (plist
->pinfo_next
!= NULL
)
1998 plist
= plist
->pinfo_next
;
1999 plist
->pinfo_next
= parts
;
2001 parts
->pinfo_name
= alloc_string("original");
2002 for (i
= 0; i
< NDKMAP
; i
++)
2004 #if defined(_SUNOS_VTOC_8)
2005 parts
->pinfo_map
[i
] = label
.dkl_map
[i
];
2007 #elif defined(_SUNOS_VTOC_16)
2008 parts
->pinfo_map
[i
].dkl_cylno
=
2009 label
.dkl_vtoc
.v_part
[i
].p_start
/ spc();
2010 parts
->pinfo_map
[i
].dkl_nblk
=
2011 label
.dkl_vtoc
.v_part
[i
].p_size
;
2013 #error No VTOC layout defined.
2014 #endif /* defined(_SUNOS_VTOC_8) */
2015 parts
->vtoc
= label
.dkl_vtoc
;
2018 * We now have a partition map. Make it the current map.
2020 cur_disk
->disk_parts
= cur_parts
= parts
;
2023 * Rewrite the labels and defect lists, as appropriate.
2025 if (EMBEDDED_SCSI
) {
2026 fmt_print("Restoring primary label.\n");
2027 if (write_label()) {
2032 fmt_print("Restoring primary label and defect list.\n");
2033 if (write_label()) {
2037 if (cur_list
.list
!= NULL
)
2038 write_deflist(&cur_list
);
2045 * If we didn't find any backup labels, say so.
2047 fmt_print("not found.\n\n");
2053 * This routine is called by c_verify() for an EFI labeled disk
2058 struct efi_info efi_info
;
2059 struct partition_info tmp_pinfo
;
2062 status
= read_efi_label(cur_file
, &efi_info
, cur_disk
);
2064 err_print("Warning: Could not read label.\n");
2067 if (cur_parts
->etoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
2068 err_print("Reading the primary EFI GPT label ");
2069 err_print("failed. Using backup label.\n");
2070 err_print("Use the 'backup' command to restore ");
2071 err_print("the primary label.\n");
2073 tmp_pinfo
.etoc
= efi_info
.e_parts
;
2075 if (cur_parts
->etoc
->efi_parts
[8].p_name
) {
2076 fmt_print("Volume name = <%8s>\n",
2077 cur_parts
->etoc
->efi_parts
[8].p_name
);
2079 fmt_print("Volume name = < >\n");
2081 fmt_print("ascii name = ");
2082 print_efi_string(efi_info
.vendor
, efi_info
.product
,
2083 efi_info
.revision
, efi_info
.capacity
);
2086 fmt_print("bytes/sector = %d\n", cur_blksz
);
2087 fmt_print("sectors = %llu\n", cur_parts
->etoc
->efi_last_lba
);
2088 fmt_print("accessible sectors = %llu\n",
2089 cur_parts
->etoc
->efi_last_u_lba
);
2091 print_map(&tmp_pinfo
);
2093 free(efi_info
.vendor
);
2094 free(efi_info
.product
);
2095 free(efi_info
.revision
);
2100 * This routine implements the 'verify' command. It allows the user
2101 * to read the labels on the current disk.
2106 struct dk_label p_label
, b_label
, *label
;
2107 struct partition_info tmp_pinfo
;
2109 int sec
, head
, i
, status
;
2110 int p_label_bad
= 0;
2111 int b_label_bad
= 0;
2112 int p_label_found
= 0;
2113 int b_label_found
= 0;
2118 * There must be a current disk type (and therefore a current disk).
2120 if (cur_dtype
== NULL
) {
2121 err_print("Current Disk Type is not set.\n");
2125 * The disk must be formatted to read labels.
2127 if (!(cur_flags
& DISK_FORMATTED
)) {
2128 err_print("Current Disk is unformatted.\n");
2132 * Check for a valid fdisk table entry for Solaris
2134 if (!good_fdisk()) {
2138 * Branch off here if the disk is EFI labelled.
2140 if (cur_label
== L_TYPE_EFI
) {
2141 return (c_verify_efi());
2144 * Attempt to read the primary label.
2146 status
= read_label(cur_file
, &p_label
);
2148 err_print("Warning: Could not read primary label.\n");
2152 * Verify that it is a reasonable label.
2155 * Save complete ascii string for printing later.
2157 (void) strncpy(id_str
, p_label
.dkl_asciilabel
, 128);
2159 if ((!checklabel((struct dk_label
*)&p_label
)) ||
2160 (trim_id(p_label
.dkl_asciilabel
))) {
2162 Warning: Primary label appears to be corrupt.\n");
2167 * Make sure it matches current label
2169 if ((!dtype_match(&p_label
, cur_dtype
)) ||
2170 (!parts_match(&p_label
, cur_parts
))) {
2172 Warning: Primary label on disk appears to be different from\ncurrent label.\n");
2179 * Read backup labels.
2180 * Some disks have the backup labels in a strange place.
2182 if (cur_ctype
->ctype_flags
& CF_BLABEL
)
2187 buf
= zalloc(cur_blksz
);
2189 * Loop through each copy of the backup label.
2191 for (sec
= 1; ((sec
< BAD_LISTCNT
* 2 + 1) && (sec
< nsect
));
2193 bn
= chs2bn(ncyl
+ acyl
- 1, head
, sec
) + solaris_offset
;
2195 * Attempt to read it.
2197 if ((*cur_ops
->op_rdwr
)(DIR_READ
, cur_file
, bn
,
2198 1, buf
, F_NORMAL
, NULL
))
2201 (void *) memcpy((char *)&b_label
, buf
,
2202 sizeof (struct dk_label
));
2205 * Verify that it is a reasonable label.
2207 if (!checklabel(&b_label
))
2211 * Save complete label only if no primary label exists
2214 (void) strncpy(id_str
, b_label
.dkl_asciilabel
, 128);
2216 if (trim_id(b_label
.dkl_asciilabel
))
2220 * Compare against primary label
2222 if (p_label_found
) {
2223 if ((strcmp(b_label
.dkl_asciilabel
,
2224 p_label
.dkl_asciilabel
) != 0) ||
2225 (b_label
.dkl_ncyl
!= p_label
.dkl_ncyl
) ||
2226 (b_label
.dkl_acyl
!= p_label
.dkl_acyl
) ||
2227 (b_label
.dkl_nhead
!= p_label
.dkl_nhead
) ||
2228 (b_label
.dkl_nsect
!= p_label
.dkl_nsect
)) {
2231 for (i
= 0; i
< NDKMAP
; i
++) {
2232 #if defined(_SUNOS_VTOC_8)
2233 if ((b_label
.dkl_map
[i
].dkl_cylno
!=
2234 p_label
.dkl_map
[i
].dkl_cylno
) ||
2235 (b_label
.dkl_map
[i
].dkl_nblk
!=
2236 p_label
.dkl_map
[i
].dkl_nblk
)) {
2241 #elif defined(_SUNOS_VTOC_16)
2242 if ((b_label
.dkl_vtoc
.v_part
[i
].p_tag
!=
2243 p_label
.dkl_vtoc
.v_part
[i
].p_tag
) ||
2244 (b_label
.dkl_vtoc
.v_part
[i
].p_flag
2245 != p_label
.dkl_vtoc
.v_part
[i
].
2247 (b_label
.dkl_vtoc
.v_part
[i
].p_start
2248 != p_label
.dkl_vtoc
.v_part
[i
].
2250 (b_label
.dkl_vtoc
.v_part
[i
].p_size
2251 != p_label
.dkl_vtoc
.v_part
[i
].
2257 #error No VTOC layout defined.
2258 #endif /* defined(_SUNOS_VTOC_8) */
2264 "Warning: Primary and backup labels do not match.\n");
2268 * If we didn't find any backup labels, say so.
2271 err_print("Warning: Could not read backup labels.\n");
2273 if ((!b_label_found
) || (p_label_bad
) || (b_label_bad
))
2275 Warning: Check the current partitioning and 'label' the disk or use the\n\
2276 \t 'backup' command.\n");
2279 * Print label information.
2281 if (p_label_found
) {
2282 fmt_print("\nPrimary label contents:\n");
2284 } else if (b_label_found
) {
2285 fmt_print("\nBackup label contents:\n");
2293 * Must put info into partition_info struct for
2294 * for print routine.
2296 bzero(&tmp_pinfo
, sizeof (struct partition_info
));
2297 for (i
= 0; i
< NDKMAP
; i
++) {
2299 #if defined(_SUNOS_VTOC_8)
2300 tmp_pinfo
.pinfo_map
[i
] = label
->dkl_map
[i
];
2302 #elif defined(_SUNOS_VTOC_16)
2303 tmp_pinfo
.pinfo_map
[i
].dkl_cylno
=
2304 label
->dkl_vtoc
.v_part
[i
].p_start
/ spc();
2305 tmp_pinfo
.pinfo_map
[i
].dkl_nblk
=
2306 label
->dkl_vtoc
.v_part
[i
].p_size
;
2308 #error No VTOC layout defined.
2309 #endif /* defined(_SUNOS_VTOC_8) */
2311 tmp_pinfo
.vtoc
= label
->dkl_vtoc
;
2314 fmt_print("Volume name = <%8s>\n", label
->dkl_vtoc
.v_volume
);
2315 fmt_print("ascii name = <%s>\n", id_str
);
2316 fmt_print("pcyl = %4d\n", label
->dkl_pcyl
);
2317 fmt_print("ncyl = %4d\n", label
->dkl_ncyl
);
2318 fmt_print("acyl = %4d\n", label
->dkl_acyl
);
2320 #if defined(_SUNOS_VTOC_16)
2321 fmt_print("bcyl = %4d\n", label
->dkl_bcyl
);
2322 #endif /* defined(_SUNOS_VTOC_16) */
2324 fmt_print("nhead = %4d\n", label
->dkl_nhead
);
2325 fmt_print("nsect = %4d\n", label
->dkl_nsect
);
2327 print_map(&tmp_pinfo
);
2334 * This command implements the inquiry command, for embedded SCSI
2335 * disks only, which issues a SCSI inquiry command, and
2336 * displays the resulting vendor, product id and revision level.
2342 struct scsi_inquiry
*inq
;
2346 inq
= (struct scsi_inquiry
*)inqbuf
;
2348 if (uscsi_inquiry(cur_file
, inqbuf
, sizeof (inqbuf
))) {
2349 err_print("Failed\n");
2352 fmt_print("Vendor: ");
2353 print_buf(inq
->inq_vid
, sizeof (inq
->inq_vid
));
2354 fmt_print("\nProduct: ");
2355 print_buf(inq
->inq_pid
, sizeof (inq
->inq_pid
));
2356 fmt_print("\nRevision: ");
2357 print_buf(inq
->inq_revision
, sizeof (inq
->inq_revision
));
2366 * This routine allows the user to set the 8-character
2367 * volume name in the vtoc. It then writes both the
2368 * primary and backup labels onto the current disk.
2377 char defvolname
[LEN_DKL_VVOL
+1];
2379 char s1
[MAXPATHLEN
], nclean
[MAXPATHLEN
];
2384 * There must be a current disk type (and therefore a current disk).
2386 if (cur_dtype
== NULL
) {
2387 err_print("Current Disk Type is not set.\n");
2391 * The current disk must be formatted to label it.
2393 if (!(cur_flags
& DISK_FORMATTED
)) {
2394 err_print("Current Disk is unformatted.\n");
2398 * Check for a valid fdisk table entry for Solaris
2400 if (!good_fdisk()) {
2404 * The current disk must be formatted to label it.
2406 if (cur_parts
== NULL
) {
2408 "Please select a partition map for the disk first.\n");
2413 * Check to see if there are any mounted file systems anywhere
2414 * on the current disk. If so, refuse to label the disk, but
2415 * only if the partitions would change for the mounted partitions.
2418 if (checkmount((diskaddr_t
)-1, (diskaddr_t
)-1)) {
2419 /* Bleagh, too descriptive */
2420 if (check_label_with_mount()) {
2422 "Cannot label disk while it has mounted partitions.\n\n");
2428 * Check to see if there are partitions being used for swapping
2429 * on the current disk. If so, refuse to label the disk, but
2430 * only if the partitions would change for the swap partitions.
2433 if (checkswap((diskaddr_t
)-1, (diskaddr_t
)-1)) {
2434 /* Bleagh, too descriptive */
2435 if (check_label_with_swap()) {
2437 "Cannot label disk while its partitions are currently \
2438 being used for swapping.\n\n");
2444 * Check to see if any partitions used for svm, vxvm, ZFS zpool
2445 * or live upgrade are on the disk. If so, refuse to label the
2446 * disk, but only if we are trying to shrink a partition in
2449 if (checkdevinuse(cur_disk
->disk_name
, (diskaddr_t
)-1,
2450 (diskaddr_t
)-1, 0, 1)) {
2451 err_print("Cannot label disk while its partitions "
2452 "are in use as described.\n");
2457 * Prompt for the disk volume name.
2459 prompt
= "Enter 8-character volume name (remember quotes)";
2460 bzero(x
.defvolname
, LEN_DKL_VVOL
+1);
2461 bcopy(cur_disk
->v_volume
, x
.defvolname
, LEN_DKL_VVOL
);
2463 * Get the input using "get_inputline" since
2464 * input would never return null string.
2466 fmt_print("%s[\"%s\"]:", prompt
, x
.defvolname
);
2469 * Get input from the user.
2471 get_inputline(nclean
, MAXPATHLEN
);
2472 clean_token(s1
, nclean
);
2477 volname
= x
.defvolname
;
2480 * remove the " mark from volname.
2485 while (s1
[i
] != '"' && s1
[i
] != '\0')
2488 clean_token(nclean
, volname
);
2491 (void) sscanf(&s1
[0], "%1024s", nclean
);
2496 * Make sure the user is serious.
2498 if (check("Ready to label disk, continue")) {
2503 * Use the volume name chosen above
2505 bzero(cur_disk
->v_volume
, LEN_DKL_VVOL
);
2506 bcopy(volname
, cur_disk
->v_volume
, min((int)strlen(volname
),
2508 if (cur_label
== L_TYPE_EFI
) {
2509 bzero(cur_parts
->etoc
->efi_parts
[8].p_name
, LEN_DKL_VVOL
);
2510 bcopy(volname
, cur_parts
->etoc
->efi_parts
[8].p_name
,
2514 * Write the labels out (this will also notify unix) and
2518 if (status
= write_label())
2519 err_print("Label failed.\n");