LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-13-2008, 12:28 AM   #1
sunlinux
Member
 
Registered: Feb 2006
Distribution: RHCL 5
Posts: 239

Rep: Reputation: 30
removing all partition from disk


I am developing own customize linux. like running live linux from usb and then copy it contents to disk after partitions.

I want to know if I detect Disk(hda/sda).

how to make a script to detect partitions on it, if any partition found how to delete all partition in one go, and make a single partition hda1 n copy my usb contents n grub it. and run linux from disk after removing usb.

Pls. guys help me ..
 
Old 12-13-2008, 03:31 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
I would advise against auto removing any partitions on the hard drive.

how about including parted or fdisk in your USB?

Would you just want to create a single / partition? what about swap space?
 
Old 12-14-2008, 10:41 PM   #3
sunlinux
Member
 
Registered: Feb 2006
Distribution: RHCL 5
Posts: 239

Original Poster
Rep: Reputation: 30
hi Disillusionist,

I want to start with only / partition, creating swap will make it complex isn'it.

also tell me hot to auto part with usb (fdisk) is thr any specific command ?
 
Old 12-15-2008, 12:48 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
AFAIK there is no way to automatically remove existing partitions and create a single partition with fdisk.

You can display exising partition table (as root):
Code:
fdisk -l /dev/sda
Using parted to display existing partitions (as root):
Code:
parted -s /dev/sda print
You need to list the previous partition table, so that you know what partitions need to be removed.

Using parted to remove partition 1 (as root):
Code:
parted -s /dev/sda rm 1
Using parted to make a partition (as root):
Code:
parted -s /dev/sda mkpart primary 0 8590
You need to specify the start and end point when creating a partition, as you want a single partition of the whole disk, you can take the end partition from the output of the parted print statement (shown above)


format the partition:
Code:
mke2fs -T ext3 /dev/sda1
Man mke2fs for more options

Code suggestions

listing existing partitions:
Code:
parted -s /dev/sda print|awk '/^ / {print $1}'
finding the size of the disk:
Code:
parted -s /dev/sda print|awk '/^Disk/ {print $3}'|sed 's/[Mm][Bb]//'
 
Old 12-15-2008, 03:01 AM   #5
sunlinux
Member
 
Registered: Feb 2006
Distribution: RHCL 5
Posts: 239

Original Poster
Rep: Reputation: 30
nice suggestion n commands .

What if in case I want no user intervention in printing, deleting(all) & creating a partition on disk with scripts.I want all is done in background automatically

Last edited by sunlinux; 12-15-2008 at 03:03 AM.
 
Old 12-15-2008, 12:00 PM   #6
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
That was where I was heading with the code suggestions, you need to use the print statement so that you can find the information that you need.

Fully automated example (not tested):
Code:
#!/bin/bash
###
### Script to automatically format all partitions on /dev/sda
### and create a single partition for the whole disk
###

# Remove each partition
for v_partition in $(parted -s /dev/sda print|awk '/^ / {print $1}')
do
   parted -s /dev/sda rm ${v_partition}
done

# Find size of disk
v_disk=$(parted -s /dev/sda print|awk '/^Disk/ {print $3}'|sed 's/[Mm][Bb]//')

# Create single partition
parted -s /dev/sda mkpart primary 0 ${v_disk}

# Format the partition
mke2fs -T ext3 /dev/sda1

###
### Then all you have to do is copy your image to the new partition
### install grub and reboot
###

Last edited by Disillusionist; 12-16-2008 at 02:36 AM.
 
Old 12-16-2008, 01:10 AM   #7
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
All of the following examples assume it is /dev/sda that you are wanting to mess with. Change the /dev/sda specification to the actual drive you are working on.

WARNING! Do the following AT YOUR OWN RISK. You can easily screw the pooch if you are not extremely careful. I make no guarantees that I have typed the examples below accurately and without error. Read up on the "dd" command so you know what you are doing before trying any of this.

WARNING! Do NOT mix up "if" ("input file") and "of" ("output file") arguments. This is an error that is easy to make, and can be devastating. You get no warning from "dd" when you attempt to do something ill advised. "dd" just does it without question.

=====

Before doing any of this, I would recommend saving your existing MBR to an external place (flashdrive, etc.):

Code:
dd bs=512 count=1 if=/dev/sda of=/path/to/your/flashdrive/mbr.bin
To restore your MBR if you later find you've screwed it up:

