@@ -431,39 +431,48 @@ static int container_setup_sysctl(struct hyper_container *container)
431431 return 0 ;
432432}
433433
434- static int container_setup_dns ( struct hyper_container * container )
434+ static int container_binding_file ( char * src , char * dest )
435435{
436436 int fd ;
437437 struct stat st ;
438- char * src = "/tmp/hyper/resolv.conf" ;
439438
440439 if (stat (src , & st ) < 0 ) {
441440 if (errno == ENOENT ) {
442441 fprintf (stdout , "no dns configured\n" );
443442 return 0 ;
444443 }
445444
446- perror ( "stat resolve.conf failed" );
445+ fprintf ( stderr , "stat %s failed" , src );
447446 return -1 ;
448447 }
449448
450- hyper_mkdir ("./etc" , 0755 );
451-
452- fd = open ("./etc/resolv.conf" , O_CREAT | O_WRONLY , 0644 );
449+ fd = open (dest , O_CREAT | O_WRONLY , 0644 );
453450 if (fd < 0 ) {
454- perror ( "create /etc/resolv.conf failed" );
451+ fprintf ( stderr , "create %s failed" , dest );
455452 return -1 ;
456453 }
457454 close (fd );
458455
459- if (mount (src , "./etc/resolv.conf" , NULL , MS_BIND , NULL ) < 0 ) {
460- perror ( "bind to /etc/resolv.conf failed" );
456+ if (mount (src , dest , NULL , MS_BIND , NULL ) < 0 ) {
457+ fprintf ( stderr , "bind to %s failed" , dest );
461458 return -1 ;
462459 }
463460
464461 return 0 ;
465462}
466463
464+ static int container_setup_dns ()
465+ {
466+ hyper_mkdir ("./etc" , 0755 );
467+ return container_binding_file ("/tmp/hyper/resolv.conf" , "./etc/resolv.conf" );
468+ }
469+
470+ static int container_setup_hostname ()
471+ {
472+ hyper_mkdir ("./etc" , 0755 );
473+ return container_binding_file ("/tmp/hyper/hostname" , "./etc/hostname" );
474+ }
475+
467476static int container_setup_workdir (struct hyper_container * container )
468477{
469478 if (container -> initialize ) {
@@ -637,11 +646,16 @@ static int hyper_setup_container_rootfs(void *data)
637646 goto fail ;
638647 }
639648
640- if (container_setup_dns (container ) < 0 ) {
649+ if (container_setup_dns () < 0 ) {
641650 fprintf (stderr , "container sets up dns failed\n" );
642651 goto fail ;
643652 }
644653
654+ if (container_setup_hostname () < 0 ) {
655+ fprintf (stderr , "container sets up hostname failed\n" );
656+ goto fail ;
657+ }
658+
645659 // manipulate the rootfs of the container/namespace: move the prepared path @rootfs to /
646660 if (mount (rootfs , "/" , NULL , MS_MOVE , NULL ) < 0 ) {
647661 perror ("failed to move rootfs" );
0 commit comments