aboutsummaryrefslogtreecommitdiff
path: root/vm.sh
blob: 2d6194dc554384420640faa4ee22e94713d2d979 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh

function run
{
    st \
    qemu-system-x86_64 \
		-kernel "$BIND/$KERNEL_NAME" \
		-initrd "$BIND/$INITFS_NAME" \
		-append "console=ttyS0" -nographic
}

function initramfs
{
    IMG="$(pwd)/$1"
    DIR="${IMG%.*}"
    mkdir -p $DIR

    # get busybox things
    install -D $(which busybox) $DIR/bin/busybox
    ln -sf busybox $DIR/bin/sh
    ln -sf busybox $DIR/bin/dmesg
    ln -sf busybox $DIR/bin/mount

    # copy the files in bin
    mkdir -p "$DIR/usr"
    find "$BIND" -maxdepth 1 -mindepth 1 \
         ! -name "*initramfs*" \
         ! -name "*kernel*" \
         -exec cp -r {} "$DIR/usr" \;

    # copy a bunch of kernel modules
    MODULES="usbhid usbcore hid"
    modinfo -F filename $MODULES | while read mod; do
        if [ "$mod" = "(builtin)" ]; then continue; fi

        mkdir -p "$DIR/$(dirname $mod)"

        # uncompress if compressed
        if [[ $mod == *.ko.zst ]]; then
            zstd -d -c "$mod" > "$DIR/$(dirname $mod)/$(basename $mod .zst)"
        else
            cp "$mod" "$DIR/$mod"
        fi
    done

    # make the init script
    cat <<EOF > "$DIR/init"
#!/bin/sh
# modproble all modules
for mod in $MODULES; do
    modprobe "\$mod"
done
EOF

    # append the script written in src/
    cat $2 >> "$DIR/init"
    chmod +x $DIR/init

    # make image and delete folder
    (cd $DIR; find . | cpio -H newc -o | gzip > $IMG)
    rm -rf $DIR
}

function kernel
{
    sudo cp /boot/vmlinuz-linux-lts $1
}

$1 $2 $3