Sm.java revision 0d838a0fad500a3c446df501d8aa7656c2c3a7a2
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            }
46            Log.e(TAG, "Error", e);
47            System.err.println("Error: " + e);
48        }
49        System.exit(success ? 0 : 1);
50    }
51
52    public void run(String[] args) throws Exception {
53        if (args.length < 1) {
54            throw new IllegalArgumentException();
55        }
56
57        mSm = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
58        if (mSm == null) {
59            throw new RemoteException("Failed to find running mount service");
60        }
61
62        mArgs = args;
63        String op = args[0];
64        mNextArg = 1;
65
66        if ("list-disks".equals(op)) {
67            runListDisks();
68        } else if ("list-volumes".equals(op)) {
69            runListVolumes();
70        } else if ("has-adoptable".equals(op)) {
71            runHasAdoptable();
72        } else if ("get-primary-storage-uuid".equals(op)) {
73            runGetPrimaryStorageUuid();
74        } else if ("partition".equals(op)) {
75            runPartition();
76        } else if ("mount".equals(op)) {
77            runMount();
78        } else if ("unmount".equals(op)) {
79            runUnmount();
80        } else if ("format".equals(op)) {
81            runFormat();
82        } else if ("forget".equals(op)) {
83            runForget();
84        } else {
85            throw new IllegalArgumentException();
86        }
87    }
88
89    public void runListDisks() throws RemoteException {
90        final DiskInfo[] disks = mSm.getDisks();
91        for (DiskInfo disk : disks) {
92            System.out.println(disk.getId());
93        }
94    }
95
96    public void runListVolumes() throws RemoteException {
97        final String filter = nextArg();
98        final int filterType;
99        if ("public".equals(filter)) {
100            filterType = VolumeInfo.TYPE_PUBLIC;
101        } else if ("private".equals(filter)) {
102            filterType = VolumeInfo.TYPE_PRIVATE;
103        } else if ("emulated".equals(filter)) {
104            filterType = VolumeInfo.TYPE_EMULATED;
105        } else {
106            filterType = -1;
107        }
108
109        final VolumeInfo[] vols = mSm.getVolumes(0);
110        for (VolumeInfo vol : vols) {
111            if (filterType == -1 || filterType == vol.getType()) {
112                final String envState = VolumeInfo.getEnvironmentForState(vol.getState());
113                System.out.println(vol.getId() + " " + envState + " " + vol.getFsUuid());
114            }
115        }
116    }
117
118    public void runHasAdoptable() {
119        System.out.println(SystemProperties.getBoolean(StorageManager.PROP_HAS_ADOPTABLE, false)
120                || SystemProperties.getBoolean(StorageManager.PROP_FORCE_ADOPTABLE, false));
121    }
122
123    public void runGetPrimaryStorageUuid() throws RemoteException {
124        System.out.println(mSm.getPrimaryStorageUuid());
125    }
126
127    public void runPartition() throws RemoteException {
128        final String diskId = nextArg();
129        final String type = nextArg();
130        if ("public".equals(type)) {
131            mSm.partitionPublic(diskId);
132        } else if ("private".equals(type)) {
133            mSm.partitionPrivate(diskId);
134        } else if ("mixed".equals(type)) {
135            final int ratio = Integer.parseInt(nextArg());
136            mSm.partitionMixed(diskId, ratio);
137        } else {
138            throw new IllegalArgumentException("Unsupported partition type " + type);
139        }
140    }
141
142    public void runMount() throws RemoteException {
143        final String volId = nextArg();
144        mSm.mount(volId);
145    }
146
147    public void runUnmount() throws RemoteException {
148        final String volId = nextArg();
149        mSm.unmount(volId);
150    }
151
152    public void runFormat() throws RemoteException {
153        final String volId = nextArg();
154        mSm.format(volId);
155    }
156
157    public void runForget() throws RemoteException{
158        final String fsUuid = nextArg();
159        if ("all".equals(fsUuid)) {
160            mSm.forgetAllVolumes();
161        } else {
162            mSm.forgetVolume(fsUuid);
163        }
164    }
165
166    private String nextArg() {
167        if (mNextArg >= mArgs.length) {
168            return null;
169        }
170        String arg = mArgs[mNextArg];
171        mNextArg++;
172        return arg;
173    }
174
175    private static int showUsage() {
176        System.err.println("usage: sm list-disks");
177        System.err.println("       sm list-volumes [public|private|emulated|all]");
178        System.err.println("       sm has-adoptable");
179        System.err.println("       sm get-primary-storage-uuid");
180        System.err.println("");
181        System.err.println("       sm partition DISK [public|private|mixed] [ratio]");
182        System.err.println("       sm mount VOLUME");
183        System.err.println("       sm unmount VOLUME");
184        System.err.println("       sm format VOLUME");
185        System.err.println("");
186        System.err.println("       sm forget [UUID|all]");
187        System.err.println("");
188        return 1;
189    }
190}
191