diff options
author | Werner Fink | 2012-12-10 13:27:10 +0100 |
---|---|---|
committer | Karel Zak | 2012-12-18 15:45:33 +0100 |
commit | ab6478ef2d51215441baa47fe1cb0e6aa6ba1c27 (patch) | |
tree | bb776ec1acbe3c0e25c857eed7f5c77388342d82 /lib/consoles.c | |
parent | sulogin: use alarm function to indicate if a timeout occurs (diff) | |
download | kernel-qcow2-util-linux-ab6478ef2d51215441baa47fe1cb0e6aa6ba1c27.tar.gz kernel-qcow2-util-linux-ab6478ef2d51215441baa47fe1cb0e6aa6ba1c27.tar.xz kernel-qcow2-util-linux-ab6478ef2d51215441baa47fe1cb0e6aa6ba1c27.zip |
sulogin: mount temporary /dev and /proc if not found
This is very usefull if initrd can not loaded that is no /dev and no
/proc is found. Also if the /etc/shadow and /etc/passwd is copied into
the initrd the sulogin can be used in initrd even before /dev and/or /proc
are mounted.
Signed-off-by: Werner Fink <werner@suse.de>
Diffstat (limited to 'lib/consoles.c')
-rw-r--r-- | lib/consoles.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/lib/consoles.c b/lib/consoles.c index 21c5a249a..7175a0882 100644 --- a/lib/consoles.c +++ b/lib/consoles.c @@ -39,6 +39,16 @@ #include <dirent.h> #include <unistd.h> +#ifdef USE_SULOGIN_EMERGENCY_MOUNT +# include <sys/mount.h> +# include <linux/fs.h> +# include <linux/magic.h> +# include <linux/major.h> +# ifndef MNT_DETACH +# define MNT_DETACH 2 +# endif +#endif + #include "c.h" #include "canonicalize.h" #include "consoles.h" @@ -77,6 +87,64 @@ dbgprint(const char *mesg, ...) fputc('\n', stderr); } +#ifdef USE_SULOGIN_EMERGENCY_MOUNT +/* + * Make C library standard calls such like ttyname(3) work + * even if the system does not show any of the standard + * directories. + */ + +static uint32_t emergency_flags; +# define MNT_PROCFS 0x0001 +# define MNT_DEVTMPFS 0x0002 + +static __attribute__((__destructor__)) +void emergency_do_umounts(void) +{ + if (emergency_flags & MNT_DEVTMPFS) + umount2("/dev", MNT_DETACH); + if (emergency_flags & MNT_PROCFS) + umount2("/proc", MNT_DETACH); +} + +static __attribute__((__constructor__)) +void emergency_do_mounts(void) +{ + struct stat rt, xt; + + if (emergency_flags) { + emergency_flags = 0; + return; + } + + if (stat("/", &rt) != 0) { + warn("can not get file status of root file system\n"); + return; + } + + if (stat("/proc", &xt) == 0 + && rt.st_dev == xt.st_dev + && mount("proc", "/proc", "proc", MS_RELATIME, NULL) == 0) + emergency_flags |= MNT_PROCFS; + + if (stat("/dev", &xt) == 0 + && rt.st_dev == xt.st_dev + && mount("devtmpfs", "/dev", "devtmpfs", + MS_RELATIME, "mode=0755,nr_inodes=0") == 0) { + + emergency_flags |= MNT_DEVTMPFS; + mknod("/dev/console", S_IFCHR|S_IRUSR|S_IWUSR, + makedev(TTYAUX_MAJOR, 1)); + + if (symlink("/proc/self/fd", "/dev/fd") == 0) { + ignore_result( symlink("fd/0", "/dev/stdin") ); + ignore_result( symlink("fd/1", "/dev/stdout") ); + ignore_result( symlink("fd/2", "/dev/stderr") ); + } + } +} +#endif /* USE_SULOGIN_EMERGENCY_MOUNT */ + /* * Read and allocate one line from file, * the caller has to free the result @@ -182,6 +250,11 @@ char* scandev(DIR *dir, dev_t comparedev) continue; if ((size_t)snprintf(path, sizeof(path), "/dev/%s", dent->d_name) >= sizeof(path)) continue; +#ifdef USE_SULOGIN_EMERGENCY_MOUNT + if (emergency_flags & MNT_DEVTMPFS) + mknod(path, S_IFCHR|S_IRUSR|S_IWUSR, comparedev); +#endif + name = canonicalize_path(path); break; } |