Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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)
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
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.
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.):
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.