用Qemu搭建x86_64学习环境

Posted 摩斯电码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Qemu搭建x86_64学习环境相关的知识,希望对你有一定的参考价值。

作者信息

作者:彭东林

邮箱:pengdonglin137@163.com

QQ:405728433

软件平台

主机: Ubuntu14.04 64位版本

模拟器:Qemu-2.8.0

Linux内核版本: Linux-4.10

Busybox版本:busybox-1.24.2

工具链: gcc

具备的功能

模拟一个双核或者单核的x86_64架构的系统,根文件系统用ramdisk的形式,跟Host之间采用NFS的方式实现文件共享。

正文

1、Qemu的编译安装

请参考博文用qemu搭建aarch64学习环境

2、工具链

Ubuntu系统自带的gcc

3、Linux内核编译

登录https://www.kernel.org/,   下载最新的Linux版本,目前最新的是Linux-4.10。 

下面是编译下面要用的kernel的命令:

1 #!/bin/bash
2 make O=out_x86_64 x86_64_defconfig
3 make O=out_x86_64 menuconfig
4 make O=out_x86_64 bzImage -j8
由于下面要用到ramdisk的启动方式,需要在kernel配置中支持:
1 General setup  --->
2     ----> [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
3 Device Drivers  --->
4     [*] Block devices  --->
5             <*>   RAM block device support
6             (65536) Default RAM disk size (kbytes)

这里我们给ramdisk设置的默认大小是64MB

4、制作根文件系统

登录https://busybox.net/downloads/下载要用的busybox版本,这里我下的是busybox-1.24.2

这里需要注意的是,由于我们的Host是Ubuntu14.04 64bit,所以用gcc默认编译出的文件只能用于x86_64系统,如果要编译出能在x86上面运行的程序,需要在编译的时候传递必要的参数。

执行make menuconfig,配置下面几项:

1 Busybox Settings  --->
2     Build Options  --->
3          [*] Build BusyBox as a static binary (no shared libs)
然后执行
1     make -j4
2     make install

5、制作ramdisk镜像

下面制作启动用的ramdisk,我把这个过程写成了脚本,如下:

 1 #!/bin/bash
 2 sudo rm -rf rootfs
 3 sudo rm -rf tmpfs
 4 sudo rm -rf ramdisk*
 5 sudo mkdir rootfs
 6 sudo cp ../busybox-1.24.2/_install/*  rootfs/ -raf
 7 sudo mkdir -p rootfs/proc/
 8 sudo mkdir -p rootfs/sys/
 9 sudo mkdir -p rootfs/tmp/
10 sudo mkdir -p rootfs/root/
11 sudo mkdir -p rootfs/var/
12 sudo mkdir -p rootfs/mnt/
13 sudo cp etc rootfs/ -arf
14 sudo mkdir -p rootfs/lib64
15 sudo cp -arf /lib/x86_64-linux-gnu/* rootfs/lib64/
16 sudo rm rootfs/lib/*.a
17 sudo strip rootfs/lib/*
18 sudo mkdir -p rootfs/dev/
19 sudo mknod rootfs/dev/tty1 c 4 1
20 sudo mknod rootfs/dev/tty2 c 4 2
21 sudo mknod rootfs/dev/tty3 c 4 3
22 sudo mknod rootfs/dev/tty4 c 4 4
23 sudo mknod rootfs/dev/console c 5 1
24 sudo mknod rootfs/dev/null c 1 3
25 sudo dd if=/dev/zero of=ramdisk bs=1M count=32
26 sudo mkfs.ext4 -F ramdisk
27 sudo mkdir -p tmpfs
28 sudo mount -t ext4 ramdisk ./tmpfs/  -o loop
29 sudo cp -raf rootfs/*  tmpfs/
30 sudo umount tmpfs
31 sudo gzip --best -c ramdisk > ramdisk.gz
上面需要注意的是第20行,拷贝的是x86_64的库。

用到的文件可以到这里下载。

6、Qemu支持网络

请参考博客:

用Qemu模拟vexpress-a9 --- 配置 qemu 的网络功能

用Qemu模拟vexpress-a9 (三)--- 实现用u-boot引导Linux内核

7、运行脚本

下面是运行脚本,支持网络

1 sudo qemu-system-x86_64 \\
2     -smp 2 \\
3     -m 1024M \\
4     -kernel ./linux-4.10/out_x86_64/arch/x86_64/boot/bzImage \\
5     -nographic \\
6     -append "root=/dev/ram0 rw rootfstype=ext4 console=ttyS0 init=/linuxrc" \\
7     -initrd ./rootfs/ramdisk.gz \\
8     -net nic,vlan=0 -net tap,vlan=0,ifname=tap0

8、hello world

hello.c

1 #include <stdio.h>
2 int main(int argc, const char *argv[])
3 {
4     printf("Hello world.\\n");
5     return 0;
6 }

Makefile

1 hello_x86_64:hello.c
2     gcc $^ -o $@
3 clean:
4     $(RM) hello_x86_64 *.o
5 .PHONY: clean

9、启动log

  1 $./run.sh 
  2 sudo tunctl -u root -t tap0
  3 TUNSETIFF: Device or resource busy
  4 sudo ifconfig tap0 0.0.0.0 promisc up
  5 sudo brctl addif br0 tap0
  6 brctl show
  7 bridge name    bridge id        STP enabled    interfaces
  8 br0        8000.a6e1e3920655    no        eth0
  9                             tap0
 10 [    0.000000] Linux version 4.10.0 (pengdonglin@pengdonglin-dell) (gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) ) #1 SMP Sat Feb 25 01:41:09 CST 2017
 11 [    0.000000] Command line: root=/dev/ram0 rw rootfstype=ext4 console=ttyS0 init=/linuxrc
 12 [    0.000000] x86/fpu: Legacy x87 FPU detected.
 13 [    0.000000] e820: Bios-provided physical RAM map:
 14 [    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
 15 [    0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
 16 [    0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
 17 [    0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000003ffdffff] usable
 18 [    0.000000] BIOS-e820: [mem 0x000000003ffe0000-0x000000003fffffff] reserved
 19 [    0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
 20 [    0.000000] NX (Execute Disable) protection: active
 21 [    0.000000] SMBIOS 2.8 present.
 22 [    0.000000] DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.10.1-0-g8891697-prebuilt.qemu-project.org 04/01/2014
 23 [    0.000000] e820: last_pfn = 0x3ffe0 max_arch_pfn = 0x400000000
 24 [    0.000000] x86/PAT: Configuration [0-7]: WB  WC  UC- UC  WB  WC  UC- WT  
 25 [    0.000000] found SMP MP-table at [mem 0x000f6a90-0x000f6a9f] mapped at [ffff8800000f6a90]
 26 [    0.000000] Scanning 1 areas for low memory corruption
 27 [    0.000000] RAMDISK: [mem 0x3f76f000-0x3ffdffff]
 28 [    0.000000] ACPI: Early table checksum verification disabled
 29 [    0.000000] ACPI: RSDP 0x00000000000F6860 000014 (v00 BOCHS )
 30 [    0.000000] ACPI: RSDT 0x000000003FFE1936 000030 (v01 BOCHS  BXPCRSDT 00000001 BXPC 00000001)
 31 [    0.000000] ACPI: FACP 0x000000003FFE180A 000074 (v01 BOCHS  BXPCFACP 00000001 BXPC 00000001)
 32 [    0.000000] ACPI: DSDT 0x000000003FFE0040 0017CA (v01 BOCHS  BXPCDSDT 00000001 BXPC 00000001)
 33 [    0.000000] ACPI: FACS 0x000000003FFE0000 000040
 34 [    0.000000] ACPI: APIC 0x000000003FFE187E 000080 (v01 BOCHS  BXPCAPIC 00000001 BXPC 00000001)
 35 [    0.000000] ACPI: HPET 0x000000003FFE18FE 000038 (v01 BOCHS  BXPCHPET 00000001 BXPC 00000001)
 36 [    0.000000] No NUMA configuration found
 37 [    0.000000] Faking a node at [mem 0x0000000000000000-0x000000003ffdffff]
 38 [    0.000000] NODE_DATA(0) allocated [mem 0x3f76b000-0x3f76efff]
 39 [    0.000000] Zone ranges:
 40 [    0.000000]   DMA      [mem 0x0000000000001000-0x0000000000ffffff]
 41 [    0.000000]   DMA32    [mem 0x0000000001000000-0x000000003ffdffff]
 42 [    0.000000]   Normal   empty
 43 [    0.000000] Movable zone start for each node
 44 [    0.000000] Early memory node ranges
 45 [    0.000000]   node   0: [mem 0x0000000000001000-0x000000000009efff]
 46 [    0.000000]   node   0: [mem 0x0000000000100000-0x000000003ffdffff]
 47 [    0.000000] Initmem setup node 0 [mem 0x0000000000001000-0x000000003ffdffff]
 48 [    0.000000] ACPI: PM-Timer IO Port: 0x608
 49 [    0.000000] ACPI: LAPIC_NMI (acpi_id[0xff] dfl dfl lint[0x1])
 50 [    0.000000] IOAPIC[0]: apic_id 0, version 32, address 0xfec00000, GSI 0-23
 51 [    0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 0 global_irq 2 dfl dfl)
 52 [    0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 5 global_irq 5 high level)
 53 [    0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 9 high level)
 54 [    0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 10 global_irq 10 high level)
 55 [    0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 11 global_irq 11 high level)
 56 [    0.000000] Using ACPI (MADT) for SMP configuration information
 57 [    0.000000] ACPI: HPET id: 0x8086a201 base: 0xfed00000
 58 [    0.000000] smpboot: Allowing 2 CPUs, 0 hotplug CPUs
 59 [    0.000000] PM: Registered nosave memory: [mem 0x00000000-0x00000fff]
 60 [    0.000000] PM: Registered nosave memory: [mem 0x0009f000-0x0009ffff]
 61 [    0.000000] PM: Registered nosave memory: [mem 0x000a0000-0x000effff]
 62 [    0.000000] PM: Registered nosave memory: [mem 0x000f0000-0x000fffff]
 63 [    0.000000] e820: [mem 0x40000000-0xfffbffff] available for PCI devices
 64 [    0.000000] clocksource: refined-jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1910969940391419 ns
 65 [    0.000000] setup_percpu: NR_CPUS:64 nr_cpumask_bits:64 nr_cpu_ids:2 nr_node_ids:1
 66 [    0.000000] percpu: Embedded 34 pages/cpu @ffff88003f400000 s100824 r8192 d30248 u1048576
 67 [    0.000000] Built 1 zonelists in Node order, mobility grouping on.  Total pages: 257897
 68 [    0.000000] Policy zone: DMA32
 69 [    0.000000] Kernel command line: root=/dev/ram0 rw rootfstype=ext4 console=ttyS0 init=/linuxrc
 70 [    0.000000] PID hash table entries: 4096 (order: 3, 32768 bytes)
 71 [    0.000000] Memory: 1004968K/1048056K available (9278K kernel code, 1238K rwdata, 2804K rodata, 1180K init, 800K bss, 43088K reserved, 0K cma-reserved)
 72 [    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=2, Nodes=1
 73 [    0.000000] Hierarchical RCU implementation.
 74 [    0.000000]     Build-time adjustment of leaf fanout to 64.
 75 [    0.000000]     RCU restricting CPUs from NR_CPUS=64 to nr_cpu_ids=2.
 76 [    0.000000] RCU: Adjusting geometry for rcu_fanout_leaf=64, nr_cpu_ids=2
 77 [    0.000000] NR_IRQS:4352 nr_irqs:440 16
 78 [    0.000000] Console: colour VGA+ 80x25
 79 [    0.000000] console [ttyS0] enabled
 80 [    0.000000] clocksource: hpet: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604467 ns
 81 [    0.000000] tsc: Unable to calibrate against PIT
 82 [    0.000000] tsc: using HPET reference calibration
 83 [    0.000000] tsc: Detected 3591.691 MHz processor
 84 [    0.000000] tsc: Marking TSC unstable due to TSCs unsynchronized
 85 [    0.009000] Calibrating delay loop (skipped), value calculated using timer frequency.. 7183.38 BogoMIPS (lpj=3591691)
 86 [    0.009000] pid_max: default: 32768 minimum: 301
 87 [    0.009000] ACPI: Core revision 20160930
 88 [    0.026676] ACPI: 1 ACPI AML tables successfully acquired and loaded
 89 [    0.027506] Security Framework initialized
 90 [    0.027662] SELinux:  Initializing.
 91 [    0.028700] Dentry cache hash table entries: 131072 (order: 8, 1048576 bytes)
 92 [    0.030126] Inode-cache hash table entries: 65536 (order: 7, 524288 bytes)
 93 [    0.032123] Mount-cache hash table entries: 2048 (order: 2, 16384 bytes)
 94 [    0.032317] Mountpoint-cache hash table entries: 2048 (order: 2, 16384 bytes)
 95 [    0.041861] mce: CPU supports 10 MCE banks
 96 [    0.042731] Last level iTLB entries: 4KB 0, 2MB 0, 4MB 0
 97 [    0.042871] Last level dTLB entries: 4KB 0, 2MB 0, 4MB 0, 1GB 0
 98 [    0.045503] Freeing SMP alternatives memory: 36K
 99 [    0.053762] smpboot: Max logical packages: 2
100 [    0.056935] ..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1
101 [    0.067000] smpboot: CPU0: AMD QEMU Virtual CPU version 2.5+ (family: 0x6, model: 0x6, stepping: 0x3)
102 [    0.069535] Performance Events: PMU not available due to virtualization, using software events only.
103 [    0.074611] Huh? What family is it: 0x6?!
104 [    0.076109] smp: Bringing up secondary CPUs ...
105 [    0.078263] x86: Booting SMP configuration:
106 [    0.078388] .... node  #0, CPUs:      #1
107 [    0.154337] smp: Brought up 1 node, 2 CPUs
108 [    0.155166] smpboot: Total of 2 processors activated (22280.71 BogoMIPS)
109 [    0.165179] devtmpfs: initialized
110 [    0.173105] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1911260446275000 ns
111 [    0.173978] futex hash table entries: 512 (order: 3, 32768 bytes)
112 [    0.179029] RTC time: 10:20:14, date: 02/25/17
113 [    0.181124] kworker/u4:0 (20) used greatest stack depth: 14472 bytes left
114 [    0.184192] NET: Registered protocol family 16
115 [    0.198105] cpuidle: using governor menu
116 [    0.198283] PCCT header not found.
117 [    0.199162] ACPI: bus type PCI registered
118 [    0.203021] PCI: Using configuration type 1 for base access
119 [    0.206635] mtrr: your CPUs had inconsistent fixed MTRR settings
120 [    0.206808] mtrr: your CPUs had inconsistent variable MTRR settings
121 [    0.207207] mtrr: your CPUs had inconsistent MTRRdefType settings
122 [    0.207207] mtrr: probably your BIOS does not setup all CPUs.
123 [    0.207289] mtrr: corrected configuration.
124 [    0.210240] kworker/u4:1 (49) used greatest stack depth: 14160 bytes left
125 [    0.332230] HugeTLB registered 2 MB page size, pre-allocated 0 pages
126 [    0.335289] ACPI: Added _OSI(Module Device)
127 [    0.335405] ACPI: Added _OSI(Processor Device)
128 [    0.335598] ACPI: Added _OSI(3.0 _SCP Extensions)
129 [    0.335706] ACPI: Added _OSI(Processor Aggregator Device)
130 [    0.352062] ACPI: Interpreter enabled
131 [    0.352801] ACPI: (supports S0 S3 S4 S5)
132 [    0.352921] ACPI: Using IOAPIC for interrupt routing
133 [    0.353377] PCI: Using host bridge windows from ACPI; if necessary, use "pci=nocrs" and report a bug
134 [    0.405360] ACPI: PCI Root Bridge [PCI0] (domain 0000 [bus 00-ff])
135 [    0.405851] acpi PNP0A03:00: _OSC: OS supports [ASPM ClockPM Segments MSI]
136 [    0.406182] acpi PNP0A03:00: _OSC failed (AE_NOT_FOUND); disabling ASPM
137 [    0.406675] acpi PNP0A03:00: fail to add MMCONFIG information, can\'t access extended PCI configuration space under this bridge.
138 [    0.408902] PCI host bridge to bus 0000:00
139 [    0.409130] pci_bus 0000:00: root bus resource [io  0x0000-0x0cf7 window]
140 [    0.409375] pci_bus 0000:00: root bus resource [io  0x0d00-0xffff window]
141 [    0.409544] pci_bus 0000:00: root bus resource [mem 0x000a0000-0x000bffff window]
142 [    0.409704] pci_bus 0000:00: root bus resource [mem 0x40000000-0xfebfffff window]
143 [    0.410110] pci_bus 0000:00: root bus resource [bus 00-ff]
144 [    0.418473] pci 0000:00:01.1: legacy IDE quirk: reg 0x10: [io  0x01f0-0x01f7]
145 [    0.418682] pci 0000:00:01.1: legacy IDE quirk: reg 0x14: [io  0x03f6]
146 [    0.419036] pci 0000:00:01.1: legacy IDE quirk: reg 0x18: [io  0x0170-0x0177]
147 [    0.419211] pci 0000:00:01.1: legacy IDE quirk: reg 0x1c: [io  0x0376]
148 [    0.421322] pci 0000:00:01.3: quirk: [io  0x0600-0x063f] claimed by PIIX4 ACPI
149 [    0.421586] pci 0000:00:01.3: quirk: [io  0x0700-0x070f] claimed by PIIX4 SMB
150 [    0.446234] ACPI: PCI Interrupt Link [LNKA] (IRQs 5 *10 11)
151 [    0.447156] ACPI: PCI Interrupt Link [LNKB] (IRQs 5 *10 11)
152 [    0.447840] ACPI: PCI Interrupt Link [LNKC] (IRQs 5 10 *11)
153 [    0.448645] ACPI: PCI Interrupt Link [LNKD] (IRQs 5 10 *11)
154 [    0.449094] ACPI: PCI Interrupt Link [LNKS] (IRQs *9)
155 [    0.451577] ACPI: Enabled 3 GPEs in block 00 to 0F
156 [    0.456364] pci 0000:00:02.0: vgaarb: setting as boot VGA device
157 [    0.456785] pci 0000:00:02.0: vgaarb: VGA device added: decodes=io+mem,owns=io+mem,locks=none
158 [    0.457063] pci 0000:00:02.0: vgaarb: bridge control possible
159 [    0.457190] vgaarb: loaded
160 [    0.459405] SCSI subsystem initialized
161 [    0.461363] ACPI: bus type USB registered
162 [    0.461864] usbcore: registered new interface driver usbfs
163 [    0.462260] usbcore: registered new interface driver hub
164 [    0.463169] usbcore: registered new device driver usb
165 [    0.464285] pps_core: LinuxPPS API ver. 1 registered
166 [    0.464402] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it>
167 [    0.465114] PTP clock support registered
168 [    arm搭建x86运行时

58.搭建KVM虚拟化工具(YUM)

qemu-system-x86_64方式创建KVM虚拟机

qemu-system-x86_64方式创建KVM虚拟机

CentOS6上搭建lxr系统以方便浏览项目源代码

SVN 环境搭建