Difference between revisions of "OPS435 Python Assignment 2 C"
(→Instruction) |
(→Overview) |
||
(7 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
[[Category:OPS435-Python]][[Category:rchan]] | [[Category:OPS435-Python]][[Category:rchan]] | ||
= Overview = | = Overview = | ||
− | In order to use a physical storage device (e.g. Hard Disk, USB Flash | + | In order to use a physical storage device (e.g. Hard Disk, USB Flash memory, SSD, etc.), the first step is to create partition(s) on it. There many different ways to partition a physical storage device. The two most common partition schemes used on Intel based micro-computer systems are MBR and GPT. Detail about the MBR partition scheme can be found [https://en.wikipedia.org/wiki/Master_boot_record here], and GPT partition scheme can be found at [https://en.wikipedia.org/wiki/GUID_Partition_Table]. |
− | On most Linux systems, fdisk or parted can be used to create, view, and manage MBR and GPT | + | On most Linux systems, fdisk or parted can be used to create, view, and manage MBR and GPT partitions. |
− | |||
− | |||
+ | The task for you in this assignment is to write a python module named "partinfo.py", which contains at least three functions: part_scheme(), mbr_part(), gpt_part(), which can be used to retrieve partition information from a specified physical storage device. | ||
= Instruction = | = Instruction = | ||
== Module name, function names, function parameter(s) and return values == | == Module name, function names, function parameter(s) and return values == | ||
Name your python module as "partinfo.py". The module should contains at least the following 3 functions: | Name your python module as "partinfo.py". The module should contains at least the following 3 functions: | ||
− | * part_scheme() - which when called with a valid physical storage device name, will return the partition scheme used on the device. The | + | * <code>part_scheme()</code> - which when called with a valid physical storage device name, will return the partition scheme used on the device. The returned value should only be |
** None - no valid partition scheme found on the device | ** None - no valid partition scheme found on the device | ||
** MBR - MBR partitions is/are found on the device | ** MBR - MBR partitions is/are found on the device | ||
** GPT - GUID partitions is/are found on the device | ** GPT - GUID partitions is/are found on the device | ||
− | * mbr_part() - which when called with a valid physical storage device name, will return all the partitions (primary, extended, and logical) found on the device. Each partition information | + | * <code>mbr_part()</code> - which when called with a valid physical storage device name, will return all the partitions (primary, extended, and logical) found on the device. Each partition information returned must contain the following fields: |
** partition number | ** partition number | ||
** partition flag (bootable or not) | ** partition flag (bootable or not) | ||
Line 21: | Line 20: | ||
** start sector number (in LBA mode) | ** start sector number (in LBA mode) | ||
** size of partition in sectors | ** size of partition in sectors | ||
− | * gpt_part() - which when called with a valid physical storage device name, will return all the partitions found on the device. Each partition information | + | * <code>gpt_part()</code> - which when called with a valid physical storage device name, will return all the partitions found on the device. Each partition information returned must contain the following fields: |
** Partition type GUID | ** Partition type GUID | ||
** Unique partition GUID | ** Unique partition GUID | ||
Line 30: | Line 29: | ||
== Module and function documentation == | == Module and function documentation == | ||
Use the python docstring to provide embedded documentation for the module "partinfo.py" and the functions in it. | Use the python docstring to provide embedded documentation for the module "partinfo.py" and the functions in it. | ||
+ | |||
+ | == Sample Python codes == | ||
+ | The following sample python code reads and displays primary partitions on a given physical storage device with MBR partition scheme. Please note that this python script does not check for extended partition and does not try to extract the information for logical partitions even they exist. | ||
+ | <pre> | ||
+ | #! /usr/bin/python | ||
+ | # read and display primary partition info | ||
+ | # on a physical disk or disk image | ||
+ | # | ||
+ | import struct | ||
+ | import sys | ||
+ | |||
+ | # ask the user for disk file name | ||
+ | # if not provided on the command line | ||
+ | no_arg = len(sys.argv) # one is for command only | ||
+ | |||
+ | if no_arg <= 1: | ||
+ | diskfile=raw_input("Disk file name please: ") | ||
+ | else: | ||
+ | diskfile=sys.argv[1] | ||
+ | |||
+ | # verify that the file exist | ||
+ | # notify when file is missing | ||
+ | |||
+ | try: | ||
+ | f=open(diskfile, 'rb') | ||
+ | f.seek(446) | ||
+ | |||
+ | ptt = f.read(64) | ||
+ | pte1 = ptt[0:16] | ||
+ | pte2 = ptt[16:32] | ||
+ | pte3 = ptt[32:48] | ||
+ | pte4 = ptt[48:64] | ||
+ | tfmt='<b3sb3sLL' | ||
+ | |||
+ | t1 = struct.unpack(tfmt,pte1) | ||
+ | t2 = struct.unpack(tfmt,pte2) | ||
+ | t3 = struct.unpack(tfmt,pte3) | ||
+ | t4 = struct.unpack(tfmt,pte4) | ||
+ | print "partition #","boot","start","length","type" | ||
+ | print "partition 1:",t1[0],t1[4], t1[5], t1[2] | ||
+ | print "partition 2:",t2[0],t2[4], t2[5], t2[2] | ||
+ | print "partition 3:",t3[0], t3[4], t3[5], t3[2] | ||
+ | print "partition 4:",t4[0], t4[4], t4[5], t4[2] | ||
+ | |||
+ | except IOError: | ||
+ | print "File missing." | ||
+ | </pre> | ||
= Resources = | = Resources = |
Latest revision as of 10:24, 3 July 2018
Contents
Overview
In order to use a physical storage device (e.g. Hard Disk, USB Flash memory, SSD, etc.), the first step is to create partition(s) on it. There many different ways to partition a physical storage device. The two most common partition schemes used on Intel based micro-computer systems are MBR and GPT. Detail about the MBR partition scheme can be found here, and GPT partition scheme can be found at [1].
On most Linux systems, fdisk or parted can be used to create, view, and manage MBR and GPT partitions.
The task for you in this assignment is to write a python module named "partinfo.py", which contains at least three functions: part_scheme(), mbr_part(), gpt_part(), which can be used to retrieve partition information from a specified physical storage device.
Instruction
Module name, function names, function parameter(s) and return values
Name your python module as "partinfo.py". The module should contains at least the following 3 functions:
-
part_scheme()
- which when called with a valid physical storage device name, will return the partition scheme used on the device. The returned value should only be- None - no valid partition scheme found on the device
- MBR - MBR partitions is/are found on the device
- GPT - GUID partitions is/are found on the device
-
mbr_part()
- which when called with a valid physical storage device name, will return all the partitions (primary, extended, and logical) found on the device. Each partition information returned must contain the following fields:- partition number
- partition flag (bootable or not)
- partition type
- start sector number (in LBA mode)
- size of partition in sectors
-
gpt_part()
- which when called with a valid physical storage device name, will return all the partitions found on the device. Each partition information returned must contain the following fields:- Partition type GUID
- Unique partition GUID
- First LBA sector of the partition
- Last LBA sector of the partition
- Attribute flags
- Partition name
Module and function documentation
Use the python docstring to provide embedded documentation for the module "partinfo.py" and the functions in it.
Sample Python codes
The following sample python code reads and displays primary partitions on a given physical storage device with MBR partition scheme. Please note that this python script does not check for extended partition and does not try to extract the information for logical partitions even they exist.
#! /usr/bin/python # read and display primary partition info # on a physical disk or disk image # import struct import sys # ask the user for disk file name # if not provided on the command line no_arg = len(sys.argv) # one is for command only if no_arg <= 1: diskfile=raw_input("Disk file name please: ") else: diskfile=sys.argv[1] # verify that the file exist # notify when file is missing try: f=open(diskfile, 'rb') f.seek(446) ptt = f.read(64) pte1 = ptt[0:16] pte2 = ptt[16:32] pte3 = ptt[32:48] pte4 = ptt[48:64] tfmt='<b3sb3sLL' t1 = struct.unpack(tfmt,pte1) t2 = struct.unpack(tfmt,pte2) t3 = struct.unpack(tfmt,pte3) t4 = struct.unpack(tfmt,pte4) print "partition #","boot","start","length","type" print "partition 1:",t1[0],t1[4], t1[5], t1[2] print "partition 2:",t2[0],t2[4], t2[5], t2[2] print "partition 3:",t3[0], t3[4], t3[5], t3[2] print "partition 4:",t4[0], t4[4], t4[5], t4[2] except IOError: print "File missing."