Code:
dd bs=512 count=1 if=/path/to/your/flashdrive/mbr.bin of=/dev/sda
======

To wipe only the partition table, leaving the boot code intact:

Code:
dd bs=1 seek=446 count=64 if=/dev/zero of=/dev/sda
To wipe the entire MBR (which is probably what you really want to do):

Code:
dd bs=512 count=1 if=/dev/zero of=/dev/sda
To wipe all of Track Zero (which contains the MBR and possibly a boot loader):

Code:
dd bs=512 count=63 if=/dev/zero of=/dev/sda
By wiping all of Track Zero, you wind up with a "blank" disk, unpartitioned, with no sneaky boot sector viruses. The disk behaves like it did when it came from the factory (assuming it didn't come from the factory already partitioned and formatted - some external disks come this way).

=====

Background:

A sector is 512 bytes long. The first sector of a disk is the MBR (Master Boot Record). The MBR contains 446 bytes of "boot code", followed by 64 bytes of "partition table", followed by two bytes of who-konws-what (not used by Linux). The "partition table" contains four "partition records", each being 16 bytes in length. Track Zero is the first 63 sectors of a disk (and therefore contains the MBR plus 62 additional sectors). These 62 additional sectors are sometimes used to hold a "boot loader", or part thereof. Ill-advised copy protection schemes sometimes use these 62 sectors, often times with disastrous results for the user. Actual "normal" data storage begins on Track One, which immediately follows Track Zero.

Last edited by haertig; 12-16-2008 at 01:13 AM.
 
Old 12-16-2008, 02:43 AM   #8
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
haertig, the OP is trying to script the creation of a single partition to contain the / filesystem of his own customised linux installation.

Hopefully, he is going to warn anyone that runs the installation that their entire hard drive will be formatted.

As the intention is to remove all existing partitions and create a single partition of the whole disk, I would think that using dd is a little overkill.

I agree that making a backup of the MBR would be a good idea, but is this necessary if the rest of the disk is being zapped?

The OP will still need to install grub to the MBR to do the boot process...

Last edited by Disillusionist; 12-16-2008 at 02:44 AM. Reason: spelling
 
Old 08-04-2011, 04:07 AM   #9
SeLLeRoNe
LQ Newbie
 
Registered: Aug 2011
Location: Rome
Distribution: CentOS/Ubuntu
Posts: 1

Rep: Reputation: 0
Hi guys,

ive never registered to this website till now but ive to say, ive always found nice reply to my questions without need to ask them

Today im writing to this post cause here ive found the most complete reply to my question, but, not yet totally complete.

Im working on a restore cd for my Ubuntu 11.04 (but should be applied to any system i suppose).

The way to backup is just make a tgz file with / in it (totally about 1.2GB). Of course is just the standard system with some software and appearence customization.

Now, i wanna make both (CD and Pen Drive) for recovery all automatically.

Till now, ive tryed remastersys that yes it work, but, i wanna be more customizable (this ISO will be a standard restore system for some computer i would like to sell with pre-installed Ubuntu).

Now what i need is a bootable CD image where i can start (automatically of course) some scripts (actually one for remove partitions and create defaults, one for mount partition, uncompress backup.tgz and unmount, one for create user/change hostname.

Well, ive talked a lot, the main problem now (except for bootable iso image that i can customize) is that the script up here is going to make just a single partition with total disk space, i would like that make a 4GB Swap partition and then a / with the rest of the space but i cant find (unless till now) the parted script line for make a partition and im aware on how i should change the / partition line.

I suppose that swap should be 0 4096 and so change the / partition line from 0 to 4097 but actually im not sure, and i dont know if that is enough, so, please who are able to and want to, help me

Also, if you know/have a good base iso that start just a bash where i can add then scripts to startup, i will appreciate so much.

Thanks in advance.

Regards
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing Linux Partition and merging that space with my other Windows Partition RamenBooko Linux - General 3 10-11-2007 02:47 PM
Copy files from partition to partition too slow, SATA hard disk.What should I do£¿ Ryanlee SUSE / openSUSE 20 10-31-2005 07:30 AM
Reclaim disk partition from hard disk installation EStester Linux - Enterprise 2 03-09-2005 11:46 AM
Reclaim disk partition from hard disk installation EStester Linux - General 1 03-09-2005 11:25 AM
removing a raid0 disk johntron Linux - Hardware 2 10-07-2004 11:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration