From 9c63ab239f8bc07860da29dfd74d14f144b14ab4 Mon Sep 17 00:00:00 2001 From: Chih-Wei Huang Date: Mon, 24 Apr 2017 10:57:16 +0800 Subject: [PATCH] Add a script to run Android-x86 in QEMU The qemu-android script is installed as a part of the rpm. Users can run the script directly to launch Android-x86 in the QEMU virtual machine. The script could also be used on a build host to run the just built images in the $OUT directory. --- Android.mk | 6 +++--- initrd/init | 14 +++++++++---- initrd/scripts/2-mount | 5 ++++- rpm/qemu-android | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ rpm/rpm.spec | 6 +++++- 5 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 rpm/qemu-android diff --git a/Android.mk b/Android.mk index 7712524..ad4866b 100644 --- a/Android.mk +++ b/Android.mk @@ -79,13 +79,13 @@ $(ISO_IMAGE): $(boot_dir) $(BUILT_IMG) $(hide) isohybrid --uefi $@ || echo -e "isohybrid not found.\nInstall syslinux 4.0 or higher if you want to build a usb bootable iso." @echo -e "\n\n$@ is built successfully.\n\n" -rpm: $(LOCAL_PATH)/rpm/rpm.spec $(BUILT_IMG) +rpm: $(wildcard $(LOCAL_PATH)/rpm/*) $(BUILT_IMG) @echo ----- Making an rpm ------ - OUT=$(abspath $(PRODUCT_OUT)); mkdir -p $$OUT/rpm/BUILD; rm -rf mv $$OUT/rpm/RPMS/*; \ + OUT=$(abspath $(PRODUCT_OUT)); mkdir -p $$OUT/rpm/BUILD; rm -rf $$OUT/rpm/RPMS/*; $(ACP) $< $$OUT; \ rpmbuild -bb --target=$(if $(filter x86,$(TARGET_ARCH)),i686,x86_64) -D"cmdline $(BOARD_KERNEL_CMDLINE)" \ -D"_topdir $$OUT/rpm" -D"_sourcedir $$OUT" -D"systemimg $(notdir $(systemimg))" -D"ver $(VER)" \ $(if $(BUILD_NAME_VARIANT),-D"name $(BUILD_NAME_VARIANT)") \ - -D"install_prefix $(if $(INSTALL_PREFIX),$(INSTALL_PREFIX),android-$(VER))" $<; \ + -D"install_prefix $(if $(INSTALL_PREFIX),$(INSTALL_PREFIX),android-$(VER))" $(filter %.spec,$^); \ mv $$OUT/rpm/RPMS/*/*.rpm $$OUT # Note: requires dosfstools diff --git a/initrd/init b/initrd/init index a91e4ad..32d6a4e 100755 --- a/initrd/init +++ b/initrd/init @@ -3,7 +3,7 @@ # By Chih-Wei Huang # and Thorsten Glaser # -# Last updated 2017/03/24 +# Last updated 2017/04/23 # # License: GNU Public License # We explicitely grant the right to use the scripts @@ -84,10 +84,14 @@ check_root() mkdir /mnt/iso mount -o loop /iso/$iso /mnt/iso SRC=iso - elif [ ! -e /mnt/$SRC/$RAMDISK ]; then + fi + if [ -e /mnt/$SRC/$RAMDISK ]; then + zcat /mnt/$SRC/$RAMDISK | cpio -id > /dev/null + elif [ -b /dev/$RAMDISK ]; then + zcat /dev/$RAMDISK | cpio -id > /dev/null + else return 1 fi - zcat /mnt/$SRC/$RAMDISK | cpio -id > /dev/null if [ -e /mnt/$SRC/system.sfs ]; then mount -o loop,noatime /mnt/$SRC/system.sfs /sfs mount -o loop,noatime /sfs/system.img system @@ -97,6 +101,8 @@ check_root() elif [ -d /mnt/$SRC/system ]; then remount_rw mount --bind /mnt/$SRC/system system + elif [ -e /mnt/build.prop ]; then + mount --bind /mnt system else rm -rf * return 1 @@ -128,7 +134,7 @@ debug_shell() echo -n Detecting Android-x86... [ -z "$SRC" -a -n "$BOOT_IMAGE" ] && SRC=`dirname $BOOT_IMAGE` -[ -z "$RAMDISK" ] && RAMDISK=ramdisk.img +[ -z "$RAMDISK" ] && RAMDISK=ramdisk.img || RAMDISK=${RAMDISK##/dev/} for c in `cat /proc/cmdline`; do case $c in diff --git a/initrd/scripts/2-mount b/initrd/scripts/2-mount index 822edde..9cce018 100644 --- a/initrd/scripts/2-mount +++ b/initrd/scripts/2-mount @@ -1,6 +1,6 @@ # # By Chih-Wei Huang -# Last updated 2017/01/10 +# Last updated 2017/04/23 # # License: GNU Public License # We explicitely grant the right to use the scripts @@ -15,6 +15,9 @@ mount_data() if [ -b "/dev/$blk" ]; then [ ! -e /dev/block/$blk ] && ln /dev/$blk /dev/block mount -o noatime /dev/block/$blk data + elif [ "$DATA" = "9p" ]; then + modprobe 9pnet_virtio + mount -t 9p -o trans=virtio data data -oversion=9p2000.L,posixacl,cache=loose else remount_rw mkdir -p /mnt/$SRC/$DATA diff --git a/rpm/qemu-android b/rpm/qemu-android new file mode 100644 index 0000000..4fc00a6 --- /dev/null +++ b/rpm/qemu-android @@ -0,0 +1,53 @@ +#!/bin/bash +# By Chih-Wei Huang +# License: GNU Generic Public License v2 + +continue_or_stop() +{ + echo "Please Enter to continue or Ctrl-C to stop." + read +} + +QEMU_ARCH=`uname -m` +QEMU=qemu-system-${QEMU_ARCH} + +which $QEMU > /dev/null 2>&1 || QEMU=qemu-system-i386 +if ! which $QEMU > /dev/null 2>&1; then + echo -e "Please install $QEMU to run the program.\n" + exit 1 +fi + +cd ${OUT:-ANDROID_ROOT} + +[ -e system.img ] && SYSTEMIMG=system.img || SYSTEMIMG=system.sfs + +if [ -d data ]; then + if [ `id -u` -eq 0 ]; then + DATA="-virtfs local,id=data,path=data,security_model=passthrough,mount_tag=data" + DATADEV='DATA=9p' + else + echo -e "\n$(realpath data) subfolder exists.\nIf you want to save data to it, run $0 as root:\n\n$ sudo $0\n" + continue_or_stop + fi +elif [ -e data.img ]; then + if [ -w data.img ]; then + DATA="-drive index=2,if=virtio,id=data,file=data.img" + DATADEV='DATA=vdc' + else + echo -e "\n$(realpath data.img) exists but is not writable.\nPlease grant the write permission if you want to save data to it.\n" + continue_or_stop + fi +fi + +$QEMU -enable-kvm -vga std -sdl \ + -kernel kernel \ + -append "CMDLINE console=ttyS0 RAMDISK=vdb $DATADEV" \ + -initrd initrd.img \ + -m 2048 -smp 2 -cpu host \ + -soundhw ac97 \ + -net nic,model=e1000 -net user \ + -serial mon:stdio \ + -boot menu=on \ + -drive index=0,if=virtio,id=system,file=$SYSTEMIMG,format=raw,readonly \ + -drive index=1,if=virtio,id=ramdisk,file=ramdisk.img,format=raw,readonly \ + $DATA $@ diff --git a/rpm/rpm.spec b/rpm/rpm.spec index 0842ef2..e5adabd 100644 --- a/rpm/rpm.spec +++ b/rpm/rpm.spec @@ -6,6 +6,7 @@ Source1: kernel Source2: initrd.img Source3: ramdisk.img Source4: %{systemimg} +Source5: qemu-android License: Apache Public License / GPLv2 Group: Operating system/Android URL: http://www.android-x86.org @@ -22,8 +23,10 @@ or later. %install rm -rf %{buildroot} -mkdir -p %{buildroot}/%{install_prefix} +mkdir -p %{buildroot}/%{install_prefix} %{buildroot}%{_bindir} install -m644 %{S:1} %{S:2} %{S:3} %{S:4} %{buildroot}/%{install_prefix} +install -m755 %{S:5} %{buildroot}%{_bindir} +sed -i "s|ANDROID_ROOT|/%{install_prefix}|; s|CMDLINE|%{cmdline}|" %{buildroot}%{_bindir}/`basename %{S:5}` %post . /etc/os-release @@ -69,3 +72,4 @@ rm -rf %{buildroot} %files /%{install_prefix}/* +%{_bindir}/* -- 2.11.0