@@ -706,13 +706,9 @@ libkrun_modify_oci_configuration (void *cookie arg_unused, libcrun_context_t *co
706
706
struct krun_config * kconf = (struct krun_config * ) cookie ;
707
707
struct stat st_kvm , st_sev , st_nitro ;
708
708
bool has_kvm = true, has_sev = true, has_nitro = true;
709
- size_t len ;
709
+ size_t old_len , new_len ;
710
710
int ret ;
711
711
712
- if (def -> linux == NULL || def -> linux -> resources == NULL
713
- || def -> linux -> resources -> devices == NULL )
714
- return 0 ;
715
-
716
712
/* Always allow the /dev/kvm device. */
717
713
718
714
ret = stat ("/dev/kvm" , & st_kvm );
@@ -739,31 +735,38 @@ libkrun_modify_oci_configuration (void *cookie arg_unused, libcrun_context_t *co
739
735
has_nitro = false;
740
736
}
741
737
742
- if (has_kvm )
743
- {
744
- len = def -> linux -> resources -> devices_len ;
745
- def -> linux -> resources -> devices = xrealloc (def -> linux -> resources -> devices ,
746
- device_size * (len + 2 + (has_sev ? 1 : 0 )));
738
+ kconf -> has_kvm = has_kvm ;
739
+ kconf -> has_nitro = has_nitro ;
747
740
748
- def -> linux -> resources -> devices [len ] = make_oci_spec_dev ("a" , st_kvm .st_rdev , true, "rwm" );
749
- if (has_sev )
750
- def -> linux -> resources -> devices [len + 1 ] = make_oci_spec_dev ("a" , st_sev .st_rdev , true, "rwm" );
741
+ if (! has_kvm && ! has_nitro )
742
+ return 0 ;
751
743
752
- def -> linux -> resources -> devices_len += has_sev ? 2 : 1 ;
753
- }
744
+ /* spec says these are optional, ensure they exist so we can add our devices */
745
+ if (def -> linux == NULL )
746
+ def -> linux = xmalloc0 (sizeof (runtime_spec_schema_config_linux ));
747
+
748
+ if (def -> linux -> resources == NULL )
749
+ def -> linux -> resources = xmalloc0 (sizeof (runtime_spec_schema_config_linux_resources ));
754
750
751
+ old_len = def -> linux -> resources -> devices_len ;
752
+ new_len = old_len ;
753
+ if (has_kvm )
754
+ new_len += has_sev ? 2 : 1 ;
755
755
if (has_nitro )
756
- {
757
- len = def -> linux -> resources -> devices_len ;
758
- def -> linux -> resources -> devices = xrealloc (def -> linux -> resources -> devices ,
759
- device_size * ( len + 2 )) ;
756
+ new_len += 1 ;
757
+
758
+ def -> linux -> resources -> devices = xrealloc (def -> linux -> resources -> devices , device_size * ( new_len + 1 ));
759
+ def -> linux -> resources -> devices_len = new_len ;
760
760
761
- def -> linux -> resources -> devices [len ] = make_oci_spec_dev ("a" , st_nitro .st_rdev , true, "rwm" );
762
- def -> linux -> resources -> devices_len ++ ;
761
+ if (has_kvm )
762
+ {
763
+ def -> linux -> resources -> devices [old_len ++ ] = make_oci_spec_dev ("a" , st_kvm .st_rdev , true, "rwm" );
764
+ if (has_sev )
765
+ def -> linux -> resources -> devices [old_len ++ ] = make_oci_spec_dev ("a" , st_sev .st_rdev , true, "rwm" );
763
766
}
764
767
765
- kconf -> has_kvm = has_kvm ;
766
- kconf -> has_nitro = has_nitro ;
768
+ if ( has_nitro )
769
+ def -> linux -> resources -> devices [ old_len ++ ] = make_oci_spec_dev ( "a" , st_nitro . st_rdev , true, "rwm" ) ;
767
770
768
771
return 0 ;
769
772
}
0 commit comments