AutoMount partitions using Python in GNU/Linux
I am using various distro of GNU/Linux and I use Ubuntu 9.10,11.04,Fedora 14 and Windows 7(Though I don’t use ,Still I have a partition).
It was boring for me to mount every time I login ,so being Kickass Python fan(decent Python programmer),I tried to write a py script and I succeeded .
Here is the following steps you should perform.
1.Find all Partitions with type.
2.Write a python script which will mount all partitions.
3.Move the python file to appropriate place and start during system start up.
- Find all partitions with type.
You have to find all the partitions in the system ,most people will think df -T will yield ,yes you are wrong,df -T will yield the partition type of mounted disk only.
You should use sudo fdisk -l
kracekumar@python-lover:~$ sudo fdisk -l Disk /dev/sda: 320.1 GB, 320072933376 bytes 255 heads, 63 sectors/track, 38913 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xf897e57b Device Boot Start End Blocks Id System /dev/sda1 1 192 1536000 27 Unknown Partition 1 does not end on cylinder boundary. /dev/sda2 192 10786 85097782 7 HPFS/NTFS /dev/sda3 10787 37572 215153680+ f W95 Ext'd (LBA) /dev/sda4 37572 38914 10778624 17 Hidden HPFS/NTFS /dev/sda5 18954 22993 32442349 83 Linux /dev/sda6 27878 37572 77869056 7 HPFS/NTFS /dev/sda7 10787 15916 41199605 83 Linux /dev/sda8 18615 18953 2722986 82 Linux swap / Solaris /dev/sda9 * 15916 18497 20731904 83 Linux /dev/sda10 18497 18614 945152 82 Linux swap / Solaris /dev/sda11 22993 27671 37580800 83 Linux /dev/sda12 27671 27878 1655808 82 Linux swap / Solaris
Linux =>Means ext4 partition and some times it may be vfat too,so best way is to mount all the partitions and run df -T to confirm.
kracekumar@python-lover:~$ df -T Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/sda11 ext4 36990536 15201872 19909624 44% / none devtmpfs 1474632 740 1473892 1% /dev none tmpfs 1481240 3844 1477396 1% /dev/shm none tmpfs 1481240 240 1481000 1% /var/run none tmpfs 1481240 0 1481240 0% /var/lock /dev/sda7 ext4 40551772 36086304 2405492 94% /mnt/ubuntu9.10 /dev/sda9 ext4 20406140 6774300 12595248 35% /mnt/fedora14 /dev/sda5 vfat 32426480 29893680 2532800 93% /mnt/movies /dev/sda2 fuseblk 85097776 69094072 16003704 82% /mnt/system /dev/sda6 fuseblk 77869052 64031512 13837540 83% /mnt/others
As we expected sda5 was vfat ,now note down what are the partitions and their type.
Now partitions to be mounted are
#My partions #/dev/sda6 ->/mnt/others ->ntfs-3g #/dev/sda2 ->/mnt/system ->ntfs -3g #/dev/sda7 ->/mnt/ubuntu9.10 ->ext3 #/dev/sda3 ->/mnt/movies ->ext3 #/dev/sda9 ->/mnt/fedora14 ->ext3
Now we are done with the partitions ,in above you can find /mnt/x =>these are the places where partitions to be mounted,so we are suppose to create the directories using sudo mkdir /mnt/x.
- Write Python script
Command to mount :sudo mount -t type partition destination
Ex:
sudo mount -t ext4 /dev/sda9 /mnt/somewhere
#! /usr/bin/env python
import subprocess #to execute Linux commands,still you can use os.system but subprocess is preferred
source=('/dev/sda6','/dev/sda2','/dev/sda7','/dev/sda5','/dev/sda9')#partitions to be mounted
dest=('/mnt/others','/mnt/system','/mnt/ubuntu9.10','/mnt/movies','/mnt/fedora14')#destination to mount
type_=('ntfs-3g','ntfs-3g','ext4','vfat','ext4')#type of partition
for (x,y,z) in zip(source,dest,type_):
subprocess.Popen(["mount","-t",z,x,y])#subprocess.Popen([command,arguments])
print x,"is mounted in ",y#confirmation message but when system is started this won't be printed any where.
Now save the file in the name mount_all.py and chmod +x mount_all.py.
Now sudo mv mount_all.py /usr/local/bin/
- Make sure python file is executed during system start process .
Open sudo vim /etc/rc.local.py ,and add python /usr/local/bin/mount_all.py before exit 0 which is at the end of the file.
!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. python /usr/local/bin/mount_all.py exit 0
Above is /etc/rc.local file.
Save the file and restart the system and check.
Download mount_all.py .
Note:
Without using Python script you also can auto mount partitions ,but that method is different.