Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/scripts/salvus_nbd_format.py
Views: 687
#!/usr/bin/python1###############################################################################2#3# CoCalc: Collaborative Calculation4#5# Copyright (C) 2016, Sagemath Inc.6#7# This program is free software: you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation, either version 3 of the License, or10# (at your option) any later version.11#12# This program is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along with this program. If not, see <http://www.gnu.org/licenses/>.19#20###############################################################################21"""22Put this in visudo and make damned sure only root can edit this script. This only should be in the VM hosts not the actual VM's.2324salvus ALL=(ALL) NOPASSWD: /usr/local/bin/salvus_nbd_format.py *25"""2627import os, random, sys28from subprocess import Popen, PIPE2930if len(sys.argv) != 3:31print "Usage: %s xfs|ext4|btrfs file.img" % sys.argv[0]32sys.exit(1)3334format = sys.argv[1]35if format not in ['xfs', 'ext4', 'btrfs']:36print "format must be xfs or ext4 or btrfs"37sys.exit(1)3839image = os.path.abspath(sys.argv[2])4041if not os.path.exists(image):42print "image file %s doesn't exist" % image43sys.exit(1)444546def cmd(s):47print s48if os.system(s):49raise RuntimeError('failed: "%s"' % s)505152def cmd2(s):53print s54if isinstance(s, str):55out = Popen(s, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)56else:57out = Popen(s, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)58e = out.wait()59x = out.stdout.read() + out.stderr.read()60return x, e616263max_part = 64646566def nbd(n):67cmd("modprobe nbd max_part=%s" % max_part)68dev = '/dev/nbd%s' % n69if cmd2(['qemu-nbd', '-c', dev, image])[1]:70raise RuntimeError(out.stdout.read() + out.stderr.read())71else:72return dev737475def fdisk(dev):76x, e = cmd2('echo "n\np\n1\n\n\n\nw" | fdisk %s' % dev)77if "is already defined" in x.lower():78raise ValueError("already partitioned-- refusing to format.")79if e:80raise RuntimeError(x)818283def mkfs(dev):84# WARNING: this takes a long time, especially with xfs85if format == 'xfs':86cmd("mkfs.%s -f %sp1" % (format, dev))87else:88cmd("mkfs.%s %sp1" % (format, dev))899091def nbd_disconnect(dev):92cmd("qemu-nbd -d %s" % dev)939495dev = None96try:97for i in range(max_part):98# we try until success, since nbd is flakie.99try:100dev = nbd(i)101fdisk(dev)102mkfs(dev)103break104except RuntimeError:105pass106finally:107if dev is not None:108nbd_disconnect(dev)109110111