001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package org.anarres.qemu.exec;
006
007import java.io.File;
008import java.util.List;
009import javax.annotation.Nonnegative;
010import javax.annotation.Nonnull;
011import org.anarres.qemu.exec.host.disk.Disk;
012import org.anarres.qemu.exec.host.disk.FileDisk;
013import org.anarres.qemu.exec.recipe.QEmuVirtioDriveRecipe;
014import org.anarres.qemu.exec.util.QEmuIdAllocator;
015import org.anarres.qemu.image.QEmuImageFormat;
016
017/**
018 * A disk device backend, usually paired with a {@link QEmuDeviceOption} frontend.
019 *
020 * @see QEmuVirtioDriveRecipe
021 * @see QEmuDeviceOption
022 * @author shevek
023 */
024public class QEmuDriveOption extends AbstractQEmuOption {
025
026    // file=file,index=0,media=disk
027    public static enum Interface {
028
029        ide, scsi, sd, mtd, floppy, pflash, virtio, none
030    }
031
032    public static enum Media {
033
034        disk, cdrom
035    }
036
037    public static enum Cache {
038
039        none, writeback, unsafe, directsync, writethrough
040    }
041
042    public static enum Aio {
043
044        threads("threads"),
045        _native("native");
046        private final String text;
047        /* pp */ Aio(String text) {
048            this.text = text;
049        }
050
051        @Override
052        public String toString() {
053            return text;
054        }
055    }
056
057    public static enum Discard {
058
059        off, on
060    }
061
062    public static enum ErrorAction {
063
064        ignore, stop, report, enospc
065    }
066
067    public static enum CopyOnRead {
068
069        off, on
070    }
071    public Disk disk;
072    // bus, unit
073    public String id;
074    public int index;
075    public Interface iface;
076    // bus=, unit=
077    // addr= for virtio only?
078    public QEmuImageFormat format;
079    public Media media;
080    // cyls=c,heads=h,secs=s[,trans=t]
081    // snapshot=on|off
082    public Cache cache;
083    public Aio aio;
084    public Discard discard;
085    public ErrorAction werror;
086    public ErrorAction rerror;
087    public boolean readonly;
088    public CopyOnRead copyOnRead;
089
090    public QEmuDriveOption() {
091    }
092
093    public QEmuDriveOption(int index, Disk disk) {
094        this.disk = disk;
095        this.index = index;
096    }
097
098    public QEmuDriveOption(int index, File file) {
099        this(index, new FileDisk(file));
100    }
101
102    public QEmuDriveOption(int index, String path) {
103        this(index, new FileDisk(path));
104    }
105
106    public QEmuDriveOption(@Nonnull QEmuIdAllocator allocator, Disk disk) {
107        this(allocator.newDriveIndex(), disk);
108    }
109
110    public QEmuDriveOption(@Nonnull QEmuIdAllocator allocator, File file) {
111        this(allocator, new FileDisk(file));
112    }
113
114    public QEmuDriveOption(@Nonnull QEmuIdAllocator allocator, String path) {
115        this(allocator, new FileDisk(path));
116    }
117
118    @Nonnull
119    public QEmuDriveOption withDisk(@Nonnull Disk disk) {
120        this.disk = disk;
121        return this;
122    }
123
124    @Nonnull
125    public QEmuDriveOption withFile(@Nonnull File file) {
126        return withDisk(new FileDisk(file));
127    }
128
129    @Nonnull
130    public QEmuDriveOption withId(@Nonnull String id) {
131        this.id = id;
132        return this;
133    }
134
135    @Nonnull
136    public QEmuDriveOption withIndex(@Nonnegative int index) {
137        this.index = index;
138        return this;
139    }
140
141    @Nonnull
142    public QEmuDriveOption withIndex(@Nonnegative QEmuIdAllocator allocator) {
143        return withIndex(allocator.newDriveIndex());
144    }
145
146    @Nonnull
147    public QEmuDriveOption withInterface(@Nonnull Interface iface) {
148        this.iface = iface;
149        return this;
150    }
151
152    @Nonnull
153    public QEmuDriveOption withFormat(@Nonnull QEmuImageFormat format) {
154        this.format = format;
155        return this;
156    }
157
158    @Nonnull
159    public QEmuDriveOption withMedia(@Nonnull Media media) {
160        this.media = media;
161        return this;
162    }
163
164    @Nonnull
165    public QEmuDriveOption withCache(@Nonnull Cache cache) {
166        this.cache = cache;
167        return this;
168    }
169
170    @Nonnull
171    public QEmuDriveOption withAio(@Nonnull Aio aio) {
172        this.aio = aio;
173        return this;
174    }
175
176    @Nonnull
177    public QEmuDriveOption withDiscard(@Nonnull Discard discard) {
178        this.discard = discard;
179        return this;
180    }
181
182    @Nonnull
183    public QEmuDriveOption withWriteErrorAction(@Nonnull ErrorAction action) {
184        this.werror = action;
185        return this;
186    }
187
188    @Nonnull
189    public QEmuDriveOption withReadErrorAction(@Nonnull ErrorAction action) {
190        this.rerror = action;
191        return this;
192    }
193
194    @Nonnull
195    public QEmuDriveOption withReadOnly(boolean readonly) {
196        this.readonly = readonly;
197        return this;
198    }
199
200    @Nonnull
201    public QEmuDriveOption withCopyOnRead(@Nonnull CopyOnRead copyOnRead) {
202        this.copyOnRead = copyOnRead;
203        return this;
204    }
205
206    @Override
207    public void appendTo(List<? super String> line) {
208        StringBuilder buf = new StringBuilder();
209        appendTo(buf, "file", disk);
210        appendTo(buf, "index", index);
211        appendTo(buf, "id", id);
212        appendTo(buf, "if", iface);
213        appendTo(buf, "format", format);
214        appendTo(buf, "media", media);
215        appendTo(buf, "cache", cache);
216        appendTo(buf, "aio", aio);
217        appendTo(buf, "discard", discard);
218        appendTo(buf, "werror", werror);
219        appendTo(buf, "rerror", rerror);
220        if (readonly)
221            buf.append("readonly");
222        appendTo(buf, "copy-on-read", copyOnRead);
223
224        add(line, "-drive", buf.toString());
225    }
226}