From patchwork Sun Jan 17 21:04:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kasper Dupont X-Patchwork-Id: 8050981 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 796BB9F8AA for ; Sun, 17 Jan 2016 21:40:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BA0D62024F for ; Sun, 17 Jan 2016 21:40:10 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6186B2022D for ; Sun, 17 Jan 2016 21:40:09 +0000 (UTC) Received: from localhost ([::1]:56020 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aKv3Q-0002EK-HK for patchwork-qemu-devel@patchwork.kernel.org; Sun, 17 Jan 2016 16:40:08 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58318) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aKuUu-0002M3-NY for qemu-devel@nongnu.org; Sun, 17 Jan 2016 16:04:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aKuUq-0004ue-Mq for qemu-devel@nongnu.org; Sun, 17 Jan 2016 16:04:28 -0500 Received: from vq7.netiter.net ([78.47.169.56]:38967) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aKuUq-0004uI-Gf for qemu-devel@nongnu.org; Sun, 17 Jan 2016 16:04:24 -0500 Received: from localhost ([127.0.0.1] helo=fjtcb.17.jan.2016.kasperd.net) by vq7.netiter.net with smtp (Exim 4.82) (envelope-from ) id 1aKuUk-0001Gw-Rr; Sun, 17 Jan 2016 22:04:19 +0100 Date: Sun, 17 Jan 2016 22:04:13 +0100 From: Kasper Dupont To: qemu-devel@nongnu.org Message-ID: <20160117210413.GA30122@colin.search.kasperd.net> Mime-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.4.2.3i X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 78.47.169.56 X-Mailman-Approved-At: Sun, 17 Jan 2016 16:40:02 -0500 Subject: [Qemu-devel] Using directory as initrd X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP I would like to use a directory as initrd file without having to write it to an initrd file each time I have changed anything in that directory. I have written code to pipe an initrd directly from cpio to qemu. Do you have any feedback on the attached patch? diff -up qemu-2.0.0+dfsg/hw/core/loader.c.orig qemu-2.0.0+dfsg/hw/core/loader.c --- qemu-2.0.0+dfsg/hw/core/loader.c.orig 2014-04-17 15:30:59.000000000 +0200 +++ qemu-2.0.0+dfsg/hw/core/loader.c 2016-01-17 19:38:01.505138214 +0100 @@ -137,6 +137,70 @@ void pstrcpy_targphys(const char *name, } } +/* return the size or -1 if error */ +int load_initrd(const char *filename, uint8_t **addr) +{ + pid_t child = 0; + int fd, size = 0, buffer_size = 4096; + fd = open(filename, O_RDONLY | O_BINARY); + if (fd < 0) + return -1; + + *addr = malloc(buffer_size); + + while(*addr) { + int bytes_read = read(fd, (*addr) + size, buffer_size - size); + + if (bytes_read == 0) { + close(fd); + if (child) { + waitpid(child, NULL, 0); + } + return size; + } + + if (bytes_read > 0) { + size += bytes_read; + } else { + int pipefd[2]; + + if (errno != EISDIR) + return -1; + + assert(!child); + + if (pipe(pipefd)) + return -1; + + child = fork(); + if (child == -1) + return -1; + + if (!child) { + if(fchdir(fd)) + error(1, errno, "fchdir failed"); + if(dup2(pipefd[1], 1) == -1) + error(1, errno, "dup2 failed"); + execlp("/bin/sh", "/bin/sh", "-c", + "find . | cpio --quiet -R 0:0 -o -H newc", + NULL); + error(1, errno, "/bin/sh"); + } + + close(fd); + close(pipefd[1]); + fd = pipefd[0]; + } + + if (size == buffer_size) { + buffer_size <<= 1; + *addr = realloc(*addr, buffer_size); + } + } + + return -1; +} + /* A.OUT loader */ struct exec diff -up qemu-2.0.0+dfsg/hw/i386/pc.c.orig qemu-2.0.0+dfsg/hw/i386/pc.c --- qemu-2.0.0+dfsg/hw/i386/pc.c.orig 2014-04-17 15:30:59.000000000 +0200 +++ qemu-2.0.0+dfsg/hw/i386/pc.c 2016-01-17 19:16:04.704258817 +0100 @@ -829,12 +829,13 @@ static void load_linux(FWCfgState *fw_cf /* load initrd */ if (initrd_filename) { + uint8_t *initrd_load_buffer; if (protocol < 0x200) { fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n"); exit(1); } - initrd_size = get_image_size(initrd_filename); + initrd_size = load_initrd(initrd_filename, &initrd_load_buffer); if (initrd_size < 0) { fprintf(stderr, "qemu: error reading initrd %s: %s\n", initrd_filename, strerror(errno)); @@ -844,7 +845,8 @@ static void load_linux(FWCfgState *fw_cf initrd_addr = (initrd_max-initrd_size) & ~4095; initrd_data = g_malloc(initrd_size); - load_image(initrd_filename, initrd_data); + memcpy(initrd_data, initrd_load_buffer, initrd_size); + free(initrd_load_buffer); fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); diff -up qemu-2.0.0+dfsg/include/hw/loader.h.orig qemu-2.0.0+dfsg/include/hw/loader.h --- qemu-2.0.0+dfsg/include/hw/loader.h.orig 2014-04-17 15:30:59.000000000 +0200 +++ qemu-2.0.0+dfsg/include/hw/loader.h 2016-01-17 19:15:30.688236909 +0100 @@ -13,6 +13,7 @@ */ int get_image_size(const char *filename); int load_image(const char *filename, uint8_t *addr); /* deprecated */ +int load_initrd(const char *filename, uint8_t **addr); int load_image_targphys(const char *filename, hwaddr, uint64_t max_sz);