1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2022 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
22 #include <linux/limits.h>
24 #include <sys/mount.h>
26 #include <sys/sysmacros.h>
27 #include <sys/types.h>
29 #include <pakfire/arch.h>
30 #include <pakfire/logging.h>
31 #include <pakfire/pakfire.h>
32 #include <pakfire/parse.h>
33 #include <pakfire/path.h>
34 #include <pakfire/mount.h>
35 #include <pakfire/string.h>
36 #include <pakfire/util.h>
38 static const struct pakfire_mountpoint
{
46 // Mount a new instance of /proc
48 PAKFIRE_MNTNS_INNER
|PAKFIRE_MNTNS_OUTER
,
52 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
56 // Make /proc/sys read-only (except /proc/sys/net)
78 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
,
82 // Deny write access to /proc/sysrq-trigger (can be used to restart the host)
85 "/proc/sysrq-trigger",
93 "/proc/sysrq-trigger",
96 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
,
100 // Make /proc/irq read-only
114 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
,
118 // Make /proc/bus read-only
132 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
,
136 // Bind-Mount /sys ready-only
150 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
,
161 "mode=0755,size=4m,nr_inodes=64k",
169 "newinstance,ptmxmode=0666,mode=620",
172 // Create a new /dev/shm
178 MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
179 "mode=1777,size=1024m",
188 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
198 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
199 "mode=755,size=256m,nr_inodes=1k",
208 MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
209 "mode=1777,size=4096m",
216 static const struct pakfire_devnode
{
223 { "/dev/null", 1, 3, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
224 { "/dev/zero", 1, 5, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
225 { "/dev/full", 1, 7, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
226 { "/dev/random", 1, 8, S_IFCHR
|S_IRUSR
|S_IRGRP
|S_IROTH
, 0 },
227 { "/dev/urandom", 1, 9, S_IFCHR
|S_IRUSR
|S_IRGRP
|S_IROTH
, 0 },
228 { "/dev/kmsg", 1, 11, S_IFCHR
|S_IRUSR
|S_IRGRP
|S_IROTH
, 0 },
229 { "/dev/tty", 5, 0, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
230 { "/dev/console", 5, 1, S_IFCHR
|S_IRUSR
|S_IWUSR
, 0 },
231 { "/dev/rtc0", 252, 0, S_IFCHR
|S_IRUSR
|S_IWUSR
, 0 },
234 { "/dev/loop-control", 10, 237, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
235 { "/dev/loop0", 7, 0, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
236 { "/dev/loop1", 7, 1, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
237 { "/dev/loop2", 7, 2, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
238 { "/dev/loop3", 7, 3, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
239 { "/dev/loop4", 7, 4, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
240 { "/dev/loop5", 7, 5, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
241 { "/dev/loop6", 7, 6, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
242 { "/dev/loop7", 7, 7, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
247 static const struct pakfire_symlink
{
251 { "/dev/pts/ptmx", "/dev/ptmx", },
252 { "/proc/self/fd", "/dev/fd", },
253 { "/proc/self/fd/0", "/dev/stdin" },
254 { "/proc/self/fd/1", "/dev/stdout" },
255 { "/proc/self/fd/2", "/dev/stderr" },
256 { "/proc/kcore", "/dev/core" },
260 int pakfire_mount_change_propagation(struct pakfire_ctx
* ctx
, const char* path
, int propagation
) {
261 CTX_DEBUG(ctx
, "Changing mount propagation on %s\n", path
);
263 int r
= mount(NULL
, path
, NULL
, propagation
|MS_REC
, NULL
);
265 CTX_ERROR(ctx
, "Failed to change mount propagation on %s: %m\n", path
);
270 static int pakfire_mount_is_mountpoint(struct pakfire
* pakfire
, const char* path
) {
271 // XXX THIS STILL NEEDS TO BE IMPLEMENTED
275 int pakfire_mount_make_mounpoint(struct pakfire
* pakfire
, const char* path
) {
278 // Check if path already is a mountpoint
279 r
= pakfire_mount_is_mountpoint(pakfire
, path
);
281 // Already is a mountpoint
285 // Is not a mountpoint
290 ERROR(pakfire
, "Could not determine whether %s is a mountpoint: %m\n", path
);
294 // Bind-mount to self
295 r
= mount(path
, path
, NULL
, MS_BIND
|MS_REC
, NULL
);
297 ERROR(pakfire
, "Could not make %s a mountpoint: %m\n", path
);
304 static int pakfire_mount(struct pakfire
* pakfire
, const char* source
, const char* target
,
305 const char* fstype
, unsigned long mflags
, const void* data
) {
306 const char* options
= (const char*)data
;
308 // Check for some basic inputs
309 if (!source
|| !target
) {
314 DEBUG(pakfire
, "Mounting %s from %s (%s - %s)\n", target
, source
, fstype
, options
);
317 int r
= mount(source
, target
, fstype
, mflags
, data
);
319 ERROR(pakfire
, "Could not mount %s: %m\n", target
);
325 static int __pakfire_mount_list(char* line
, size_t length
, void* data
) {
326 struct pakfire_ctx
* ctx
= data
;
328 // Send the line to the logger
329 CTX_DEBUG(ctx
, " %.*s", (int)length
, line
);
334 int pakfire_mount_list(struct pakfire_ctx
* ctx
) {
335 CTX_DEBUG(ctx
, "Mountpoints:\n");
337 return pakfire_parse_file("/proc/self/mounts", __pakfire_mount_list
, ctx
);
340 int pakfire_populate_dev(struct pakfire
* pakfire
, int flags
) {
343 // Create device nodes
344 for (const struct pakfire_devnode
* devnode
= devnodes
; devnode
->path
; devnode
++) {
345 DEBUG(pakfire
, "Creating device node %s\n", devnode
->path
);
347 // Check if flags match
348 if (devnode
->flags
&& !(flags
& devnode
->flags
))
351 int r
= pakfire_path(pakfire
, path
, "%s", devnode
->path
);
355 dev_t dev
= makedev(devnode
->major
, devnode
->minor
);
357 r
= mknod(path
, devnode
->mode
, dev
);
359 // Continue if mknod was successful
363 // If we could not create the device node because of permission issues,
364 // it might be likely that we are running in a user namespace where creating
365 // device nodes is not permitted. Try bind-mounting them.
369 // Otherwise log an error and end
370 ERROR(pakfire
, "Could not create %s: %m\n", devnode
->path
);
374 // Create an empty file
375 r
= pakfire_touch(path
, 0444);
377 ERROR(pakfire
, "Could not create %s: %m\n", path
);
381 // Create a bind-mount over the file
382 r
= pakfire_mount(pakfire
, devnode
->path
, path
, "bind", MS_BIND
, NULL
);
388 for (const struct pakfire_symlink
* s
= symlinks
; s
->target
; s
++) {
389 DEBUG(pakfire
, "Creating symlink %s -> %s\n", s
->path
, s
->target
);
391 int r
= pakfire_path(pakfire
, path
, "%s", s
->path
);
395 r
= symlink(s
->target
, path
);
397 ERROR(pakfire
, "Could not create symlink %s: %m\n", s
->path
);
405 int pakfire_mount_interpreter(struct pakfire
* pakfire
) {
406 char target
[PATH_MAX
];
408 // Fetch the target architecture
409 const char* arch
= pakfire_get_effective_arch(pakfire
);
411 // Can we emulate this architecture?
412 char* interpreter
= pakfire_arch_find_interpreter(arch
);
414 // No interpreter required
418 DEBUG(pakfire
, "Mounting interpreter %s for %s\n", interpreter
, arch
);
420 // Where to mount this?
421 int r
= pakfire_path(pakfire
, target
, "%s", interpreter
);
426 r
= pakfire_mkparentdir(target
, 0755);
430 // Create an empty file
431 FILE* f
= fopen(target
, "w");
436 r
= pakfire_mount(pakfire
, interpreter
, target
, NULL
, MS_BIND
|MS_RDONLY
, NULL
);
438 ERROR(pakfire
, "Could not mount interpreter %s to %s: %m\n", interpreter
, target
);
443 int pakfire_mount_all(struct pakfire
* pakfire
, pakfire_mntns_t ns
, int flags
) {
444 char target
[PATH_MAX
];
447 const char* root
= "/";
449 // Fetch Pakfire's root directory
450 if (ns
== PAKFIRE_MNTNS_OUTER
)
451 root
= pakfire_get_path(pakfire
);
453 for (const struct pakfire_mountpoint
* mp
= mountpoints
; mp
->source
; mp
++) {
457 // Figure out where to mount
458 r
= pakfire_path_append(target
, root
, mp
->target
);
462 // Create target if it doesn't exist
463 if (!pakfire_path_exists(target
)) {
464 r
= pakfire_mkdir(target
, 0755);
466 ERROR(pakfire
, "Could not create %s: %m\n", target
);
472 r
= pakfire_mount(pakfire
, mp
->source
, target
, mp
->fstype
, mp
->flags
, mp
->options
);
480 int pakfire_make_ramdisk(struct pakfire
* pakfire
, char* path
, const char* args
) {
483 // Create a new temporary directory
484 char* p
= pakfire_mkdtemp(path
);
489 r
= pakfire_mount(pakfire
, "pakfire_ramdisk", p
, "tmpfs", 0, args
);
491 ERROR_ERRNO(pakfire
, r
, "Could not mount ramdisk at %s (%s): %m\n", p
, args
);
495 DEBUG(pakfire
, "Ramdisk mounted at %s (%s)\n", p
, args
);
500 int pakfire_bind(struct pakfire
* pakfire
, const char* src
, const char* dst
, int flags
) {
502 char mountpoint
[PATH_MAX
];
507 int r
= pakfire_path(pakfire
, mountpoint
, "%s", dst
);
511 DEBUG(pakfire
, "Bind-mounting %s to %s\n", src
, mountpoint
);
515 ERROR(pakfire
, "Could not stat %s: %m\n", src
);
519 // Make sure the mountpoint exists
520 switch (st
.st_mode
& S_IFMT
) {
522 r
= pakfire_mkdir(mountpoint
, st
.st_mode
);
523 if (r
&& errno
!= EEXIST
)
529 // Make parent directory
530 r
= pakfire_mkparentdir(mountpoint
, 0755);
535 FILE* f
= fopen(mountpoint
, "w");
546 // The Linux kernel seems to be quite funny when trying to bind-mount something
547 // as read-only and requires us to mount the source first, and then remount it
548 // again using MS_RDONLY.
549 if (flags
& MS_RDONLY
) {
550 r
= pakfire_mount(pakfire
, src
, mountpoint
, "bind", MS_BIND
|MS_REC
, NULL
);
554 // Add the remount flag
559 return pakfire_mount(pakfire
, src
, mountpoint
, "bind", flags
|MS_BIND
|MS_REC
, NULL
);