summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/mount.c
blob: 4ff1f5ffa2ef2e39eac86f5df2119406c09b8452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Guillaume Cottenceau (gc)
 *
 * Copyright 2000 Mandriva
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
 * Portions from Erik Troan (ewt@redhat.com)
 *
 * Copyright 1996 Red Hat Software 
 *
 */

// for asprintf:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "log.h"
#include "utils.h"
#include "modules.h"

#include "mount.h"



/* WARNING: this won't work if the argument is not /dev/ based */
int ensure_dev_exists(const char * dev)
{
	struct stat buf;
	
	if (!stat(dev, &buf))
		return 0; /* if the file already exists, we assume it's correct */

	// give udev some time to create nodes if module was just insmoded:
	system("udevadm settle");

	if (!stat(dev, &buf)) {
		log_message("I don't know how to create device %s, please post bugreport to me!", dev);
		return -1;
	}
	
	return 0;
}


/* mounts, creating the device if needed+possible */
int my_mount(char *dev, char *location, char *fs, int force_rw)
{
	char * opts = NULL;
	struct stat buf;
	int rc;

	if (strcmp(fs, "nfs")) {
	    rc = ensure_dev_exists(dev);
	    if (rc != 0) {
		    log_message("could not create required device file");
		    return -1;
	    }
	}

	log_message("mounting %s on %s as type %s", dev, location, fs);

	if (stat(location, &buf)) {
		if (mkdir(location, 0755)) {
			log_perror("could not create location dir");
			return -1;
		}
	} else if (!S_ISDIR(buf.st_mode)) {
		log_message("not a dir %s, will unlink and mkdir", location);
		if (unlink(location)) {
			log_perror("could not unlink");
			return -1;
		}
		if (mkdir(location, 0755)) {
			log_perror("could not create location dir");
			return -1;
		}
	}

#ifndef DISABLE_MEDIAS
	if (!strcmp(fs, "vfat")) {
		my_modprobe("nls_cp437", ANY_DRIVER_TYPE, NULL);
		my_modprobe("nls_iso8859_1", ANY_DRIVER_TYPE, NULL);
		my_modprobe("vfat", ANY_DRIVER_TYPE, NULL);
		opts = "check=relaxed";
	}

	if (!strcmp(fs, "ntfs")) {
		my_modprobe("ntfs", ANY_DRIVER_TYPE, NULL);
	}

	if (!strcmp(fs, "reiserfs"))
		my_modprobe("reiserfs", ANY_DRIVER_TYPE, NULL);

	if (!strcmp(fs, "reiser4"))
		my_modprobe("reiser4", ANY_DRIVER_TYPE, NULL);

	if (!strcmp(fs, "jfs"))
		my_modprobe("jfs", ANY_DRIVER_TYPE, NULL);

	if (!strcmp(fs, "xfs"))
		my_modprobe("xfs", ANY_DRIVER_TYPE, NULL);

	if (!strcmp(fs, "ext4"))
		my_modprobe("ext4", ANY_DRIVER_TYPE, NULL);

	if (!strcmp(fs, "btrfs"))
		my_modprobe("btrfs", ANY_DRIVER_TYPE, NULL);

#endif
	if (!strcmp(fs, "iso9660"))
		my_modprobe("isofs", ANY_DRIVER_TYPE, NULL);

#ifndef DISABLE_NETWORK
	if (!strcmp(fs, "nfs")) {
		my_modprobe("nfs", ANY_DRIVER_TYPE, NULL);
	}
#endif

	char *cmd;
	rc = asprintf(&cmd, "mount %s %s -t %s -o %s%s", dev, location, fs, (force_rw ? "" : "ro,"), (opts ? opts : ""));
	if (rc == -1) {
		log_perror("asprint allocation failure");
		rmdir(location);
		return rc;
	}
	rc = system(cmd);
	if (rc != 0) {
		log_perror("mount failed");
		rmdir(location);
	}

	return rc;
}