@@ -599,6 +599,45 @@ has_mount_for (libcrun_container_t *container, const char *destination)
599599 return false;
600600}
601601
602+ static int
603+ do_mount_cgroup_systemd_v1 (libcrun_container_t * container ,
604+ const char * source ,
605+ int targetfd ,
606+ const char * target ,
607+ unsigned long mountflags ,
608+ libcrun_error_t * err )
609+ {
610+ int ret ;
611+ cleanup_close int fd = -1 ;
612+ const char * subsystem = "systemd" ;
613+ cleanup_free char * subsystem_path = NULL ;
614+ cleanup_close int tmpfsdirfd = -1 ;
615+
616+ mountflags = mountflags & ~MS_BIND ;
617+
618+ ret = do_mount (container , source , targetfd , target , "tmpfs" , mountflags , "size=1024k" , 1 , err );
619+ if (UNLIKELY (ret < 0 ))
620+ return ret ;
621+
622+ /* Get a reference to the newly created cgroup directory. */
623+ tmpfsdirfd = open_mount_target (container , target , err );
624+ if (UNLIKELY (tmpfsdirfd < 0 ))
625+ return tmpfsdirfd ;
626+ targetfd = tmpfsdirfd ;
627+
628+ ret = mkdirat (targetfd , subsystem , 0755 );
629+ if (UNLIKELY (ret < 0 ))
630+ return crun_make_error (err , errno , "mkdir `%s`" , subsystem );
631+
632+ fd = openat (targetfd , subsystem , O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW );
633+ if (UNLIKELY (ret < 0 ))
634+ return crun_make_error (err , errno , "open `%s`" , subsystem_path );
635+
636+ xasprintf (& subsystem_path , "%s/%s" , target , subsystem );
637+
638+ return do_mount (container , "cgroup" , fd , subsystem_path , "cgroup" , mountflags , "none,name=systemd,xattr" , true, err );
639+ }
640+
602641static int
603642do_mount_cgroup_v1 (libcrun_container_t * container ,
604643 const char * source ,
@@ -623,7 +662,7 @@ do_mount_cgroup_v1 (libcrun_container_t *container,
623662 if (UNLIKELY (subsystems == NULL ))
624663 return -1 ;
625664
626- ret = do_mount (container , source , targetfd , target , "tmpfs" , mountflags , "size=1024k" , 1 , err );
665+ ret = do_mount (container , source , targetfd , target , "tmpfs" , mountflags & ~ MS_RDONLY , "size=1024k" , true , err );
627666 if (UNLIKELY (ret < 0 ))
628667 return ret ;
629668
@@ -658,7 +697,12 @@ do_mount_cgroup_v1 (libcrun_container_t *container,
658697
659698 it = strstr (subsystem , "name=" );
660699 if (it )
661- subsystem += 5 ;
700+ subsystem = it + 5 ;
701+
702+ if (strcmp (subsystem , "net_prio,net_cls" ) == 0 )
703+ subsystem = "net_cls,net_prio" ;
704+ if (strcmp (subsystem , "cpuacct,cpu" ) == 0 )
705+ subsystem = "cpu,cpuacct" ;
662706
663707 xasprintf (& source_subsystem , "/sys/fs/cgroup/%s" , subsystem );
664708
@@ -1144,6 +1188,8 @@ do_mounts (libcrun_container_t *container, int rootfsfd, const char *rootfs, lib
11441188 int ret ;
11451189 runtime_spec_schema_config_schema * def = container -> container_def ;
11461190 size_t rootfs_len = get_private_data (container )-> rootfs_len ;
1191+ const char * systemd_cgroup_v1 = find_annotation (container , "run.oci.systemd.force_cgroup_v1" );
1192+
11471193 for (i = 0 ; i < def -> mounts_len ; i ++ )
11481194 {
11491195 cleanup_free char * data = NULL ;
@@ -1157,7 +1203,6 @@ do_mounts (libcrun_container_t *container, int rootfsfd, const char *rootfs, lib
11571203 cleanup_close int targetfd = -1 ;
11581204
11591205 target = def -> mounts [i ]-> destination ;
1160-
11611206 while (* target == '/' )
11621207 target ++ ;
11631208
@@ -1275,7 +1320,14 @@ do_mounts (libcrun_container_t *container, int rootfsfd, const char *rootfs, lib
12751320 if (UNLIKELY (targetfd < 0 ))
12761321 return targetfd ;
12771322
1278- if (strcmp (type , "cgroup" ) == 0 )
1323+ if (systemd_cgroup_v1 && strcmp (def -> mounts [i ]-> destination , systemd_cgroup_v1 ) == 0 )
1324+ {
1325+ /* Override the cgroup mount with a single named cgroup name=systemd. */
1326+ ret = do_mount_cgroup_systemd_v1 (container , source , targetfd , target , flags , err );
1327+ if (UNLIKELY (ret < 0 ))
1328+ return ret ;
1329+ }
1330+ else if (strcmp (type , "cgroup" ) == 0 )
12791331 {
12801332 ret = do_mount_cgroup (container , source , targetfd , target , flags , err );
12811333 if (UNLIKELY (ret < 0 ))
0 commit comments