|
|
17b0f1 |
#!/usr/bin/python
|
|
|
17b0f1 |
# -*- Mode: Python; python-indent: 8; indent-tabs-mode: t -*-
|
|
|
17b0f1 |
|
|
|
17b0f1 |
import sys, os, argparse, errno
|
|
|
17b0f1 |
|
|
|
17b0f1 |
def find_service(service, runlevel):
|
|
|
17b0f1 |
priority = -1
|
|
|
17b0f1 |
|
|
|
17b0f1 |
for l in os.listdir("/etc/rc%i.d" % runlevel):
|
|
|
17b0f1 |
if len(l) < 4:
|
|
|
17b0f1 |
continue
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if l[0] != 'S' or l[3:] != service:
|
|
|
17b0f1 |
continue
|
|
|
17b0f1 |
|
|
|
17b0f1 |
p = int(l[1:3])
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if p >= 0 and p <= 99 and p >= priority:
|
|
|
17b0f1 |
priority = p;
|
|
|
17b0f1 |
|
|
|
17b0f1 |
return priority
|
|
|
17b0f1 |
|
|
|
17b0f1 |
def lookup_database(services):
|
|
|
17b0f1 |
try:
|
|
|
17b0f1 |
database = open("/var/lib/systemd/sysv-convert/database", "r")
|
|
|
17b0f1 |
except IOError, e:
|
|
|
17b0f1 |
if e.errno != errno.ENOENT:
|
|
|
17b0f1 |
raise e
|
|
|
17b0f1 |
|
|
|
17b0f1 |
return {}
|
|
|
17b0f1 |
|
|
|
17b0f1 |
found = {}
|
|
|
17b0f1 |
k = 0
|
|
|
17b0f1 |
|
|
|
17b0f1 |
for line in database:
|
|
|
17b0f1 |
service, r, p = line.strip().split("\t", 3)
|
|
|
17b0f1 |
k += 1
|
|
|
17b0f1 |
|
|
|
17b0f1 |
try:
|
|
|
17b0f1 |
runlevel = int(r)
|
|
|
17b0f1 |
priority = int(p)
|
|
|
17b0f1 |
except ValueError, e:
|
|
|
17b0f1 |
sys.stderr.write("Failed to parse database line %i. Ignoring." % k)
|
|
|
17b0f1 |
continue
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if runlevel not in (2, 3, 4, 5):
|
|
|
17b0f1 |
sys.stderr.write("Runlevel out of bounds in database line %i. Ignoring." % k)
|
|
|
17b0f1 |
continue
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if priority < 0 or priority > 99:
|
|
|
17b0f1 |
sys.stderr.write("Priority out of bounds in database line %i. Ignoring." % k)
|
|
|
17b0f1 |
continue
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if service not in services:
|
|
|
17b0f1 |
continue
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if service not in found:
|
|
|
17b0f1 |
found[service] = {}
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if runlevel not in found[service] or found[service][runlevel] < priority:
|
|
|
17b0f1 |
found[service][runlevel] = priority
|
|
|
17b0f1 |
|
|
|
17b0f1 |
return found
|
|
|
17b0f1 |
|
|
|
17b0f1 |
def mkdir_p(path):
|
|
|
17b0f1 |
try:
|
|
|
17b0f1 |
os.makedirs(path, 0755)
|
|
|
17b0f1 |
except OSError, e:
|
|
|
17b0f1 |
if e.errno != errno.EEXIST:
|
|
|
17b0f1 |
raise e
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if os.geteuid() != 0:
|
|
|
17b0f1 |
sys.stderr.write("Need to be root.\n")
|
|
|
17b0f1 |
sys.exit(1)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
parser = argparse.ArgumentParser(description='Save and Restore SysV Service Runlevel Information')
|
|
|
17b0f1 |
|
|
|
17b0f1 |
parser.add_argument('services', metavar='SERVICE', type=str, nargs='+',
|
|
|
17b0f1 |
help='Service names')
|
|
|
17b0f1 |
|
|
|
17b0f1 |
parser.add_argument('--save', dest='save', action='store_const',
|
|
|
17b0f1 |
const=True, default=False,
|
|
|
17b0f1 |
help='Save SysV runlevel information for one or more services')
|
|
|
17b0f1 |
|
|
|
17b0f1 |
parser.add_argument('--show', dest='show', action='store_const',
|
|
|
17b0f1 |
const=True, default=False,
|
|
|
17b0f1 |
help='Show saved SysV runlevel information for one or more services')
|
|
|
17b0f1 |
|
|
|
17b0f1 |
parser.add_argument('--apply', dest='apply', action='store_const',
|
|
|
17b0f1 |
const=True, default=False,
|
|
|
17b0f1 |
help='Apply saved SysV runlevel information for one or more services to systemd counterparts')
|
|
|
17b0f1 |
|
|
|
17b0f1 |
a = parser.parse_args()
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if a.save:
|
|
|
17b0f1 |
for service in a.services:
|
|
|
17b0f1 |
if not os.access("/etc/rc.d/init.d/%s" % service, os.F_OK):
|
|
|
17b0f1 |
sys.stderr.write("SysV service %s does not exist.\n" % service)
|
|
|
17b0f1 |
sys.exit(1)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
mkdir_p("/var/lib/systemd/sysv-convert")
|
|
|
17b0f1 |
database = open("/var/lib/systemd/sysv-convert/database", "a")
|
|
|
17b0f1 |
|
|
|
17b0f1 |
for runlevel in (2, 3, 4, 5):
|
|
|
17b0f1 |
priority = find_service(service, runlevel)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if priority >= 0:
|
|
|
17b0f1 |
database.write("%s\t%s\t%s\n" % (service, runlevel, priority))
|
|
|
17b0f1 |
|
|
|
17b0f1 |
elif a.show:
|
|
|
17b0f1 |
found = lookup_database(a.services)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if len(found) <= 0:
|
|
|
17b0f1 |
sys.stderr.write("No information about passed services found.\n")
|
|
|
17b0f1 |
sys.exit(1)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
for service, data in found.iteritems():
|
|
|
17b0f1 |
for runlevel, priority in data.iteritems():
|
|
|
17b0f1 |
sys.stdout.write("SysV service %s enabled in runlevel %s at priority %s\n" % (service, runlevel, priority))
|
|
|
17b0f1 |
|
|
|
17b0f1 |
elif a.apply:
|
|
|
17b0f1 |
for service in a.services:
|
|
|
17b0f1 |
if not os.access("/lib/systemd/system/%s.service" % service, os.F_OK):
|
|
|
17b0f1 |
sys.stderr.write("systemd service %s.service does not exist.\n" % service)
|
|
|
17b0f1 |
sys.exit(1)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
found = lookup_database(a.services)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if len(found) <= 0:
|
|
|
17b0f1 |
sys.stderr.write("No information about passed services found.\n")
|
|
|
17b0f1 |
sys.exit(1)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
for service, data in found.iteritems():
|
|
|
17b0f1 |
for runlevel in data.iterkeys():
|
|
|
17b0f1 |
|
|
|
17b0f1 |
sys.stderr.write("ln -sf /lib/systemd/system/%s.service /etc/systemd/system/runlevel%i.target.wants/%s.service\n" % (service, runlevel, service))
|
|
|
17b0f1 |
|
|
|
17b0f1 |
mkdir_p("/etc/systemd/system/runlevel%i.target.wants" % runlevel)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
try:
|
|
|
17b0f1 |
os.symlink("/lib/systemd/system/%s.service" % service,
|
|
|
17b0f1 |
"/etc/systemd/system/runlevel%i.target.wants/%s.service" % (runlevel, service))
|
|
|
17b0f1 |
except OSError, e:
|
|
|
17b0f1 |
if e.errno != errno.EEXIST:
|
|
|
17b0f1 |
raise e
|
|
|
17b0f1 |
|
|
|
17b0f1 |
else:
|
|
|
17b0f1 |
parser.print_help()
|