Skip to content

Instantly share code, notes, and snippets.

@samueldr
Created September 3, 2019 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samueldr/f481ef6306c2d0c18b2dde4422fb7b05 to your computer and use it in GitHub Desktop.
Save samueldr/f481ef6306c2d0c18b2dde4422fb7b05 to your computer and use it in GitHub Desktop.
$ run_test simple 1024 1024
$ local name=simple
$ shift
$ local first_size=1024
$ shift
$ local second_size=1024
$ shift
$ local start=1024
$ rm -f simple.img
$ truncate -s 3072KiB simple.img
$ ls -lh simple.img
-rw-r--r-- 1 samuel users 3.0M Sep 3 17:50 simple.img
$ ls -l simple.img
-rw-r--r-- 1 samuel users 3145728 Sep 3 17:50 simple.img
$ cat
$ cat script.sfdisk
label: dos
label-id: 0x012345678
start=1024KiB, size=1024KiB
start=2048KiB, size=1024KiB
$ sfdisk simple.img
Checking that no-one is using this disk right now ... OK
Disk simple.img: 3 MiB, 3145728 bytes, 6144 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
>>> Script header accepted.
>>> Script header accepted.
>>> Created a new DOS disklabel with disk identifier 0x12345678.
simple.img1: Created a new partition 1 of type 'Linux' and of size 1 MiB.
simple.img2: Created a new partition 2 of type 'Linux' and of size 1 MiB.
simple.img3: Done.
New situation:
Disklabel type: dos
Disk identifier: 0x12345678
Device Boot Start End Sectors Size Id Type
simple.img1 2048 4095 2048 1M 83 Linux
simple.img2 4096 6143 2048 1M 83 Linux
The partition table has been altered.
Syncing disks.
$ rm script.sfdisk
$ run_test complex 13312 1024
$ local name=complex
$ shift
$ local first_size=13312
$ shift
$ local second_size=1024
$ shift
$ local start=1024
$ rm -f complex.img
$ truncate -s 15360KiB complex.img
$ ls -lh complex.img
-rw-r--r-- 1 samuel users 15M Sep 3 17:50 complex.img
$ ls -l complex.img
-rw-r--r-- 1 samuel users 15728640 Sep 3 17:50 complex.img
$ cat
$ cat script.sfdisk
label: dos
label-id: 0x012345678
start=1024KiB, size=13312KiB
start=14336KiB, size=1024KiB
$ sfdisk complex.img
Checking that no-one is using this disk right now ... OK
Disk complex.img: 15 MiB, 15728640 bytes, 30720 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
>>> Script header accepted.
>>> Script header accepted.
>>> Created a new DOS disklabel with disk identifier 0x12345678.
complex.img1: Created a new partition 1 of type 'Linux' and of size 13 MiB.
complex.img2: All space for primary partitions is in use.
Failed to add #2 partition: Invalid argument
Leaving.
#!/usr/bin/env bash
set -e
set -u
PS4=" $ "
set -x
run_test() {
local name=$1 ; shift
local first_size=$1 ; shift
local second_size=$1 ; shift
local start=1024
rm -f "$name.img"
truncate -s $((start+first_size+second_size))KiB "$name.img"
ls -lh "$name.img"
ls -l "$name.img"
cat > script.sfdisk <<-EOF
label: dos
label-id: 0x012345678
start=$((start))KiB, size=$((first_size))KiB
start=$((start+first_size))KiB, size=$((second_size))KiB
EOF
cat script.sfdisk
sfdisk "$name.img" < script.sfdisk
rm script.sfdisk
}
# Simple, two parts, each 1MiB.
run_test simple 1024 1024
# 13MiB, so should stay aligned both to MiB and sectors.
# Somehow the second partition can't be created!
run_test complex $((1024 * 13)) 1024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment