@@ -417,39 +417,48 @@ static int container_setup_sysctl(struct hyper_container *container)
417417 return 0 ;
418418}
419419
420- static int container_setup_dns ( struct hyper_container * container )
420+ static int container_binding_file ( char * src , char * dest )
421421{
422422 int fd ;
423423 struct stat st ;
424- char * src = "/tmp/hyper/resolv.conf" ;
425424
426425 if (stat (src , & st ) < 0 ) {
427426 if (errno == ENOENT ) {
428427 fprintf (stdout , "no dns configured\n" );
429428 return 0 ;
430429 }
431430
432- perror ( "stat resolve.conf failed" );
431+ fprintf ( stderr , "stat %s failed" , src );
433432 return -1 ;
434433 }
435434
436- hyper_mkdir ("./etc" , 0755 );
437-
438- fd = open ("./etc/resolv.conf" , O_CREAT | O_WRONLY , 0644 );
435+ fd = open (dest , O_CREAT | O_WRONLY , 0644 );
439436 if (fd < 0 ) {
440- perror ( "create /etc/resolv.conf failed" );
437+ fprintf ( stderr , "create %s failed" , dest );
441438 return -1 ;
442439 }
443440 close (fd );
444441
445- if (mount (src , "./etc/resolv.conf" , NULL , MS_BIND , NULL ) < 0 ) {
446- perror ( "bind to /etc/resolv.conf failed" );
442+ if (mount (src , dest , NULL , MS_BIND , NULL ) < 0 ) {
443+ fprintf ( stderr , "bind to %s failed" , dest );
447444 return -1 ;
448445 }
449446
450447 return 0 ;
451448}
452449
450+ static int container_setup_dns ()
451+ {
452+ hyper_mkdir ("./etc" , 0755 );
453+ return container_binding_file ("/tmp/hyper/resolv.conf" , "./etc/resolv.conf" );
454+ }
455+
456+ static int container_setup_hostname ()
457+ {
458+ hyper_mkdir ("./etc" , 0755 );
459+ return container_binding_file ("/tmp/hyper/hostname" , "./etc/hostname" );
460+ }
461+
453462static int container_setup_workdir (struct hyper_container * container )
454463{
455464 if (container -> initialize ) {
@@ -623,11 +632,16 @@ static int hyper_setup_container_rootfs(void *data)
623632 goto fail ;
624633 }
625634
626- if (container_setup_dns (container ) < 0 ) {
635+ if (container_setup_dns () < 0 ) {
627636 fprintf (stderr , "container sets up dns failed\n" );
628637 goto fail ;
629638 }
630639
640+ if (container_setup_hostname () < 0 ) {
641+ fprintf (stderr , "container sets up hostname failed\n" );
642+ goto fail ;
643+ }
644+
631645 // manipulate the rootfs of the container/namespace: move the prepared path @rootfs to /
632646 if (mount (rootfs , "/" , NULL , MS_MOVE , NULL ) < 0 ) {
633647 perror ("failed to move rootfs" );
0 commit comments