about summary refs log tree commit diff stats
path: root/tools/qemu.html
blob: 0079dfc69600744695d3d7e3c22849d6e87adab3 (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE html>
<html dir="ltr" lang="en">
    <head>
        <meta charset='utf-8'>
        <title>Qemu</title>
    </head>
    <body>

        <a href="index.html">Tools Index</a>

        <h1>Qemu</h1>

        <h2 id="kern">1. Host System</h2>

        <p>Load modules, in this case kvm of intel cpu;</p>

        <pre>
        # modprobe -a kvm-intel tun virtio
        </pre>

        <p>Add users to kvm group;</p>

        <pre>
        # usermod -a -G kvm c9admin
        # usermod -a -G kvm username
        </pre>

        <h2 id="disk">2. Disk images</h2>

        <dl>
            <dt>img</dt>
            <dd>Raw disk image, allows dd to a physical device.</dd>
            <dt>raw</dt>
            <dd>Raw disk image, allows dd to a physical device.</dd>
            <dt>qcow2</dt>
            <dd>Qcow disk image file used by qemu.</dd>
        </dl>

        <p>Create hard disk image, there is different types,
        this describes how to create a qcow2 type;</p>

        <pre>
        $ qemu-img create -f qcow2 crux-img.qcow2 15G
        </pre>

        <p>You can mount disk image;</p>

        <pre>
        $ sudo modprobe nbd
        $ sudo qemu-nbd -c /dev/nbd0 /crux-img.qcow2
        </pre>

        <p>To disconnect image disk (ndb);</p>

        <pre>
        $ sudo qemu-nbd -d /dev/nbd0
        </pre>

        <p>Information about preparing
        <a href="../core/install.html#step2">partitions</a>
        and <a href="storage.html">storage</a> administration.
        You can use image as a normal disk, example how
        to use parted to create a gpt system table;</p>

        <pre>
        # parted --script $DEV \
            mklabel gpt \
            mkpart ESP fat32 1MiB 120MiB \
            mkpart primary ext4 120MiB 720MiB \
            mkpart primary ext4 720MiB 2720MiB \
            mkpart primary ext4 2720MiB 5000MiB \
            set 1 boot on
        </pre>

        <pre>
        # kpartx -a -s -l -u /dev/nbd0
        </pre>

        <p>Use /dev/mapper/$(name_of_device) to assign correct blocks;</p>

        <pre>
        # mkfs.fat -F 32 $BLK_EFI
        # mkfs.ext4 $BLK_BOOT
        # mkfs.ext4 $BLK_ROOT
        # mkfs.ext4 $BLK_VAR
        </pre>

        <p>Mount partition;</p>

        <pre>
        # mount $BLK_ROOT $CHROOT
        # mkdir -p $CHROOT/boot
        # mount $BLK_BOOT $CHROOT/boot
        # mkdir -p $CHROOT/boot/efi
        # mount $BLK_EFI $CHROOT/boot/efi
        # mkdir -p $CHROOT/var
        # mount $BLK_VAR $CHROOT/var
        </pre>

        <h2 id="net">2. Network</h2>

        <p>Network configuration;</p>

        <dl>
            <dt>slirp</dt>
            <dd>Default virtual NAT'd network.</dd>
            <dt>tun/tap</dt>
            <dd>Good performance to create virtually any type of network
            topology.</dd>
            <dt>vde</dt>
            <dd>The VDE networking backend.</dd>
        </dl>

        <pre>
        KERNEL=="tun", GROUP="kvm", MODE="0660", OPTIONS+="static_node=net/tun"
        </pre>


        <h3>2.1. Public Bridge</h3>

        <p>Create <a href="network.html#bridge">bridge</a>, create new
        tap and add it to bridge;</p>

        <pre>
        # DEV="br0"
        # TAP="tap1"
        </pre>

        <pre>
        # ip tuntap add ${TAP} mode tap group kvm
        # ip link set ${TAP} up
        </pre>

        <pre>
        # ip link set ${TAP} master ${DEV}
        </pre>

        <h3>2.2. Routing</h3>

        <p>Create interface with correct permissions set for kvm group.</p>

        <pre>
        # sysctl -w net.ipv4.ip_forward=1
        # iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
        </pre>

        <h2 id="guest">Guest System</h2>

        <p>Start qemu with 512 of ram, mydisk.img as disk and boot from iso</p>

        <p>See <a href="scripts/system-qemu.sh">scripts/system-qemu.sh</a>,
        as template. Run virtual machine that uses above tap device;</p>

        <pre>
        $ ISO=~/crux-3.2.iso
        $ IMG=~/crux-img.qcow2
        $ TAP="tap1"

        $ qemu-system-x86_64 \
            -enable-kvm \
            -m 1024 \
            -boot d \
            -cdrom ${ISO} \
            -hda ${IMG} \
            -net nic,model=virtio -net tap,ifname=${TAP},script=no,downscript=no
        </pre>

        <pre>
        $ ISO=~/crux-3.2.iso
        $ IMG=~/crux-img.qcow2

        $ qemu-system-x86_64 \
            -enable-kvm \
            -m 1024 \
            -boot d \
            -cdrom ${ISO} \
            -hda ${IMG} \
            -net nic,model=virtio -net tap,ifname=${TAP},script=no,downscript=no
        </pre>

        <a href="index.html">Tools Index</a>
        <p>This is part of the c9 Manual.
        Copyright (C) 2016
        c9 team.
        See the file <a href="../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
        for copying conditions.</p>
    </body>
</html>