]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Unix/ventoy_unix_src/DragonFly/oinit.c
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Donn Seeley at Berkeley Software Design, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)init.c 8.1 (Berkeley) 7/15/93
34 * $FreeBSD: src/sbin/init/init.c,v 1.38.2.8 2001/10/22 11:27:32 des Exp $
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/mount.h>
40 #include <sys/sysctl.h>
57 #include <sys/reboot.h>
58 #include <sys/mount.h>
66 int prepare_dmtable(void);
67 int mount_cd9660(char *dev
, char *dir
);
68 int mount_null(const char *src
, const char *dst
);
69 int mount_tmpfs(int argc
, char *argv
[]);
71 static int setctty(const char *name
)
76 if ((fd
= open(name
, O_RDWR
)) == -1) {
80 if (login_tty(fd
) == -1) {
87 static void ventoy_init(char **argv_orig
)
91 char arg0
[MAXPATHLEN
];
92 char arg1
[MAXPATHLEN
];
93 char arg2
[MAXPATHLEN
];
97 /* step1: mount tmpfs */
98 vdebug("[VTOY] step 1: mount tmpfs ...");
99 strcpy(arg0
, "mount_tmpfs");
100 strcpy(arg1
, "tmpfs");
101 strcpy(arg2
, "/tmp");
106 error
= mount_tmpfs(3, argv
);
107 vdebug(" %d\n", error
);
109 /* step 2: prepare dmtable */
110 vdebug("[VTOY] step 2: prepare device-mapper table...\n");
111 (void)prepare_dmtable();
113 /* step 3: create device mapper */
114 vdebug("[VTOY] step 3: create device-mapper ...\n");
115 if ((pid
= fork()) == 0) {
116 sigemptyset(&sa
.sa_mask
);
118 sa
.sa_handler
= SIG_IGN
;
119 sigaction(SIGTSTP
, &sa
, NULL
);
120 sigaction(SIGHUP
, &sa
, NULL
);
125 argv
[3] = "/tmp/dmtable";
126 argv
[4] = "--readonly";
129 sigprocmask(SIG_SETMASK
, &sa
.sa_mask
, NULL
);
130 execv("/sbin/dmsetup", __DECONST(char **, argv
));
131 exit(1); /* force single user mode */
135 wpid
= waitpid(-1, &status
, WUNTRACED
);
136 } while (wpid
!= pid
);
138 /* step 4: mount iso */
139 vdebug("[VTOY] step 4: mount device-mapper ...");
140 strcpy(arg0
, "/dev/mapper/ventoy");
141 strcpy(arg1
, "/new_root");
142 error
= mount_cd9660(arg0
, arg1
);
143 vdebug(" %d\n", error
);
145 /* step 5: mount devfs */
146 vdebug("[VTOY] step 5: mount devfs ...");
147 strcpy(arg0
, "/dev");
148 strcpy(arg1
, "/new_root/dev");
149 mount_null(arg0
, arg1
);
150 vdebug(" %d\n", error
);
152 /* step 6: umount tmpfs */
153 error
= unmount("/tmp", 0);
154 vdebug("[VTOY] step 6: unmount tmpfs %d\n", error
);
156 /* step 7: swich_root */
157 vdebug("[VTOY] step 7: switch root ...\n");
158 error
= chdir("/new_root");
161 printf("[VTOY] chdir /new_root failed %d\n", error
);
165 error
= chroot_kernel("/new_root");
168 printf("[VTOY] chroot_kernel /new_root failed %d\n", error
);
172 error
= chroot("/new_root");
175 printf("[VTOY] chroot /new_root failed %d\n", error
);
179 vdebug("[VTOY] step 8: now run /sbin/init ...\n");
180 execv("/sbin/init", __DECONST(char **, argv_orig
));
182 /* We failed to exec /sbin/init in the chroot, sleep forever */
184 printf("[VTOY] ################### DEAD ################\n");
190 int main(int argc __unused
, char **argv
)
192 size_t varsize
= sizeof(int);
194 /* Dispose of random users. */
196 errx(1, "%s", strerror(EPERM
));
198 /* Init is not allowed to die, it would make the kernel panic */
199 signal(SIGTERM
, SIG_IGN
);
201 setctty(_PATH_CONSOLE
);
203 sysctlbyname("debug.bootverbose", &boot_verbose
, &varsize
, NULL
, 0);
205 vdebug("======= Ventoy Init Start ========\n");