Can a partition be created with a command in a Bash script?


Results 1 to 6 of 6

Thread: Can a partition be created with a command in a Bash script?

  1. #1
    Join Date
    Jun 2004
    Location
    Newcastle upon Tyne
    Posts
    2,978

    Can a partition be created with a command in a Bash script?

    I am venturing into LVM. It appeas possible for a user to create, format, mount a logical volume and do whatever needed with terminal commands and so everythng can be controlled and instructed by a script file.

    I wish to know if there is a way to create a normal partition in a hard disk by instructions in a script file.

    So far the partitioning of a hard disk seems to require running a partition manipulator like cfdisk, fdisk or sfdisk which works interactively with the user.

    I know exactly what I after and wish to carry out the work unattended.

    It does not appear possible at the moment and may be for a good reason too because it can be highly dangerous.

    Will appreciate any feed back on the subject.
    Linux user started Jun 2004 - No. 361921
    Using a Linux live CD to clone XP
    To install Linux and keep Windows MBR untouched
    Adding extra Linux & Doing it in a lazy way
    A Grub menu booting 100+ systems & A "Howto" to install and boot 145 systems
    Just cloning tips Just booting tips A collection of booting tips

    Judge asked Linux "You are being charged murdering Windoze by stabbing its heart with a weapon, what was it?" Replied Linux "A Live CD"

  2. #2
    Join Date
    Apr 2003
    Location
    UK
    Posts
    1,180
    Actually sfdisk can be used non-interactively so can probably do what you want.

  3. #3
    Join Date
    Sep 1999
    Posts
    3,202
    Or use expect/send and regular fdisk

  4. #4
    Join Date
    Oct 2002
    Location
    AZ, USA
    Posts
    110

    Sample

    I used the following at my prior employer so it has some base assumptions in it from that enviroment.

    Code:
    #!/bin/bash
    # First parameter should be the device to format  <Required>
    # second parameter is the Label for the partition <Required>
    # third parameter is the mount point             <Optional>
    #
    #  Note This is designed to setup an entire "disk/LUN" as 1 partition.
    #       It is designed to produce "VAULTs" for TapeLabs VTS as used at some prior employer.
    #
    # !!!! this script needs to be run as ROOT for fdisk and mke2fs to be
    #  found in $PATH << JHC >>
    #
    if [ $# -lt 2 ]; then
    	echo "usage Format_Vault requires a device, fdisk comand file, and Label fields"
    	echo "Format_Vault.sh device  lab [Mount]"
    	echo "where device is the disk to be formated (sda,sdb,sdc,...)"
    	echo "      Label is the label to put on the first partition"
    	echo "      Mount is the optional mount point relative to /"
    	exit 1
    fi
    if [ $# -gt 3 ]; then
    	echo "warning more than 3 parameters provided extras ignored"
    fi
    if [ -z $1 ];then
       echo " error on device param => ${1}"
       exit 1
    fi
    device="${1}"
    #
    if $( echo $2 | grep --quiet -P '^VAULT\d\d$' )
    then
    	lab="${2}"
    else
    	echo " Label field is not of form VAULTnn => ${2}"
    	exit 1
    fi
    device=${device##/dev/}
    
    part=${device}1
    echo " fdisk /dev/${device}"
    # call fdisk on device /dev/$1 with input
    # New
    # Primary
    # 1'st partition
    # default starting address
    # default ending address
    # Write
    fdisk /dev/"${device}" <<EOF1
    n
    p
    1
    
    
    w
    EOF1
    fdisk /dev/"${device}" <<EOF2
    p
    EOF2
    # format the partition just created
    echo "mke2fs -j -L /${lab} /dev/${part}"
    mke2fs -j -L /"${lab}" /dev/"${part}"
    
    if [ ${#3} -eq '7' ]; then
    	echo "mkdir /${3}"
    	mkdir /${3}
    fi
    You will need to make changes to it if you use it.

  5. #5
    Join Date
    Jun 2004
    Location
    Newcastle upon Tyne
    Posts
    2,978
    Thanks for tips.

    I am having fun trying them out in tandem with doing the same with LV in the LVM.
    Linux user started Jun 2004 - No. 361921
    Using a Linux live CD to clone XP
    To install Linux and keep Windows MBR untouched
    Adding extra Linux & Doing it in a lazy way
    A Grub menu booting 100+ systems & A "Howto" to install and boot 145 systems
    Just cloning tips Just booting tips A collection of booting tips

    Judge asked Linux "You are being charged murdering Windoze by stabbing its heart with a weapon, what was it?" Replied Linux "A Live CD"

  6. #6
    Join Date
    Sep 2005
    Posts
    681
    LVM is the only way i organize data on my computer. its so smart and powerful . well LVM2 is .
    "Software is like sex: it's better when its free."
    -LINUS TORVALDS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •