2017年3月19日 星期日

備份與回存硬碟分割資訊

備份

sfdisk -d /dev/sdx > part_table_filepath

回存

sfdisk /dev/sdx < part_table_filepath

root@cyvm:/dev# sfdisk /dev/sdd < /media/sf_hd_image_backup/5054/part_table
Checking that no-one is using this disk right now ... OK

Disk /dev/sdd: 698.7 GiB, 750156374016 bytes, 1465149168 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
Disklabel type: dos
Disk identifier: 0x5e487107

Old situation:

Device     Boot    Start        End    Sectors  Size Id Type
/dev/sdd1           2048   31459327   31457280   15G 27 Hidden NTFS WinRE
/dev/sdd2       31459328   38799359    7340032  3.5G 12 Compaq diagnostics
/dev/sdd3  *    38799360   39004159     204800  100M  7 HPFS/NTFS/exFAT
/dev/sdd4       39004160 1465145343 1426141184  680G  f W95 Ext'd (LBA)
/dev/sdd5       39006208 1465145343 1426139136  680G  7 HPFS/NTFS/exFAT

>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Created a new DOS disklabel with disk identifier 0x2fdd5485.
Created a new partition 1 of type 'Hidden NTFS WinRE' and of size 9.8 GiB.
/dev/sdd2: Created a new partition 2 of type 'FAT16' and of size 51.2 GiB.
/dev/sdd3: Created a new partition 3 of type 'HPFS/NTFS/exFAT' and of size 50.9 GiB.
/dev/sdd4: 
New situation:

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sdd1              63  20466809  20466747  9.8G 27 Hidden NTFS WinRE
/dev/sdd2  *     20466810 127732814 107266005 51.2G  6 FAT16
/dev/sdd3       127732815 234436544 106703730 50.9G  7 HPFS/NTFS/exFAT

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

dd

使用 dd 僅能備份主要分割資訊,所以不建議使用,這裡僅供研究參考用。

參考:https://wiki.archlinux.org/index.php/Fdisk

MBR 使用磁碟最前面 512 bytes 來儲存資訊,包含了三部份:

  1. 446 bytes: boot loader
  2. 64 bytes: partition table (共四組,每組使用16 bytes)
  3. 2 bytes: identifier

備份方法:

dd if=/dev/sdx of=part_table_filepath bs=1 skip=446 count=64

回存方法:

dd if=part_table_filepath of=/dev/sdx bs=1 seek=446 count=64

2017年3月18日 星期六

使用 systemd 操作 service - 列出所有受管理的項目

cy@cyvm:~$ systemctl status -all
● cyvm
    State: degraded
     Jobs: 0 queued
   Failed: 1 units
    Since: 六 2017-03-18 10:26:53 CST; 7h ago
   CGroup: /
           ├─init.scope
           │ └─1 /sbin/init splash
           ├─system.slice
           │ ├─avahi-daemon.service
           │ │ ├─859 avahi-daemon: running [cyvm.local
           │ │ └─931 avahi-daemon: chroot helpe
           │ ├─vboxadd-x11.service
...
cy@cyvm:~$ systemctl list-unit-files --all
UNIT FILE                                  STATE
proc-sys-fs-binfmt_misc.automount          static
dev-hugepages.mount                        static
dev-mqueue.mount                           static
proc-sys-fs-binfmt_misc.mount              static
sys-fs-fuse-connections.mount              static
sys-kernel-config.mount                    static
sys-kernel-debug.mount                     static
acpid.path                                 enabled
cups.path                                  enabled
systemd-ask-password-console.path          static
systemd-ask-password-plymouth.path         static
systemd-ask-password-wall.path             static
...

2017年3月17日 星期五

shell script 特殊技巧收集

將字串變大/小寫

大寫

cy@cyvm:~$ STR=teSt
cy@cyvm:~$ echo ${STR}
teSt

首字大寫

cy@cyvm:~$ echo ${STR^}
TeSt

全部大寫

cy@cyvm:~$ echo ${STR^^}
TEST

小寫

cy@cyvm:~$ STR=TeSt
cy@cyvm:~$ echo ${STR}
TeSt

首字小寫

cy@cyvm:~$ echo ${STR,}
teSt

全部小寫

cy@cyvm:~$ echo ${STR,,}
test