2 * Copyright IBM Corp 2008
3 * Author: Hans-Joachim Picht <hans@linux.vnet.ibm.com>
5 * Linux for System z shutdown action
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/types.h>
34 int use_ccw
= 1; /* default is a ccw device */
35 char loadparm
[9]; /* the entry in the boot menu */
37 int verbose
; /* default: don't be verbose */
38 int bootprog
; /* default bootprog value is 0 */
40 char wwpn
[20]; /* 18 character +0x" */
42 char lun
[20]; /* 18 character +0x" */
44 char devno
[9]; /* device number e.g. 0.0.4711 */
46 char device
[9]; /* the device itself, e.g. sda1 */
47 char partition
[15]; /* partition, e.g. /dev/dasda1 */
49 char devparent
[9]; /* the device a partion belongs to. e.g. dasda */
50 char saction
[8]; /* the shutdown action */
51 char name
[256]; /* program name */
52 int action
; /* either CCW, FCP or NODE */
55 * "main" function for the lsreipl related stuff
57 int lsreipl(int argc
, char *argv
[])
60 char bootprog
[1024], lba
[1024], val
[9];
62 /* parse the command line options in getop.c */
63 parse_lsreipl_options(argc
, argv
);
65 rc
= get_reipl_type();
67 printf("Re-IPL type: fcp\n");
68 rc
= strrd(wwpn
, "/sys/firmware/reipl/fcp/wwpn");
71 rc
= strrd(lun
, "/sys/firmware/reipl/fcp/lun");
74 rc
= strrd(devno
, "/sys/firmware/reipl/fcp/device");
77 rc
= strrd(bootprog
, "/sys/firmware/reipl/fcp/bootprog");
80 rc
= strrd(lba
, "/sys/firmware/reipl/fcp/br_lba");
84 printf("WWPN: %s\n", wwpn
);
86 printf("LUN: %s\n", lun
);
87 if (strlen(devno
) > 0)
88 printf("Device: %s\n", devno
);
89 if (strlen(bootprog
) > 0)
90 printf("bootprog: %s\n", bootprog
);
92 printf("br_lba: %s\n", lba
);
95 printf("Re-IPL type: ccw\n");
96 rc
= strrd(devno
, "/sys/firmware/reipl/ccw/device");
99 if (strlen(devno
) > 0)
100 printf("Device: %s\n", devno
);
102 * check if we can read the load parameter (Loadparm)
104 rc
= strrd(val
, "/sys/firmware/reipl/ccw/loadparm");
106 printf("Loadparm: %s\n", val
);
108 printf("Loadparm: \n");
114 * "main" function for the reipl related stuff
117 int reipl(int argc
, char *argv
[])
124 /* parse the command line options in getop.c */
125 parse_options(argc
, argv
);
127 * in case we want to reipl from a ccw device
130 if (action
== ACT_NODE
) {
131 rc
= get_ccw_dev(partition
, device
);
133 fprintf(stderr
, "%s: Cannot find device for "
134 "partition: %s\n", name
, partition
);
137 rc
= get_ccw_devno(device
, devno
);
139 fprintf(stderr
, "%s: Unable to lookup device"
140 " number for device %s\n", name
,
145 if (isccwdev(devno
) != 0) {
146 fprintf(stderr
, "%s: Unable to find a valid ccw"
147 " device number\n", name
);
150 rc
= strwrt(devno
, "/sys/firmware/reipl/ccw/device");
152 fprintf(stderr
, "%s: Failed to set ccw device "
154 rc
= strwrt("ccw", "/sys/firmware/reipl/reipl_type");
156 fprintf(stderr
, "%s: Failed to set reipl type "
158 printf("Settings changed to: %s\n", devno
);
159 printf("Re-IPL type: ccw\n");
160 if (strlen(loadparm
) != 0) {
161 rc
= strwrt(loadparm
,
162 "/sys/firmware/reipl/ccw/loadparm");
165 "/sys/firmware/reipl/ccw/loadparm");
168 fprintf(stderr
, "%s: Failed to set "
170 printf("Loadparm: %s\n", loadparm
);
173 * in case we reipl from a scsi device
177 * detect the necessary settings based on the device file:
178 * device number, lun and wwpn
180 if (action
== ACT_NODE
) {
181 /* get the device from the partition */
182 rc
= get_fcp_dev(partition
, device
);
184 fprintf(stderr
, "%s Cannot find device for"
185 " partition: %s\n", name
, partition
);
188 if (ispartition(device
) != 0) {
189 fprintf(stderr
, "%s: %s is not a valid "
190 "partition\n", name
, partition
);
194 printf("device: found %s\n", device
);
195 sprintf(path
, "/sys/block/%s/device", device
);
197 printf("path is %s\n", path
);
198 if (chdir(path
) != 0) {
199 fprintf(stderr
, "%s: Cannot find required"
200 " data related to device %s\n",
204 rc
= get_wwpn(device
, wwpn
);
206 fprintf(stderr
, "%s: Failed to lookup "
210 rc
= get_lun(device
, lun
);
212 fprintf(stderr
, "%s: Failed to lookup "
216 rc
= get_fcp_devno(device
, devno
);
218 fprintf(stderr
, "%s: Cannot find device for "
219 "partition: %s\n", name
, partition
);
223 printf("Device: %s\n", devno
);
225 if (isfcpdev(devno
) != 0) {
226 fprintf(stderr
, "%s: %s is not a valid fcp device"
227 " number.\n", name
, devno
);
230 rc
= strwrt(devno
, "/sys/firmware/reipl/fcp/device");
232 fprintf(stderr
, "%s: Failed to set fcp device "
236 rc
= strwrt(wwpn
, "/sys/firmware/reipl/fcp/wwpn");
238 fprintf(stderr
, "%s: Failed to set WWPN\n", name
);
241 rc
= strwrt(lun
, "/sys/firmware/reipl/fcp/lun");
243 fprintf(stderr
, "%s: Failed to set fcp LUN\n", name
);
246 rc
= strwrt("fcp", "/sys/firmware/reipl/reipl_type");
248 fprintf(stderr
, "%s: Failed to set reipl type to"
253 * set the boot record logical block address. Master boot
254 * record. It is always 0 for Linux
256 rc
= intwrt(0, "/sys/firmware/reipl/fcp/br_lba");
258 fprintf(stderr
, "%s: Failed to set boot record "
259 "logical address to 0\n", name
);
262 rc
= intwrt(bootprog
, "/sys/firmware/reipl/fcp/bootprog");
264 fprintf(stderr
, "%s: Failed to set "
268 printf("Settings changed to: %s\n", devno
);
269 printf("WWPN: %s\n", wwpn
);
270 printf("LUN: %s\n", lun
);
272 printf("Bootprog: %d\n", bootprog
);
273 printf("Re-IPL type: fcp\n");
277 /* "main" function for list shutdown actions */
278 int lsshut(int argc
, char *argv
[])
284 parse_lsshut_options(argc
, argv
);
285 printf("Trigger Action\n");
286 printf("========================\n");
287 if (access("/sys/firmware/shutdown_actions/on_halt", R_OK
) == 0) {
288 rc
= get_sa(tmp
, "on_halt");
290 if (strncmp(tmp
, "vmcmd", strlen("vmcmd")) == 0) {
291 rc
= strrd(cmd
, "/sys/firmware/vmcmd/on_halt");
294 printf("Halt %s (\"%s\")\n", tmp
,
297 printf("Halt %s\n", tmp
);
301 if (access("/sys/firmware/shutdown_actions/on_panic", R_OK
) == 0) {
302 rc
= get_sa(tmp
, "on_panic");
304 if (strncmp(tmp
, "vmcmd", strlen("vmcmd")) == 0) {
305 rc
= strrd(cmd
, "/sys/firmware/vmcmd/on_panic");
308 printf("Panic %s (\"%s\")\n", tmp
,
311 printf("Panic %s\n", tmp
);
314 if (access("/sys/firmware/shutdown_actions/on_poff", R_OK
) == 0) {
315 rc
= get_sa(tmp
, "on_poff");
317 if (strncmp(tmp
, "vmcmd", strlen("vmcmd")) == 0) {
318 rc
= strrd(cmd
, "/sys/firmware/vmcmd/on_poff");
321 printf("Power off %s (\"%s\")\n", tmp
,
324 printf("Power off %s\n", tmp
);
327 if (access("/sys/firmware/shutdown_actions/on_reboot", R_OK
) == 0) {
328 rc
= get_sa(tmp
, "on_reboot");
330 if (strncmp(tmp
, "vmcmd", strlen("vmcmd")) == 0) {
331 rc
= strrd(cmd
, "/sys/firmware/vmcmd/"
335 printf("Reboot %s (\"%s\")\n", tmp
,
338 printf("Reboot %s\n", tmp
);
344 /* "main" function for shutdown actions */
345 int chshut(int argc
, char *argv
[])
347 parse_shutdown_options(argc
, argv
);
351 int main(int argc
, char *argv
[])
354 strcpy(name
, argv
[0]);
355 if (check_for_root() != 0) {
356 fprintf(stderr
, "%s: You must be root to perform this "
357 "operation\n", name
);
360 /* lets see how we are called */
361 if (strstr(argv
[0], "chreipl") != NULL
) {
365 if (strstr(argv
[0], "chshut") != NULL
) {
369 if (strstr(argv
[0], "lsreipl") != NULL
) {
373 if (strstr(argv
[0], "lsshut") != NULL
) {
377 fprintf(stderr
, "%s: Dont know how we are called.\n", name
);