Skip to content

Commit 983411c

Browse files
committed
Fix support for multiple mounts. mkdir -p mountpoints
1 parent 5b3b95c commit 983411c

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ RUN apt-get update && apt-get install dietlibc-dev cpio
44
WORKDIR /build
55
COPY . .
66
RUN ./autogen.sh \
7+
#&& ./configure --enable-debug CC="diet gcc" \
78
&& ./configure CC="diet gcc" \
89
&& make \
910
&& mkdir initramfs \

devices.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,5 +482,9 @@ int is_fs_with_serial(const char *device_name, const char *serial)
482482
return 0;
483483
}
484484

485+
//#ifdef ENABLE_DEBUG
486+
//warn("File ", fn_buf, " contains ", device_serial, NULL);
487+
//#endif
488+
485489
return strncmp(serial, device_serial, VIRTIO_BLK_ID_BYTES) == 0;
486490
}

tiny_initramfs.c

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,67 @@ int main(int argc, char **argv)
123123
timeout_togo = -1;
124124
for (int mount_idx = 0; mount_idx <= required_mounts; mount_idx++) {
125125
wait_for_device(real_device_name, &timeout_togo, mount_device[mount_idx], root_delay);
126-
}
127-
128-
#ifdef ENABLE_DEBUG
129-
warn("Waited for root device", NULL);
130-
#endif
131126

132-
for (int mount_idx = 0; mount_idx <= required_mounts; mount_idx++) {
127+
#ifdef ENABLE_DEBUG
128+
warn("Waited for device ", mount_device[mount_idx], NULL);
129+
#endif
130+
133131
r = mount_filesystem(real_device_name, mount_target[mount_idx], strlen(mount_fstype[mount_idx]) ? mount_fstype[mount_idx] : NULL, mount_options[mount_idx]);
132+
if (r == -ENOENT) {
133+
// Recursive mkdir
134+
int orig_len = strlen(mount_target[mount_idx]);
135+
int cur_len = orig_len;
136+
// First iterate up until we find an existing parent
137+
while(r == -ENOENT && mount_target[mount_idx][0] == '/') {
138+
if (mkdir(mount_target[mount_idx], 0777) < 0) {
139+
r = -errno;
140+
} else {
141+
r = 0;
142+
}
143+
if (r == -EEXIST) {
144+
// We're done
145+
r = 0;
146+
}
147+
if (r == -ENOENT) {
148+
// Iterate up the directory structure
149+
for (int i = cur_len - 1; i>=0; i--) {
150+
cur_len -= 1;
151+
if (mount_target[mount_idx][i] == '/') {
152+
mount_target[mount_idx][i] = 0;
153+
}
154+
}
155+
}
156+
}
157+
158+
// Restore the original path and mkdir along it
159+
while (r == 0 && cur_len < orig_len) {
160+
if (mount_target[mount_idx][cur_len] == 0) {
161+
// Ok we truncated the path before, try mkdir on it
162+
mount_target[mount_idx][cur_len] = '/';
163+
if (mkdir(mount_target[mount_idx], 0777) < 0) {
164+
r = -errno;
165+
} else {
166+
r = 0;
167+
}
168+
if (r == -EEXIST) {
169+
// We're done
170+
r = 0;
171+
}
172+
}
173+
cur_len += 1;
174+
}
175+
176+
if (r == 0) {
177+
r = mount_filesystem(real_device_name, mount_target[mount_idx], strlen(mount_fstype[mount_idx]) ? mount_fstype[mount_idx] : NULL, mount_options[mount_idx]);
178+
}
179+
}
134180
if (r < 0)
135181
panic(-r, "Failed to mount filesystem from ", mount_device[mount_idx], " into ", mount_target[mount_idx], NULL);
136182
}
137183

138184
#ifdef ENABLE_DEBUG
139-
warn("Mounted root filesystem", NULL);
185+
warn("Mounted all filesystems, contents of /proc/self/mountinfo:", NULL);
186+
debug_dump_file("/proc/self/mountinfo");
140187
#endif
141188

142189
/* We need these regardless of /usr handling */

0 commit comments

Comments
 (0)