Sm.java revision 82bfca4609244f2b8169bd779765791d6638b448
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.commands.sm;
18
19import android.os.RemoteException;
20import android.os.ServiceManager;
21import android.os.SystemProperties;
22import android.os.storage.DiskInfo;
23import android.os.storage.IMountService;
24import android.os.storage.StorageManager;
25import android.os.storage.VolumeInfo;
26import android.util.Log;
27
28public final class Sm {
29    private static final String TAG = "Sm";
30
31    IMountService mSm;
32
33    private String[] mArgs;
34    private int mNextArg;
35    private String mCurArgData;
36
37    public static void main(String[] args) {
38        boolean success = false;
39        try {
40            new Sm().run(args);
41            success = true;
42        } catch (Exception e) {
43            if (e instanceof IllegalArgumentException) {
44                showUsage();
45                System.exit(1);
46            }
47            Log.e(TAG, "Error", e);
48            System.err.println("Error: " + e);
49        }
50        System.exit(success ? 0 : 1);
51    }
52
53    public void run(String[] args) throws Exception {
54        if (args.length < 1) {
55            throw new IllegalArgumentException();
56        }
57
58        mSm = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
59        if (mSm == null) {
60            throw new RemoteException("Failed to find running mount service");
61        }
62
63        mArgs = args;
64        String op = args[0];
65        mNextArg = 1;
66
67        if ("list-disks".equals(op)) {
68            runListDisks();
69        } else if ("list-volumes".equals(op)) {
70            runListVolumes();
71        } else if ("has-adoptable".equals(op)) {
72            runHasAdoptable();
73        } else if ("get-primary-storage-uuid".equals(op)) {
74            runGetPrimaryStorageUuid();
75        } else if ("set-force-adoptable".equals(op)) {
76            runSetForceAdoptable();
77        } else if ("partition".equals(op)) {
78            runPartition();
79        } else if ("mount".equals(op)) {
80            runMount();
81        } else if ("unmount".equals(op)) {
82            runUnmount();
83        } else if ("format".equals(op)) {
84            runFormat();
85        } else if ("benchmark".equals(op)) {
86            runBenchmark();
87        } else if ("forget".equals(op)) {
88            runForget();
89        } else {
90            throw new IllegalArgumentException();
91        }
92    }
93
94    public void runListDisks() throws RemoteException {
95        final boolean onlyAdoptable = "adoptable".equals(nextArg());
96        final DiskInfo[] disks = mSm.getDisks();
97        for (DiskInfo disk : disks) {
98            if (!onlyAdoptable || disk.isAdoptable()) {
99                System.out.println(disk.getId());
100            }
101        }
102    }
103
104    public void runListVolumes() throws RemoteException {
105        final String filter = nextArg();
106        final int filterType;
107        if ("public".equals(filter)) {
108            filterType = VolumeInfo.TYPE_PUBLIC;
109        } else if ("private".equals(filter)) {
110            filterType = VolumeInfo.TYPE_PRIVATE;
111        } else if ("emulated".equals(filter)) {
112            filterType = VolumeInfo.TYPE_EMULATED;
113        } else {
114            filterType = -1;
115        }
116
117        final VolumeInfo[] vols = mSm.getVolumes(0);
118        for (VolumeInfo vol : vols) {
119            if (filterType == -1 || filterType == vol.getType()) {
120                final String envState = VolumeInfo.getEnvironmentForState(vol.getState());
121                System.out.println(vol.getId() + " " + envState + " " + vol.getFsUuid());
122            }
123        }
124    }
125
126    public void runHasAdoptable() {
127        System.out.println(SystemProperties.getBoolean(StorageManager.PROP_HAS_ADOPTABLE, false));
128    }
129
130    public void runGetPrimaryStorageUuid() throws RemoteException {
131        System.out.println(mSm.getPrimaryStorageUuid());
132    }
133
134    public void runSetForceAdoptable() throws RemoteException {
135        final boolean forceAdoptable = Boolean.parseBoolean(nextArg());
136        mSm.setDebugFlags(forceAdoptable ? StorageManager.DEBUG_FORCE_ADOPTABLE : 0,
137                StorageManager.DEBUG_FORCE_ADOPTABLE);
138    }
139
140    public void runPartition() throws RemoteException {
141        final String diskId = nextArg();
142        final String type = nextArg();
143        if ("public".equals(type)) {
144            mSm.partitionPublic(diskId);
145        } else if ("private".equals(type)) {
146            mSm.partitionPrivate(diskId);
147        } else if ("mixed".equals(type)) {
148            final int ratio = Integer.parseInt(nextArg());
149            mSm.partitionMixed(diskId, ratio);
150        } else {
151            throw new IllegalArgumentException("Unsupported partition type " + type);
152        }
153    }
154
155    public void runMount() throws RemoteException {
156        final String volId = nextArg();
157        mSm.mount(volId);
158    }
159
160    public void runUnmount() throws RemoteException {
161        final String volId = nextArg();
162        mSm.unmount(volId);
163    }
164
165    public void runFormat() throws RemoteException {
166        final String volId = nextArg();
167        mSm.format(volId);
168    }
169
170    public void runBenchmark() throws RemoteException {
171        final String volId = nextArg();
172        mSm.benchmark(volId);
173    }
174
175    public void runForget() throws RemoteException{
176        final String fsUuid = nextArg();
177        if ("all".equals(fsUuid)) {
178            mSm.forgetAllVolumes();
179        } else {
180            mSm.forgetVolume(fsUuid);
181        }
182    }
183
184    private String nextArg() {
185        if (mNextArg >= mArgs.length) {
186            return null;
187        }
188        String arg = mArgs[mNextArg];
189        mNextArg++;
190        return arg;
191    }
192
193    private static int showUsage() {
194        System.err.println("usage: sm list-disks [adoptable]");
195        System.err.println("       sm list-volumes [public|private|emulated|all]");
196        System.err.println("       sm has-adoptable");
197        System.err.println("       sm get-primary-storage-uuid");
198        System.err.println("       sm set-force-adoptable [true|false]");
199        System.err.println("");
200        System.err.println("       sm partition DISK [public|private|mixed] [ratio]");
201        System.err.println("       sm mount VOLUME");
202        System.err.println("       sm unmount VOLUME");
203        System.err.println("       sm format VOLUME");
204        System.err.println("       sm benchmark VOLUME");
205        System.err.println("");
206        System.err.println("       sm forget [UUID|all]");
207        System.err.println("");
208        return 1;
209    }
210}
211