001/*
002 * To change this license header, choose License Headers in Project Properties.
003 * To change this template file, choose Tools | Templates
004 * and open the template in the editor.
005 */
006package org.anarres.qemu.examples;
007
008import com.google.auto.service.AutoService;
009import com.jcraft.jsch.ChannelExec;
010import com.jcraft.jsch.ChannelSftp;
011import com.jcraft.jsch.JSch;
012import com.jcraft.jsch.Session;
013import java.io.ByteArrayInputStream;
014import java.net.URI;
015import org.anarres.qemu.exec.QEmuArchitecture;
016import org.anarres.qemu.exec.QEmuCommandLine;
017import org.anarres.qemu.exec.QEmuDisplayOption;
018import org.anarres.qemu.exec.QEmuMemoryOption;
019import org.anarres.qemu.exec.VncDisplay;
020import org.anarres.qemu.exec.recipe.QEmuMonitorRecipe;
021import org.anarres.qemu.exec.recipe.QEmuPerformanceRecipe;
022import org.anarres.qemu.exec.recipe.QEmuVirtioDriveRecipe;
023import org.anarres.qemu.image.QEmuImage;
024import org.anarres.qemu.manager.QEmuProcess;
025import org.anarres.qemu.qapi.api.DeviceDelCommand;
026import org.anarres.qemu.qapi.common.QApiConnection;
027
028/**
029 *
030 * @author shevek
031 */
032@AutoService(QEmuExample.class)
033public class QEmuRAIDExample extends AbstractQEmuExample {
034
035    @Override
036    public void invoke(String[] args) throws Exception {
037        QEmuImage root = newImage("root", URI.create("http://ubuntu.com/cloud.img"));
038        QEmuImage sdb = newImage("sdb", 1, QEmuMemoryOption.Magnitude.GIGA);
039        QEmuImage sdc = newImage("sdc", 1, QEmuMemoryOption.Magnitude.GIGA);
040
041        QEmuCommandLine line = new QEmuCommandLine(QEmuArchitecture.x86_64);
042        line.addOptions(
043                new QEmuPerformanceRecipe(),
044                new QEmuMonitorRecipe(4447),
045                new QEmuDisplayOption(new VncDisplay.Socket(4)),
046                new QEmuVirtioDriveRecipe(line.getAllocator(), root),
047                new QEmuVirtioDriveRecipe(line.getAllocator(), sdb),
048                new QEmuVirtioDriveRecipe(line.getAllocator(), sdc)
049        );
050
051        QEmuProcess process = manager.execute(line);
052        QApiConnection connection = process.getConnection();
053
054        JSch jsch = new JSch();
055        Session session = jsch.getSession("root", "10.42.43.2");
056        session.connect();
057
058        UPLOAD:
059        {
060            ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp-server");
061            sftp.connect();
062            sftp.put(new ByteArrayInputStream(new byte[]{}), "/root/script0.sh");
063            sftp.put(new ByteArrayInputStream(new byte[]{}), "/root/script1.sh");
064            sftp.disconnect();
065        }
066
067        SETUP:
068        {
069            ChannelExec exec = (ChannelExec) session.openChannel("exec");
070            exec.setCommand("/root/script0.sh");
071            exec.connect();
072            exec.disconnect();
073        }
074
075        connection.call(new DeviceDelCommand("virtio-disk-2"));
076
077        TEST:
078        {
079            ChannelExec exec = (ChannelExec) session.openChannel("exec");
080            exec.setCommand("/root/script1.sh");
081            exec.connect();
082            exec.disconnect();
083        }
084
085        process.destroy();
086    }
087}