Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
How to use Sfdisk to create or repartition your hard drive.
Well, one powerful feature of sfdisk is to repartition your hard drive or create new partitions. Create a file called "Test.data" which has entries in the following format, one per line, and the fields are comma delimited:
Start, Size, ID, Bootable
Start = cylinder to start at (first available cylinder if left blank), Size = number of cylinders (all if left blank), ID = type of partition (Linux, Swap, MSDOS, or other), and Bootable = if this partition is bootable. There are other options, but we won't get into them in this article.
To make it so you have 1 partition of 136 cylinders, a second partition with 254 cylinders of the SWAP ID (82), and a third Linux partition bootable that grabs the rest of the space (ID = 83), make a file list this,
1,136
101,254,82
201,,83,*
and then issue this command to take this configuration and execute it on your slave hard drive on your secondary controller
/sbin/sfdisk /dev/hdd << Test.data
and then issue this command to see what you did
/sbin/sfdisk /dev/hdd
I works ok for me. Maybe that little diddy on the end needs to be used for your situation....
Code:
# sfdisk /dev/hda < file.txt
Checking that no-one is using this disk right now ...
OK
Disk /dev/hda: 155061 cylinders, 16 heads, 63 sectors/track
Old situation:
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hda1 0+ 20317 20318- 10240240+ 7 HPFS/NTFS
/dev/hda2 0 - 0 0 0 Empty
/dev/hda3 0 - 0 0 0 Empty
/dev/hda4 0 - 0 0 0 Empty
New situation:
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hda1 * 0+ 155060 155061- 78150743+ 83 Linux
/dev/hda2 0 - 0 0 0 Empty
/dev/hda3 0 - 0 0 0 Empty
/dev/hda4 0 - 0 0 0 Empty
Successfully wrote the new partition table
Re-reading the partition table ...
If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)
what version are you using?
mine: sfdisk version 3.07 (aeb@cwi.nl, 990908)
does your file look like mine?
Ok if I use this in my file:
;;83;*
I get this:
Code:
root@xxxxxx: /www: sfdisk /dev/hdb < Test.data
Checking that no-one is using this disk right now ...
OK
Disk /dev/hdb: 2097 cylinders, 16 heads, 63 sectors/track
Old situation:
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hdb1 * 0+ 2091 2092- 1054336+ 6 FAT16
/dev/hdb2 0 - 0 0 0 Empty
/dev/hdb3 0 - 0 0 0 Empty
/dev/hdb4 0 - 0 0 0 Empty
unrecognized bootable flag - choose - or *
sfdisk: bad input
root@xxxxxx: /www:
so I added the ; at the end.
;;83;*;
then I got this
Code:
root@xxxxxxxx: /www: sfdisk /dev/hdb < Test.data
Checking that no-one is using this disk right now ...
OK
Disk /dev/hdb: 2097 cylinders, 16 heads, 63 sectors/track
Old situation:
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hdb1 * 0+ 2091 2092- 1054336+ 6 FAT16
/dev/hdb2 0 - 0 0 0 Empty
/dev/hdb3 0 - 0 0 0 Empty
/dev/hdb4 0 - 0 0 0 Empty
partial c,h,s specification?
sfdisk: bad input
# sfdisk -v
sfdisk version 3.07 (aeb@cwi.nl, 990908)
I think the problem is coming from the file editor. When I used kwrite, I got the error.
When I used the vi editor ( vi test.txt ) to put the data in, then the error doesn't exist.
Code:
# kwrite test.txt
# sfdisk /dev/hda < test.txt
Checking that no-one is using this disk right now ...
OK
Disk /dev/hda: 155061 cylinders, 16 heads, 63 sectors/track
Old situation:
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hda1 0+ 20317 20318- 10240240+ 7 HPFS/NTFS
/dev/hda2 0 - 0 0 0 Empty
/dev/hda3 0 - 0 0 0 Empty
/dev/hda4 0 - 0 0 0 Empty
sfdisk: long or incomplete input line - quitting
Code:
# vi test.txt
# sfdisk /dev/hda < test.txt
Checking that no-one is using this disk right now ...
OK
Disk /dev/hda: 155061 cylinders, 16 heads, 63 sectors/track
Old situation:
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hda1 0+ 20317 20318- 10240240+ 7 HPFS/NTFS
/dev/hda2 0 - 0 0 0 Empty
/dev/hda3 0 - 0 0 0 Empty
/dev/hda4 0 - 0 0 0 Empty
New situation:
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hda1 * 0+ 155060 155061- 78150743+ 83 Linux
/dev/hda2 0 - 0 0 0 Empty
/dev/hda3 0 - 0 0 0 Empty
/dev/hda4 0 - 0 0 0 Empty
Successfully wrote the new partition table
Re-reading the partition table ...
If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)
root@xxxxxx: ~: sfdisk /dev/hdb << EOF
> ,,83,*
> EOF
Checking that no-one is using this disk right now ...
OK
Disk /dev/hdb: 2097 cylinders, 16 heads, 63 sectors/track
Old situation:
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hdb1 * 0+ 2091 2092- 1054336+ 6 FAT16
/dev/hdb2 0 - 0 0 0 Empty
/dev/hdb3 0 - 0 0 0 Empty
/dev/hdb4 0 - 0 0 0 Empty
New situation:
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hdb1 * 0+ 2096 2097- 1056887+ 83 Linux
/dev/hdb2 0 - 0 0 0 Empty
/dev/hdb3 0 - 0 0 0 Empty
/dev/hdb4 0 - 0 0 0 Empty
Successfully wrote the new partition table
Re-reading the partition table ...
If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)
You're welcome!
There is a command to strip newline stuff off of files which are created with some editors like notepad, can't remember exactly what that command is right now.
That will let you modify an existing layout or make one that's close with something like Gparted, dump it to a file, modify it, put it back. Or email it or publish it, etc. You can always practice on any blank device but an SD card in a reader is especially cheap and expendable. If you need to do sector math apcalc is especially good, you can use big integers like 1024209543168 and it doesn't put them into scientific notation.
And don't use anything but true text editors on these files. It can be in Windows (like Notepad) but then FTP the file back to Linux as text, not binary, so the line ends get converted.
What I'm searching for and not finding is whether you can put comments in there. Guess I should try it. OK, you can use # at the beginning of a line it seems, I didn't try it in the middle of a line. So you can do stuff like:
Code:
label: dos
unit: sectors
device: /dev/sdb
first-lba: 64
# to use: sfdisk /dev/sdb < try1
# dummy uboot area
/dev/sdb1 : start= 64, size= 4000, type= 6
# stick in an extended
/dev/sdb2 : start= 4064, size= 62329888, type= 85
# now 10 gb swap start 2048 into the extended @ 6112 (wasted space?)
/dev/sdb5 : start= 6112, size= 19531250, type= 82
# now close with a main OS partition
/dev/sdb6 : start= 1953736, size= 60380216, type= 83
# 62333952 sectors in the full sd (32 gb)
# "parted /dev/sdb unit s print" to print sectors
Last edited by ab1jx; 05-04-2020 at 12:52 PM.
Reason: tried a #
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.