Skip to content

Instantly share code, notes, and snippets.

@RecNes
Last active June 20, 2018 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RecNes/c3e9263d102b38b4e590141ab3682303 to your computer and use it in GitHub Desktop.
Save RecNes/c3e9263d102b38b4e590141ab3682303 to your computer and use it in GitHub Desktop.
Script for automatic file system check and mount usb disk for raspberry pi raspbian
"""
Please READ carefuly first and use at your OWN RISK!
This script is repeatedly try to mount EXT4 partition to given point.
Any other FS types are igored while writing this script. Works ONLY EXT4!
Before mounting, checks and corrects the file system errors. THIS MAY CAUSE LOSS OF DATA!
Append cronjob such as:
15 * * * * /usr/bin/python /root/repeatedly_mount.py
Developen on: Python 2.7.13
"""
__author__ = 'Sencer Hamarat'
__license__ = "Creative Commons Attribution-ShareAlike 3.0 Unported License"
__version__ = "1.1"
__maintainer__ = "Sencer Hamarat"
__status__ = "Production"
import os
import sys
from time import sleep
partition = '/dev/sda1'
mount_point = '/media/Backup'
lock_file = '/tmp/repeatedly_mount.lock'
if os.path.isfile(lock_file):
print('Already running...')
sys.exit()
else:
os.system('touch %s' % lock_file)
try:
content = ''
with open('/etc/mtab', 'r') as f:
content = f.read()
if partition not in content:
# Make sure the partition is clean before trying to mount.
os.system('fsck.ext4 -y %s' % partition)
count = 0
while mount_point not in content:
if count > 0:
print('Trying again...')
else:
print('Not mounted, trying to mount...')
os.system('systemctl daemon-reload && systemctl restart local-fs.target')
os.system('mount -a')
count += 1
sleep(1)
with open('/etc/mtab', 'r') as f:
content = f.read()
print('SUCCESSFULY MOUNTED!')
except KeyboardInterrupt:
pass
finally:
os.unlink(lock_file)
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment