1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * rmf_menu.c :
28 * Command line options to rmformat are processed in this file.
29 */
30
31 #include "rmformat.h"
32 #include <sys/smedia.h>
33 #include <priv_utils.h>
34
35 extern int32_t D_flag;
36 extern int32_t e_flag;
37 extern int32_t H_flag;
38 extern int32_t U_flag;
39 extern int32_t V_flag;
40 extern int32_t b_flag;
41 extern int32_t w_flag;
42 extern int32_t W_flag;
43 extern int32_t s_flag;
71 extern void check_invalid_combinations();
72 extern void check_invalid_combinations_again(int32_t);
73 extern void process_options();
74 extern void get_passwd(struct smwp_state *wp, int32_t confirm);
75 extern int32_t valid_slice_file(smedia_handle_t, int32_t, char *,
76 struct extvtoc *);
77 extern void trap_SIGINT();
78 extern void release_SIGINT();
79 extern int32_t verify(smedia_handle_t handle, int32_t fd,
80 diskaddr_t start_sector, uint32_t nblocks,
81 char *buf, int32_t flag, int32_t blocksize, int32_t no_raw_rw);
82 extern void my_perror(char *err_string);
83 extern void write_default_label(smedia_handle_t, int32_t fd);
84 extern int find_device(int defer, char *tmpstr);
85
86 void overwrite_metadata(int32_t fd, smedia_handle_t handle);
87
88 int32_t write_sunos_label(int32_t fd, int32_t media_type);
89
90 int32_t my_open(char *device_name, int32_t flags);
91 int32_t check_and_unmount_vold(char *device_name, int32_t flag);
92 int32_t check_and_unmount_scsi(char *device_name, int32_t flag);
93
94 int32_t check_and_unmount_floppy(int32_t fd, int32_t flag);
95 int32_t get_confirmation(void);
96
97
98 static void process_F_flag(smedia_handle_t handle, int32_t fd);
99 static void process_w_flag(smedia_handle_t handle);
100 static void process_W_flag(smedia_handle_t handle);
101 static void process_R_flag(smedia_handle_t handle);
102 void process_p_flag(smedia_handle_t handle, int32_t fd);
103 static void process_c_flag(smedia_handle_t handle);
104 static void process_V_flag(smedia_handle_t handle, int32_t fd);
105 static void process_s_flag(smedia_handle_t, int32_t fd);
106 static void process_e_flag(smedia_handle_t handle);
107 static void process_H_flag(smedia_handle_t handle, int32_t fd);
108 static void process_D_flag(smedia_handle_t handle, int32_t fd);
109 static void process_b_flag(int32_t fd);
110 static void process_l_flag(void);
111
112 void
113 process_options()
114 {
115 int32_t fd;
116 smedia_handle_t handle;
117 int32_t m_scsi_umount = 0;
118 int32_t m_flp_umount = 0;
119 int32_t v_device_umount = 0;
120 int32_t umount_required = 0;
121 int32_t removable;
122 int32_t umount_failed = 0;
123 struct dk_minfo media;
124
125 check_invalid_combinations();
126
127 if (l_flag && !dev_name) {
128 process_l_flag();
129 return;
130 }
131
132 if (U_flag) {
133 if (!(F_flag || H_flag || D_flag)) {
134 F_flag = 1;
135 long_format = 1;
136 }
137 }
152 PERROR("DKIOCREMOVABLE ioctl failed");
153 (void) close(fd);
154 exit(1);
155 }
156 if (!removable) {
157 (void) fprintf(stderr,
158 gettext("Not a removable media device\n"));
159 (void) close(fd);
160 exit(1);
161 }
162
163 if (ioctl(fd, DKIOCGMEDIAINFO, &media) < 0) {
164 (void) fprintf(stderr,
165 gettext("No media in specified device\n"));
166 (void) close(fd);
167 exit(1);
168 }
169
170 /* Check if volume manager has mounted this */
171 if (umount_required) {
172 v_device_umount = check_and_unmount_vold(dev_name, U_flag);
173 if (v_device_umount != 1) {
174 m_scsi_umount = check_and_unmount_scsi(dev_name,
175 U_flag);
176 if (m_scsi_umount != 1) {
177 m_flp_umount = check_and_unmount_floppy(fd,
178 U_flag);
179 if (m_flp_umount != 1) {
180 umount_failed = 1;
181 }
182 }
183 }
184 }
185
186 if (umount_required && U_flag && umount_failed) {
187 if (v_device_umount || m_scsi_umount || m_flp_umount) {
188 (void) fprintf(stderr,
189 gettext("Could not unmount device.\n"));
190 (void) close(fd);
191 exit(1);
192 }
193 }
194
195 if (umount_required && !U_flag) {
196 if (v_device_umount || m_scsi_umount || m_flp_umount) {
197 (void) fprintf(stderr, gettext("Device mounted.\n"));
198 (void) fprintf(stderr,
199 gettext("Requested operation can not be \
200 performed on a mounted device.\n"));
201 (void) close(fd);
202 exit(1);
203 }
204 }
291 if (l_flag) {
292 process_l_flag();
293 }
294
295 (void) smedia_release_handle(handle);
296 (void) close(fd);
297 }
298
299 /*
300 * This routine handles the F_flag.
301 * This options should not be used for floppy. However,
302 * if this option is used for floppy, the option will
303 * be forced to SM_FORMAT_HD and smedia_format is called.
304 * Note that smedia_format is a blocked mode format and it
305 * returns only after the complete formatting is over.
306 */
307
308 static void
309 process_F_flag(smedia_handle_t handle, int32_t fd)
310 {
311 uint32_t format_flag;
312 int32_t old_per = 0;
313 int32_t new_per, ret_val;
314
315 if (force_format) {
316 (void) fprintf(stderr,
317 gettext("Formatting disk.\n"));
318 } else {
319 (void) fprintf(stderr,
320 gettext("Formatting will erase all the data on disk.\n"));
321 if (!get_confirmation())
322 return;
323 }
324
325 if (quick_format)
326 format_flag = SM_FORMAT_QUICK;
327 else if (long_format)
328 format_flag = SM_FORMAT_LONG;
329 else if (force_format)
330 format_flag = SM_FORMAT_FORCE;
331
398
399 if (quick_format)
400 overwrite_metadata(fd, handle);
401
402 (void) write_default_label(handle, fd);
403 }
404
405 release_SIGINT();
406 }
407
408 /*
409 * List removable devices.
410 */
411 static void
412 process_l_flag()
413 {
414 int retry;
415 int removable;
416 int total_devices_found_last_time;
417 int defer = 0;
418 char *tmpstr;
419
420 #define MAX_RETRIES_FOR_SCANNING 3
421
422 vol_running = volmgt_running();
423 if (vol_running)
424 defer = 1;
425 (void) printf(gettext("Looking for devices...\n"));
426 total_devices_found_last_time = 0;
427
428 /*
429 * Strip out any leading path. For example, /dev/rdsk/c3t0d0s2
430 * will result in tmpstr = c3t0d0s2. dev_name is given as input
431 * argument.
432 */
433 if (dev_name) {
434 if ((tmpstr = strrchr(dev_name, '/')) != NULL) {
435 tmpstr += sizeof (char);
436 } else {
437 tmpstr = dev_name;
438 }
|
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 *
21 *
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 *
25 * Copyright 2014 Andrew Stormont.
26 */
27
28 /*
29 * rmf_menu.c :
30 * Command line options to rmformat are processed in this file.
31 */
32
33 #include "rmformat.h"
34 #include <sys/smedia.h>
35 #include <priv_utils.h>
36
37 extern int32_t D_flag;
38 extern int32_t e_flag;
39 extern int32_t H_flag;
40 extern int32_t U_flag;
41 extern int32_t V_flag;
42 extern int32_t b_flag;
43 extern int32_t w_flag;
44 extern int32_t W_flag;
45 extern int32_t s_flag;
73 extern void check_invalid_combinations();
74 extern void check_invalid_combinations_again(int32_t);
75 extern void process_options();
76 extern void get_passwd(struct smwp_state *wp, int32_t confirm);
77 extern int32_t valid_slice_file(smedia_handle_t, int32_t, char *,
78 struct extvtoc *);
79 extern void trap_SIGINT();
80 extern void release_SIGINT();
81 extern int32_t verify(smedia_handle_t handle, int32_t fd,
82 diskaddr_t start_sector, uint32_t nblocks,
83 char *buf, int32_t flag, int32_t blocksize, int32_t no_raw_rw);
84 extern void my_perror(char *err_string);
85 extern void write_default_label(smedia_handle_t, int32_t fd);
86 extern int find_device(int defer, char *tmpstr);
87
88 void overwrite_metadata(int32_t fd, smedia_handle_t handle);
89
90 int32_t write_sunos_label(int32_t fd, int32_t media_type);
91
92 int32_t my_open(char *device_name, int32_t flags);
93
94 int32_t check_and_unmount_scsi(char *device_name, int32_t flag);
95 int32_t check_and_unmount_floppy(int32_t fd, int32_t flag);
96 int32_t get_confirmation(void);
97
98
99 static void process_F_flag(smedia_handle_t handle, int32_t fd);
100 static void process_w_flag(smedia_handle_t handle);
101 static void process_W_flag(smedia_handle_t handle);
102 static void process_R_flag(smedia_handle_t handle);
103 void process_p_flag(smedia_handle_t handle, int32_t fd);
104 static void process_c_flag(smedia_handle_t handle);
105 static void process_V_flag(smedia_handle_t handle, int32_t fd);
106 static void process_s_flag(smedia_handle_t, int32_t fd);
107 static void process_e_flag(smedia_handle_t handle);
108 static void process_H_flag(smedia_handle_t handle, int32_t fd);
109 static void process_D_flag(smedia_handle_t handle, int32_t fd);
110 static void process_b_flag(int32_t fd);
111 static void process_l_flag(void);
112
113 void
114 process_options()
115 {
116 int32_t fd;
117 smedia_handle_t handle;
118 int32_t m_flp_umount = 0;
119 int32_t v_device_umount = 0;
120 int32_t umount_required = 0;
121 int32_t removable;
122 int32_t umount_failed = 0;
123 struct dk_minfo media;
124
125 check_invalid_combinations();
126
127 if (l_flag && !dev_name) {
128 process_l_flag();
129 return;
130 }
131
132 if (U_flag) {
133 if (!(F_flag || H_flag || D_flag)) {
134 F_flag = 1;
135 long_format = 1;
136 }
137 }
152 PERROR("DKIOCREMOVABLE ioctl failed");
153 (void) close(fd);
154 exit(1);
155 }
156 if (!removable) {
157 (void) fprintf(stderr,
158 gettext("Not a removable media device\n"));
159 (void) close(fd);
160 exit(1);
161 }
162
163 if (ioctl(fd, DKIOCGMEDIAINFO, &media) < 0) {
164 (void) fprintf(stderr,
165 gettext("No media in specified device\n"));
166 (void) close(fd);
167 exit(1);
168 }
169
170 /* Check if volume manager has mounted this */
171 if (umount_required) {
172 v_device_umount = check_and_unmount_scsi(dev_name, U_flag);
173 if (v_device_umount != 1) {
174 m_flp_umount = check_and_unmount_floppy(fd, U_flag);
175 if (m_flp_umount != 1) {
176 umount_failed = 1;
177 }
178 }
179 }
180
181 if (umount_required && U_flag && umount_failed) {
182 if (v_device_umount || m_scsi_umount || m_flp_umount) {
183 (void) fprintf(stderr,
184 gettext("Could not unmount device.\n"));
185 (void) close(fd);
186 exit(1);
187 }
188 }
189
190 if (umount_required && !U_flag) {
191 if (v_device_umount || m_scsi_umount || m_flp_umount) {
192 (void) fprintf(stderr, gettext("Device mounted.\n"));
193 (void) fprintf(stderr,
194 gettext("Requested operation can not be \
195 performed on a mounted device.\n"));
196 (void) close(fd);
197 exit(1);
198 }
199 }
286 if (l_flag) {
287 process_l_flag();
288 }
289
290 (void) smedia_release_handle(handle);
291 (void) close(fd);
292 }
293
294 /*
295 * This routine handles the F_flag.
296 * This options should not be used for floppy. However,
297 * if this option is used for floppy, the option will
298 * be forced to SM_FORMAT_HD and smedia_format is called.
299 * Note that smedia_format is a blocked mode format and it
300 * returns only after the complete formatting is over.
301 */
302
303 static void
304 process_F_flag(smedia_handle_t handle, int32_t fd)
305 {
306 uint32_t format_flag = 0;
307 int32_t old_per = 0;
308 int32_t new_per, ret_val;
309
310 if (force_format) {
311 (void) fprintf(stderr,
312 gettext("Formatting disk.\n"));
313 } else {
314 (void) fprintf(stderr,
315 gettext("Formatting will erase all the data on disk.\n"));
316 if (!get_confirmation())
317 return;
318 }
319
320 if (quick_format)
321 format_flag = SM_FORMAT_QUICK;
322 else if (long_format)
323 format_flag = SM_FORMAT_LONG;
324 else if (force_format)
325 format_flag = SM_FORMAT_FORCE;
326
393
394 if (quick_format)
395 overwrite_metadata(fd, handle);
396
397 (void) write_default_label(handle, fd);
398 }
399
400 release_SIGINT();
401 }
402
403 /*
404 * List removable devices.
405 */
406 static void
407 process_l_flag()
408 {
409 int retry;
410 int removable;
411 int total_devices_found_last_time;
412 int defer = 0;
413 char *tmpstr = NULL;
414
415 #define MAX_RETRIES_FOR_SCANNING 3
416
417 vol_running = volmgt_running();
418 if (vol_running)
419 defer = 1;
420 (void) printf(gettext("Looking for devices...\n"));
421 total_devices_found_last_time = 0;
422
423 /*
424 * Strip out any leading path. For example, /dev/rdsk/c3t0d0s2
425 * will result in tmpstr = c3t0d0s2. dev_name is given as input
426 * argument.
427 */
428 if (dev_name) {
429 if ((tmpstr = strrchr(dev_name, '/')) != NULL) {
430 tmpstr += sizeof (char);
431 } else {
432 tmpstr = dev_name;
433 }
|