Skip to content

Instantly share code, notes, and snippets.

@caHarkness
Last active Mar 25, 2022
Embed
What would you like to do?
An /etc/init.d script for automatically mounting Windows shares in Debian Linux
#! /bin/sh
### BEGIN INIT INFO
# Provides: cifs-share-x
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Automatically mount Windows shares in Debian Linux
### END INIT INFO
#
# To install this, run as root:
# export SERVICE_NAME="cifs-share-x"; curl "https://gist.githubusercontent.com/caHarkness/22bb218d016663444b1a64023937530d/raw/cifs-share" > /etc/init.d/$SERVICE_NAME; chmod +x /etc/init.d/$SERVICE_NAME; sed -n "s/cifs-share-x/$SERVICE_NAME/gp" /etc/init.d/$SERVICE_NAME; ln -s /etc/init.d/$SERVICE_NAME /etc/rc2.d/S99$SERVICE_NAME; nano /etc/init.d/$SERVICE_NAME; sleep 1; systemctl daemon-reload
#
WINDOWS_USER="Windows User"
WINDOWS_PASSWORD="password"
WINDOWS_SHARE="//DESKTOP-NAME/Share"
LINUX_USER="root"
LINUX_GROUP="root"
LINUX_DIR="/mnt/Share"
mount_share () {
mkdir -p "$LINUX_DIR"
mount -t cifs -o username="$WINDOWS_USER",password="$WINDOWS_PASSWORD",uid=$LINUX_USER,gid=$LINUX_GROUP "$WINDOWS_SHARE" "$LINUX_DIR"
}
unmount_share () {
umount "$LINUX_DIR"
}
list_share () {
ls -lah "$LINUX_DIR"
}
edit_service () {
SERVICE_NAME=$(basename $0)
nano /etc/init.d/$SERVICE_NAME
sleep 1
systemctl daemon-reload
}
uninstall_service () {
SERVICE_NAME=$(basename $0)
rm /etc/init.d/$SERVICE_NAME
rm /etc/rc2.d/S99$SERVICE_NAME
echo "Service uninstalled."
}
PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/lsb/init-functions
case "$1" in
start)
mount_share
;;
stop)
unmount_share
;;
restart)
unmount_share
mount_share
;;
list)
list_share
;;
edit)
edit_service
;;
uninstall)
uninstall_service
;;
*)
echo "Usage: $0 {start|stop|restart|list|edit|uninstall}"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment