@@ -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