Simple script and config (type-safe) for building custom Linux kernels for Firecracker MicroVMs

refactor: enhance type definitions for configuration schemas

+56 -8
+56 -8
config.ts
··· 10 10 */ 11 11 12 12 // Base config value types 13 - const ConfigValueSchema = z.union([ 13 + const ConfigValueSchema: z.ZodType< 14 + "y" | "m" | "n" | number | string | boolean 15 + > = z.union([ 14 16 z.literal("y"), // Built-in 15 17 z.literal("m"), // Module 16 18 z.literal("n"), // Not set (explicit) ··· 20 22 ]); 21 23 22 24 // Individual config entry 23 - const ConfigEntrySchema = z.object({ 25 + const ConfigEntrySchema: z.ZodType<{ 26 + key: string; 27 + value?: z.infer<typeof ConfigValueSchema>; 28 + comment?: string; 29 + }> = z.object({ 24 30 key: z.string(), 25 31 value: ConfigValueSchema.optional(), 26 32 comment: z.string().optional(), ··· 42 48 ); 43 49 44 50 // Main kernel config schema 45 - export const KernelConfigSchema = z.object({ 51 + export const KernelConfigSchema: z.ZodType<{ 52 + version?: string | undefined; 53 + buildInfo?: 54 + | { 55 + compiler?: string | undefined; 56 + gccVersion?: string | undefined; 57 + buildSalt?: string | undefined; 58 + } 59 + | undefined; 60 + sections: ConfigSection[]; 61 + flatConfig: Record<string, ConfigValue | undefined>; 62 + }> = z.object({ 46 63 version: z.string().optional(), 47 64 buildInfo: z 48 65 .object({ ··· 56 73 }); 57 74 58 75 // Specific schemas for common config categories 59 - export const ProcessorConfigSchema = z.object({ 76 + export const ProcessorConfigSchema: z.ZodType<{ 77 + SMP?: boolean | undefined; 78 + NR_CPUS?: number | undefined; 79 + X86_64?: boolean | undefined; 80 + NUMA?: boolean | undefined; 81 + PREEMPT?: boolean | undefined; 82 + PREEMPT_VOLUNTARY?: boolean | undefined; 83 + PREEMPT_NONE?: boolean | undefined; 84 + }> = z.object({ 60 85 SMP: z.boolean().optional(), 61 86 NR_CPUS: z.number().optional(), 62 87 X86_64: z.boolean().optional(), ··· 66 91 PREEMPT_NONE: z.boolean().optional(), 67 92 }); 68 93 69 - export const SecurityConfigSchema = z.object({ 94 + export const SecurityConfigSchema: z.ZodType<{ 95 + SECURITY?: boolean | undefined; 96 + SECURITY_SELINUX?: boolean | undefined; 97 + SECURITY_APPARMOR?: boolean | undefined; 98 + SECURITY_SMACK?: boolean | undefined; 99 + SECCOMP?: boolean | undefined; 100 + STACKPROTECTOR?: boolean | undefined; 101 + FORTIFY_SOURCE?: boolean | undefined; 102 + }> = z.object({ 70 103 SECURITY: z.boolean().optional(), 71 104 SECURITY_SELINUX: z.boolean().optional(), 72 105 SECURITY_APPARMOR: z.boolean().optional(), ··· 76 109 FORTIFY_SOURCE: z.boolean().optional(), 77 110 }); 78 111 79 - export const NetworkingConfigSchema = z.object({ 112 + export const NetworkingConfigSchema: z.ZodType<{ 113 + NET?: boolean | undefined; 114 + INET?: boolean | undefined; 115 + IPV6?: boolean | undefined; 116 + NETFILTER?: boolean | undefined; 117 + PACKET?: boolean | undefined; 118 + UNIX?: boolean | undefined; 119 + }> = z.object({ 80 120 NET: z.boolean().optional(), 81 121 INET: z.boolean().optional(), 82 122 IPV6: z.boolean().optional(), ··· 85 125 UNIX: z.boolean().optional(), 86 126 }); 87 127 88 - export const FilesystemConfigSchema = z.object({ 128 + export const FilesystemConfigSchema: z.ZodType<{ 129 + EXT4_FS?: boolean | undefined; 130 + XFS_FS?: boolean | undefined; 131 + BTRFS_FS?: boolean | undefined; 132 + NFS_FS?: boolean | undefined; 133 + TMPFS?: boolean | undefined; 134 + }> = z.object({ 89 135 EXT4_FS: z.boolean().optional(), 90 136 XFS_FS: z.boolean().optional(), 91 137 BTRFS_FS: z.boolean().optional(), ··· 753 799 } 754 800 } 755 801 756 - export const validateKernelConfig = (data: unknown) => { 802 + export const validateKernelConfig = ( 803 + data: unknown 804 + ): ReturnType<typeof KernelConfigSchema.safeParse> => { 757 805 return KernelConfigSchema.safeParse(data); 758 806 }; 759 807