all the python scripts are for python2, and fedora31 requires to be specific
[vsys-scripts.git] / root-context / exec / pvtfs
1 #!/usr/bin/python2
2 # Lets a user loopback-mount a private fs of a given type.
3 # To use, configure the following vsys-attributes in addition to setting the vsys tag for the slice
4 # - vsys_privatefs_list -> comma-separated list of desired filesystems, e.g. xfs,btrfs
5 # - vsys_privatefs_size -> desired size in megabytes
6
7 import sys
8 import os
9 import string
10
11 vsys_config_dir = "/etc/planetlab/vsys-attributes"
12 slicename=sys.argv[1]
13 sliceroot='/vservers/%s'%slicename
14
15 private_fs_config=os.path.join(vsys_config_dir,slicename,"vsys_privatefs_list")
16 private_fs_size_config=os.path.join(vsys_config_dir,slicename,"vsys_privatefs_size")
17
18 private_fs_str = open(private_fs_config).read().rstrip()
19
20 nonamespace_path = '/etc/vservers/%s/nonamespace'%slicename
21 if (not os.path.isfile(nonamespace_path)):
22         # Touch
23         open(nonamespace_path,'w').close()
24
25         os.system('vserver %s stop'%slicename)
26         os.system('vserver %s start'%slicename)
27         print 'Reconfiguring slice for private mounts'
28         exit(1)
29
30 fs_lst = private_fs_str.split(',')
31 try:
32         private_fs_size_str = open(private_fs_size_config).read().rstrip()
33         fs_size = int(private_fs_size_str)
34 except:
35         fs_size = 100
36
37 num_fs = len(fs_lst)
38 if num_fs==0:
39     print >>sys.stderr, "Please configure your private filesystems at PLC."
40     sys.exit(1)
41
42 for fs in fs_lst:
43         # XXX There's a race condition here. You could 
44         # intervene between the creation of the image
45         # and the mount operation to replace the empty image
46         # with a rogue volume.
47
48         fs_file = '%s/pvtfs-%s'%(sliceroot,fs)
49         os.system('umount %s 2>&1>/dev/null'%fs_file)
50
51         sanity_check_cmd = 'cat /proc/mounts | grep %s | wc -l'%fs_file
52         sanity_check = os.popen(sanity_check_cmd).read().rstrip()
53         if int(sanity_check)!=0:
54                 print 'Private fs could not be remounted. Please contact support.'
55                 exit(1)
56         os.system('mkdir -p %s'%fs_file)
57         
58         # This is the critical step
59         if (os.system('dd if=/dev/zero of=%s.img bs=1000000 count=%d'%(fs_file,fs_size))):
60                 print 'Could not recycle private fs. Please contact support.'
61                 exit(1)
62
63         os.system('mkfs.%s %s.img'%(fs,fs_file))
64         os.system('mount -o loop -t %s %s.img %s'%(fs, fs_file, fs_file))