### Mount and Populate QEMU Disk Image Source: https://github.com/selinuxproject/selinux-kernel/wiki/Getting-Started These commands show how to mount the created disk image using a loop device, install a minimal Linux distribution (Fedora in this example) into it using `dnf`, and configure basic system settings like the root password and fstab. This prepares the disk image to be used as a root filesystem. ```bash sudo losetup -fP qemu-disk --show sudo mount -o rw /dev/loop0 /mnt sudo dnf --installroot=/mnt --releasever=42 install @core sudo chroot /mnt passwd echo "/dev/sda / ext4 defaults 0 1" | sudo tee -a /mnt/etc/fstab sudo umount /mnt sudo losetup -d /dev/loop0 ``` -------------------------------- ### Audio Streaming Example Commands Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/usb/gadget-testing.rst These commands demonstrate how to stream audio data to and from the host. The first example shows a basic setup using arecord and aplay, while the second uses specific card and device names. ```bash $ arecord -f dat -t wav -D hw:2,0 | aplay -D hw:0,0 & $ arecord -f dat -t wav -D hw:CARD=UAC1Gadget,DEV=0 | \ aplay -D default:CARD=OdroidU3 ``` -------------------------------- ### Start VDO Volume with Modified Parameters Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/admin-guide/device-mapper/vdo.rst This example shows starting a VDO volume again, demonstrating that while logical and physical sizes must match previous settings, other parameters like hash, logical, and physical block sizes can be modified. ```bash dmsetup create vdo1 --table \ "0 10485760 vdo V4 /dev/dm-1 786432 512 65550 5000 hash 1 logical 3 physical 2" ``` -------------------------------- ### KVM s390 Get Guest Storage Keys (KVM_S390_GET_SKEYS) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/virt/kvm/api.rst Retrieves guest storage key values on the s390 architecture using the KVM_S390_GET_SKEYS ioctl. It requires the KVM_CAP_S390_SKEYS capability and operates on VM ioctls. Parameters include the starting guest frame number, the count of frames, and a buffer address for the storage key data. ```c struct kvm_s390_skeys { __u64 start_gfn; __u64 count; __u64 skeydata_addr; __u32 flags; __u32 reserved[9]; }; // Example usage (conceptual) // struct kvm_s390_skeys skey_args; // skey_args.start_gfn = 0; // skey_args.count = 10; // skey_args.skeydata_addr = (__u64)buffer; // ioctl(vm_fd, KVM_S390_GET_SKEYS, &skey_args); ``` -------------------------------- ### KVM_PV_VM_VERIFY Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/virt/kvm/api.rst Verifies the integrity of the unpacked image. KVM is only allowed to start protected VCPUs if this command succeeds. ```APIDOC ## POST /kvm/pv/vm/verify ### Description Verifies the integrity of the unpacked VM image. Successful verification is required before starting protected VCPUs. ### Method POST ### Endpoint /kvm/pv/vm/verify ### Parameters #### Query Parameters - **cmd** (string) - Required - The command value, expected to be 'KVM_PV_VM_VERIFY'. ### Request Example ```json { "cmd": "KVM_PV_VM_VERIFY" } ``` ### Response #### Success Response (200) - **rc** (integer) - Ultravisor return code. - **rrc** (integer) - Ultravisor return reason code. #### Response Example ```json { "rc": 0, "rrc": 0 } ``` ``` -------------------------------- ### Get PowerPC PVInfo (KVM_PPC_GET_PVINFO) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/virt/kvm/api.rst Fetches PowerPC specific information required for the guest, such as hypercall instructions. This data can be passed to the guest via device tree or other means. The flags bitmap indicates additional fields present in the structure. ```c struct kvm_ppc_pvinfo { __u32 flags; __u32 hcall[4]; __u8 pad[108]; }; /* the host supports the ePAPR idle hcall #define KVM_PPC_PVINFO_FLAGS_EV_IDLE (1<<0) */ ``` -------------------------------- ### Start Hardware Device Initialization (C) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/networking/ieee802154.rst Handler called by the IEEE 802.15.4 module to initialize the hardware device. This function should perform any necessary setup for the radio transceiver. ```c int start(struct ieee802154_hw *hw) ``` -------------------------------- ### Create VDO Volume with dmsetup Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/admin-guide/device-mapper/vdo.rst This example demonstrates how to create a VDO volume using the `dmsetup create` command. It specifies the logical and physical device sizes, along with other VDO-specific parameters. ```bash dmsetup create vdo0 --table \ "0 2097152 vdo V4 /dev/dm-1 262144 4096 32768 16380" ``` -------------------------------- ### Change Current VTY Connection Example Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/arch/powerpc/hvcs.rst This example demonstrates how to change the currently selected VTY partner by writing a valid partner CLC (Connection Logical Channel) to the 'current_vty' file. This operation is performed on the system's sysfs interface. ```bash Pow5:/sys/bus/vio/drivers/hvcs/30000004 # echo U5112.428.10304 8A-V4-C0 > current_vty ``` -------------------------------- ### Create and Test a Basic Initramfs 'Hello World' Program Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/filesystems/ramfs-rootfs-initramfs.rst This snippet demonstrates how to create a simple C program that prints 'Hello world!' and runs indefinitely. It then compiles this program statically, packages it into a cpio.gz archive suitable for an initramfs, and tests it using QEMU with the kernel's initrd loading mechanism. ```c #include #include int main(int argc, char *argv[]) { printf("Hello world!\n"); sleep(999999999); } ``` ```shell gcc -static hello.c -o init echo init | cpio -o -H newc | gzip > test.cpio.gz qemu -kernel /boot/vmlinuz -initrd test.cpio.gz /dev/zero ``` -------------------------------- ### Start Espeakup Daemon (Manual Install) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/admin-guide/spkguide.txt This command manually starts the Espeakup speech synthesizer daemon after it has been compiled and installed from source. The binary is typically located in /usr/bin. This requires root privileges. ```bash /usr/bin/espeakup ``` -------------------------------- ### Set NCR53C8XX Boot Setup Commands (insmod) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/scsi/ncr53c8xx.rst Enables passing setup commands to the ncr53c8xx driver using the 'insmod' command. Unlike LILO, spaces are used as separators for options when using 'insmod'. This example sets the same options as the LILO example. ```bash insmod ncr53c8xx.o ncr53c8xx="tags:4 sync:10 debug:0x200" ``` -------------------------------- ### Sample Boot Configuration (2.02+) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/arch/x86/boot.rst Presents a memory layout for modern boot protocols (2.02 and later). It demonstrates how the stack/heap can occupy a full 64K segment, with the kernel command line located above it, offering more flexibility. ```c /* When loading below 0x90000, use the entire segment: ============= =================== 0x0000-0x7fff Real mode kernel 0x8000-0xdfff Stack and heap 0xe000-0xffff Kernel command line ============= =================== */ /* For boot protocol 2.02 or higher */ if (protocol >= 0x0202 && loadflags & 0x01) heap_end = 0xe000; else heap_end = 0x9800; if (protocol >= 0x0201) { heap_end_ptr = heap_end - 0x200; loadflags |= 0x80; /* CAN_USE_HEAP */ } ``` -------------------------------- ### CPU Hotplug State Setup Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/core-api/cpu_hotplug.rst Functions for setting up CPU hotplug states, with options to install callbacks only or to install and invoke startup callbacks. ```APIDOC ## CPU Hotplug State Setup API ### Description These functions are used to set up CPU hotplug states within the SELinux kernel. They allow for the installation of startup and teardown callbacks, with variations for immediate callback invocation or deferred invocation. ### Methods - `cpuhp_setup_state_nocalls(state, name, startup, teardown)` - `cpuhp_setup_state_nocalls_cpuslocked(state, name, startup, teardown)` - `cpuhp_setup_state_multi(state, name, startup, teardown)` - `cpuhp_setup_state(state, name, startup, teardown)` - `cpuhp_setup_state_cpuslocked(state, name, startup, teardown)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **state** (int) - The state identifier. Can be statically allocated or dynamically allocated. - **name** (const char *) - A string for sysfs output and instrumentation, following the convention "subsys:mode" or "subsys/driver:mode". - **startup** (void (*startup)(void)) - Function pointer to the callback invoked during CPU online operation. Set to NULL if not required. - **teardown** (void (*teardown)(void)) - Function pointer to the callback invoked during CPU offline operation. Set to NULL if not required. ### Request Example ```json { "state": 1, "name": "perf/online", "startup": "NULL", "teardown": "NULL" } ``` ### Response #### Success Response (200) - **0**: Statically allocated state was successfully set up. - **>0**: Dynamically allocated state was successfully set up. The returned number is the state number which was allocated. #### Failure Response (<0) - **<0**: Operation failed. #### Response Example ```json { "status": "success", "state_number": 5 } ``` ### Notes - `_cpuslocked()` variants must be used when called from a CPU hotplug read locked region. - These functions cannot be used from within CPU hotplug callbacks. - `cpuhp_setup_state()` and `cpuhp_setup_state_cpuslocked()` invoke the @startup callback for relevant online CPUs. ``` -------------------------------- ### Example: Full Multi-Instance State Lifecycle (C) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/core-api/cpu_hotplug.rst Demonstrates the complete lifecycle of setting up a dynamic multi-instance state, adding multiple instances with their nodes, removing instances, and finally removing the multi-instance state itself. ```c state = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "subsys:online", subsys_cpu_online, subsys_cpu_offline); if (state < 0) return state; .... ret = cpuhp_state_add_instance(state, &inst1->node); if (ret) return ret; .... ret = cpuhp_state_add_instance(state, &inst2->node); if (ret) return ret; .... cpuhp_remove_instance(state, &inst1->node); .... cpuhp_remove_instance(state, &inst2->node); .... cpuhp_remove_multi_state(state); ``` -------------------------------- ### Crop and Downscale Example using media-ctl Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/admin-guide/media/imx.rst This example demonstrates how to crop an input frame and then downscale it using the media-ctl command. It shows the sequence of commands to set the input format, apply cropping, and then set the composition size for downscaling. ```none media-ctl -V "'ipu1_csi0_mux':2[fmt:UYVY2X8/1280x960]" media-ctl -V "'ipu1_csi0':0[crop:(0,0)/640x480]" media-ctl -V "'ipu1_csi0':0[compose:(0,0)/320x240]" ``` -------------------------------- ### Manually Creating hvcs Device Nodes with mknod Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/arch/powerpc/hvcs.rst Provides examples of using the `mknod` command to manually create persistent character device nodes for hvcs devices. This ensures that device nodes exist even before the hvcs module is loaded. ```shell mknod /dev/hvcs0 c 254 0 mknod /dev/hvcs1 c 254 1 mknod /dev/hvcs2 c 254 2 mknod /dev/hvcs3 c 254 3 ``` -------------------------------- ### Install Kernel and Modules (make) Source: https://github.com/selinuxproject/selinux-kernel/wiki/Getting-Started Installs the compiled kernel and its modules onto the system. This command requires root privileges. ```bash sudo make modules_install install ``` -------------------------------- ### Min-Heap Example Usage in C Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/core-api/min_heap.rst Demonstrates the initialization and usage of the min-heap API, including defining callbacks, initializing the heap with a buffer, building the heap, and peeking at the top element. ```c #include int my_less_function(const void *lhs, const void *rhs, void *args) { return (*(int *)lhs < *(int *)rhs); } struct min_heap_callbacks heap_cb = { .less = my_less_function, /* Comparison function for heap order */ .swp = NULL, /* Use default swap function */ }; void example_usage(void) { /* Pre-populate the buffer with elements */ int buffer[5] = {5, 2, 8, 1, 3}; /* Declare a min-heap */ DEFINE_MIN_HEAP(int, my_heap); /* Initialize the heap with preallocated buffer and size */ min_heap_init(&my_heap, buffer, 5); /* Build the heap using min_heapify_all */ my_heap.nr = 5; /* Set the number of elements in the heap */ min_heapify_all(&my_heap, &heap_cb, NULL); /* Peek at the top element (should be 1 in this case) */ int *top = min_heap_peek(&my_heap); pr_info("Top element: %d\n", *top); } ``` -------------------------------- ### Control PCI IDE BMDMA Engine (C) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/driver-api/libata.rst Hooks for managing the IDE Bus Master DMA (BMDMA) engine during transactions. These include setup, start, stop, and status retrieval. FIS-based drivers often leave these as no-ops. Legacy IDE drivers commonly use helper functions like ata_bmdma_setup. ```c void (*bmdma_setup) (struct ata_queued_cmd *qc); void (*bmdma_start) (struct ata_queued_cmd *qc); void (*bmdma_stop) (struct ata_port *ap); u8 (*bmdma_status) (struct ata_port *ap); ``` -------------------------------- ### mkfs.f2fs Quick Options Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/filesystems/f2fs.rst This section outlines the common quick options for the `mkfs.f2fs` command, used for formatting partitions with the F2FS filesystem. Options include setting volume labels, allocation strategies, overprovisioning ratios, and discard command behavior. ```none =============== =========================================================== ``-l [label]`` Give a volume label, up to 512 unicode name. ``-a [0 or 1]`` Split start location of each area for heap-based allocation. 1 is set by default, which performs this. ``-o [int]`` Set overprovision ratio in percent over volume size. 5 is set by default. ``-s [int]`` Set the number of segments per section. 1 is set by default. ``-z [int]`` Set the number of sections per zone. 1 is set by default. ``-e [str]`` Set basic extension list. e.g. "mp3,gif,mov" ``-t [0 or 1]`` Disable discard command or not. 1 is set by default, which conducts discard. =============== ``` -------------------------------- ### Ftrace Boot-Time Tracing Configuration Example Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/trace/boottime-trace.rst Example configuration for enabling ftrace event tracing at boot time, including setting filters, enabling events, defining kprobe events, and creating synthetic events with histograms. ```shell ftrace.event { task.task_newtask { filter = "pid < 128" enable } kprobes.vfs_read { probes = "vfs_read $arg1 $arg2" filter = "common_pid < 200" enable } synthetic.initcall_latency { fields = "unsigned long func", "u64 lat" hist { keys = func.sym, lat values = lat sort = lat } } initcall.initcall_start.hist { keys = func var.ts0 = common_timestamp.usecs } initcall.initcall_finish.hist { keys = func var.lat = common_timestamp.usecs - $ts0 onmatch { event = initcall.initcall_start trace = initcall_latency, func, $lat } } } ``` -------------------------------- ### Udev Install Rules Script Example (Shell) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/admin-guide/aoe/examples.rst An example shell script for installing udev rules. This script automates the process of placing udev rule files in the correct system directory, ensuring they are active upon system boot or device detection. ```shell #!/bin/bash # Example script to install udev rules cp udev-rules.d/* /etc/udev/rules.d/ udevadm control --reload-rules udevadm trigger ``` -------------------------------- ### Install Build Dependencies (dnf) Source: https://github.com/selinuxproject/selinux-kernel/wiki/Getting-Started Installs necessary packages for building the kernel using the dnf package manager. This is a prerequisite for kernel compilation. ```bash sudo dnf build-dep kernel ``` -------------------------------- ### Starting a UML Instance Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/virt/uml/user_mode_linux_howto_v2.rst This command launches a User Mode Linux (UML) instance with specified memory, root filesystem, network configuration (tap0), and console settings. It configures the instance to use a local image file as its root and connect to the host via a tap interface, with console output directed to the starting terminal. ```bash # linux mem=2048M umid=TEST \ ubd0=Filesystem.img \ vec0:transport=tap,ifname=tap0,depth=128,gro=1 \ root=/dev/ubda con=null con0=null,fd:2 con1=fd:0,fd:1 ``` -------------------------------- ### DAX User Code Example: Scan Command CCB Setup Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/arch/sparc/oradax/oracle-dax.rst Illustrates the setup of the CCB for a DAX Scan command in user code. This example configures the control, completion, input, and access fields of the CCB to perform a scan operation with specific input and output formats. It assumes 'input' and 'nbits' are defined elsewhere. ```c struct ccb *ccb; // Assume ccb is allocated and points to a CCB structure unsigned long input; // Assume input buffer address is set int nbits; // Assume number of bits is set ccb->control = /* Table 36.1, CCB Header Format */ (2L << 48) /* command = Scan Value */ | (3L << 40) /* output address type = primary virtual */ | (3L << 34) /* primary input address type = primary virtual */ /* Section 36.2.1, Query CCB Command Formats */ | (1 << 28) /* 36.2.1.1.1 primary input format = fixed width bit packed */ | (0 << 23) /* 36.2.1.1.2 primary input element size = 0 (1 bit) */ | (8 << 10) /* 36.2.1.1.6 output format = bit vector */ | (0 << 5) /* 36.2.1.3 First scan criteria size = 0 (1 byte) */ | (31 << 0); /* 36.2.1.3 Disable second scan criteria */ ccb->completion = 0; /* Completion area address, to be filled in by driver */ ccb->input0 = (unsigned long) input; /* primary input address */ ccb->access = /* Section 36.2.1.2, Data Access Control */ (2 << 24) /* Primary input length format = bits */ | (nbits - 1); /* number of bits in primary input stream, minus 1 */ ccb->input1 = 0; /* secondary input address, unused */ // Note: output and table fields would also need to be set for a complete command. ``` -------------------------------- ### phylink Device Tree Configuration Examples Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/networking/sfp-phylink.rst Examples of device tree node configurations for phylink. The first example shows a basic PHY mode setup, while the second demonstrates 'in-band-status' mode for SGMII signaling. ```none ð { phy = <&phy>; phy-mode = "sgmii"; }; ``` ```none ð { managed = "in-band-status"; phy = <&phy>; phy-mode = "sgmii"; }; ``` -------------------------------- ### dmsetup Command to Create Cache Device (Shell) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/admin-guide/device-mapper/cache.rst Provides examples of `dmsetup create` commands for setting up a cache device. These commands specify the cache type, underlying devices (metadata, SSD, origin), block size, writeback mode, and various tuning parameters like sequential and random thresholds. ```shell dmsetup create my_cache --table '0 41943040 cache /dev/mapper/metadata \ /dev/mapper/ssd /dev/mapper/origin 512 1 writeback default 0' ``` ```shell dmsetup create my_cache --table '0 41943040 cache /dev/mapper/metadata \ /dev/mapper/ssd /dev/mapper/origin 1024 1 writeback \ mq 4 sequential_threshold 1024 random_threshold 8' ``` -------------------------------- ### EP93xx Framebuffer Setup Callback (C) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/fb/ep93xx-fb.rst An example of a board-specific framebuffer setup function. It receives the platform_device structure, from which the ep93xxfb_mach_info and fb_info structures can be accessed. ```c static int some_board_fb_setup(struct platform_device *pdev) { struct ep93xxfb_mach_info *mach_info = pdev->dev.platform_data; struct fb_info *fb_info = platform_get_drvdata(pdev); /* Board specific framebuffer setup */ } ``` -------------------------------- ### Release-Acquire Chain Example (C) Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/memory-barriers.txt Demonstrates the use of smp_load_acquire and smp_store_release in C to establish ordering between specific CPUs. It shows how these operations create a chain of synchronization, prohibiting certain outcomes but also highlighting that this ordering is local to the chain and may not extend to other CPUs. ```C int u, v, x, y, z; void cpu0(void) { r0 = smp_load_acquire(&x); WRITE_ONCE(u, 1); smp_store_release(&y, 1); } void cpu1(void) { r1 = smp_load_acquire(&y); r4 = READ_ONCE(v); r5 = READ_ONCE(u); smp_store_release(&z, 1); } void cpu2(void) { r2 = smp_load_acquire(&z); smp_store_release(&x, 1); } void cpu3(void) { WRITE_ONCE(v, 1); smp_mb(); r3 = READ_ONCE(u); } ``` -------------------------------- ### Install bindgen CLI Tool Source: https://github.com/selinuxproject/selinux-kernel/blob/main/Documentation/rust/quick-start.rst Installs the bindgen command-line interface tool using cargo. This tool is used to generate bindings to the C side of the kernel at build time. The installation is locked to the latest compatible versions. ```bash cargo install --locked bindgen-cli ```